Comparing new Integer objects in an ArrayList question

I store Integer objects representing the index of the objects I want to track. Later in my code, I want to check if a specific index of an object matches one of those integers that I saved earlier. I do this by creating an ArrayList and creating a new Integer from the for loop index:

ArrayList<Integer> courseselectItems = new ArrayList(); //Find the course elements that are within a courseselect element and add their indicies to the ArrayList for(int i=0; i<numberElementsInNodeList; i++) { if (nodeList.item(i).getParentNode().getNodeName().equals("courseselect")) { courseselectItems.add(new Integer(i)); } } 

Then I want to check later if the ArrayList contains a specific index:

 //Cycle through the namedNodeMap array to find each of the course codes for(int i=0; i<numberElementsInNodeList; i++) { if(!courseselectItems.contains(new Integer(i))) { //Do Stuff } } 

My question is: when I create a new Integer using new Integer(i) , can I compare integers using ArrayList.contains() ? That is, when I create a new object using new Integer(i) , will it be the same as the previously created Integer object, if the int value used to create them is the same?

I hope I have not made this too incomprehensible. Thanks for the help!

+7
java arraylist integer
source share
7 answers

Yes, you can use List.contains() since it uses equals() , and Integer supports this when comparing with other Integer s.

In addition, due to automatic boxing, you can simply write:

 List<Integer> list = new ArrayList<Integer>(); ... if (list.contains(37)) { // auto-boxed to Integer ... } 

It is worth noting that:

 List list = new ArrayList(); list.add(new Integer(37)); if (list.contains(new Long(37)) { ... } 

will always return false , because Integer not Long . At some point, it causes most people.

Finally, try making your variables, which are Java collections of an interface type, rather than a specific type, therefore:

 List<Integer> courseselectItems = new ArrayList(); 

not

 ArrayList<Integer> courseselectItems = new ArrayList(); 
+17
source share

My question is: when I create a new Integer using the new Integer (i), can I compare integers using ArrayList.contains ()? That is, when I create a new object using the new Integer (i), will it be the same as the previously created Integer object, if the int value used to create them is the same?

The short answer is yes.

Long answer:

That is, when I create a new object using the new Integer (i), will it be the same as the previously created Integer object, if the int value used to create them is the same?

I assume that you mean "... it will be the same instance as ..."? The answer to this question is: no - calling new will always create a separate instance separately from the previous instance, even if the constructor options are identical.

However, despite having a separate identity, these two objects will have an equivalent value, that is, calling .equals () between them will return true.

Collection.contains ()

It turns out that having separate instances of the equivalent value (.equals () returns true) is fine. The .contains() method is in the Collection interface. The Javadoc description for .contains() reads:

http://java.sun.com/javase/6/docs/api/java/util/Collection.html#contains(java.lang.Object)

boolean contains (Object o)

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

That way, he will do what you want.

Data structure

You should also consider if you have the correct data structure.

Is the list purely protection? important order? Are you worried about duplicates? Since a list is an order, using a list may mean that your code takes care of the order. Or that you need to maintain duplicates in the data structure.

However, if the order is not important, if you do not want or will not have duplicates, and if you really use this data structure only to check whether it contains a certain value, then you might think about whether you should use Set instead.

+4
source share

The short answer is yes, you should be able to do ArrayList.contains(new Integer(14)) , for example, to see if there are 14 in the list. The reason is that Integer overrides the equals method to correctly compare itself with other instances with the same value.

+1
source share

Yes, it will, because List.contains() uses the equals() method of the object to compare. And Integer.equals() compares the integer value.

+1
source share

As cletus and DJ are mentioned, your approach will work.

I don't know the context of your code, but if you don't need specific indexes, consider the following style as well:

 List<Node> courseSelectNodes = new ArrayList<Node>(); //Find the course elements that are within a courseselect element //and add them to the ArrayList for(Node node : numberElementsInNodeList) { if (node.getParentNode().getNodeName().equals("courseselect")) { courseSelectNodes.add(node); } } // Do stuff with courseSelectNodes for(Node node : courseSelectNodes) { //Do Stuff } 
+1
source share

I put my answer in the form of (passing) a test, as an example of how you could examine this yourself. Keeping you from using SO is great - it's just trying to boost performance tests .

 import java.util.ArrayList; import junit.framework.TestCase; public class ContainsTest extends TestCase { public void testContains() throws Exception { ArrayList<Integer> list = new ArrayList<Integer>(); assertFalse(list.contains(new Integer(17))); list.add(new Integer(17)); assertTrue(list.contains(new Integer(17))); } } 
+1
source share

Yes, automatic boxing occurs, but this leads to reduced performance. From your example it is not clear why you would like to solve the problem this way.

In addition, because of the box, manually creating an Integer class is redundant.

0
source share

All Articles