Question

I used this straight-forward method:

    Collection<Integer> aCollection = Arrays.asList(1,2,3,4,5,6);
    Integer a = new Integer(5);

    if( aCollection.contains(a) )
        System.out.println("aCollection contains 5");

Result is "aCollection contains 5". Integers are not unique objects in whole system, but in this case are the "5" in Collection<Integer> and the new Integer(5) in fact references to the same object? Can I safely assume that any collection will behave this way when calling .contains()?

I am not sure because when comparing references and values by == result is:

3 == new Integer(3)
new Integer(3) != new Integer(3)
new Integer(3) == 3
Was it helpful?

Solution 2

It is defined in JavaDoc:

boolean contains(Object o)

Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

In words: If o is null, contains will return true if the collection contains a null element, otherwise it will return true if and only if there is an element that equals() the one you are searching for.

Every call to new Integer(5) will return a different reference, but all the objects are equal to one another according to the equals() method of the Integer class:

Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.

OTHER TIPS

contains does not compare by reference. new Integer(50000) is different from another call to new Integer(50000). However, firstInt.equals(new Integer(50000)) will return true, hence contains will work. I use higher numbers since interning can happen with lower ones.

0-127 will be put into a fixed pool when performing boxing but not when explicitly creating new objects so you'll have the same reference to the same value, but above that, you have no guarantee of any interning, and references may be to different Integer objects for the same value. Integer.valueOf( will attempt to intern where appropriate, namely always within [-128, 128) and with no specification for or against interning outside this range.

Can I safely assume that any collection will behave this way when calling .contains()?

You can assume that .contains(Object o) will return true if and only if one or more members of the collection satisfies thatmember.equals(o), or if o is null, and there is at least one null member.

Technically:

Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).

.contains() of an collection calls the .equals() method internally.

In your case you are using an Integer object. And an Integer object .equals() might return true in all the cases you have listed.

However that might not be case for a custom object you have say Person.

Now for a collection

Collection<Person> aCollection = Arrays.asList(b,c,d);
    Person a = new Person("Abhishek");

may return false. because you have not overridden the .equals() method in your Person class

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top