Search code examples
javalinked-listcontainslong-integer

List of Longs how do you check if it contains a value?


I am attempting to check if a list of long's does not contain a certain value. It would seem as though that condition is never met, even though I know that one value does not exist in a certain linked list...

for(int i : organizationIDs){
    if(!ListOfOrgIds.contains(Long.valueOf(i))){
        addThese.add(new Long(i));
    }
}

I am essentially looking for value that doesn't exist in the orgID's array, if it doesn't exist, add it to the addThese linked list... Am I missing some nuance with Long's that I should know?

ListOfOrgIds as found in the debugger

14057
821
18021

OrganizationIDs as found in the debugger

821
14057
18021

Let me just put it this way, I am looking right at the debugger, and it is telling me that ListOfOrgIds.contains(i) is false... which is patently untrue...enter image description here

To be specific, look at the the values of ListOfOrgs...

enter image description here

821 is indeed in there. Why am I getting a false on the contains call?


Solution

  • The problem you are having is that java does not allow a primitive to be widened then boxed, only boxed then widened. This means an int cannot become a Long, but an int could become an Object (via Integer). The top answer to this question describes it fairly well. You don't get any compilation feedback in this case as the contains method does not use the type parameter of list, it accepts any object. This has caught me out many times in the past too.

    Below is a SSCCE showing both the result you are getting, and a working example. Note all that is required is to explicitly cast the int to a long.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class BoxingTest {
    
        public static void main(String[] args) throws Exception {
    
            List<Integer> intList = Arrays.asList(new Integer[]{14, 24, 69});
            List<Long> sourceLongList = Arrays.asList(new Long[]{14L, 17L});
    
            List<Long> resultsList;
            /* Test as in question code */
            resultsList = new ArrayList<Long>();
            for(int i : intList){
                if(!sourceLongList.contains(i)){
                    resultsList.add(new Long(i));
                }
            }
            printList(resultsList);
    
            /* Can't box then widen, so cast */
            resultsList = new ArrayList<Long>();
            for(int i : intList){
                if(!sourceLongList.contains((long)i)){
                    resultsList.add(new Long(i));
                }
            }
            printList(resultsList);
    
        }
    
        private static <T> void printList(List<T> values){
            StringBuilder contents = new StringBuilder();
            for(T value : values){
                contents.append(value);
                contents.append(" ");
            }
            System.out.println("List contains: " + contents);
        }
    
    }