Java List Equality and Object Identity


Java lists manage inserted objects based on their equality (see List JavaDoc). In some instances, it can be confusing that this equality has a different meaning than an equal identity of objects based on their reference.

This becomes quite apparent in the following example:

final List<String> list = new ArrayList<String>(); 
final String s1 = "one"; 
final String s2 = "one"; 

Here, we would usually expect “1” as result, since the String s2 has been inserted after the String s1 (which should have the index 0). However, Java prints “0” as answer in above example. 0 is returned since the indexOf(…) method only attempts to find an object which is equal to the passed parameter (“one”) and not the identical object to the passed object (s2).


Use the IdentityArrayList below:

* Copyright 2011 Max Rohde

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
package mx.gwtutils;

import java.util.ArrayList;
import java.util.Collection;

* An array list implemention depending on object identity (==) rather than
* equality (.equals) to identify elements.<br/>
* <br/>
* See: <a href=
* ""
* >KnowledgeNetworks: Java List Equality and Object Identity</a>
* @author <a href="">Max Rohde</a>
* @param <E>
public class IdentityArrayList<E> extends ArrayList<E> {

private static final long serialVersionUID = 1L;

public boolean remove(final Object o) {
return super.remove(this.indexOf(o)) != null;

public boolean contains(final Object o) {
return indexOf(o) >= 0;

public int indexOf(final Object o) {
for (int i = 0; i < size(); i++)
if (o == get(i))
return i;
return -1;

public int lastIndexOf(final Object o) {
for (int i = size() - 1; i >= 0; i--)
if (o == get(i))
return i;
return -1;

public IdentityArrayList() {

public IdentityArrayList(final Collection<? extends E> c) {

public IdentityArrayList(final int initialCapacity) {


There are a number of further implementation of List and Set in Java, which consider the object identity rather than the object equality:

IdentityArrayList part of the Sun/Oracle JDK at sun.awt.util (on

IdentitySet by Sebastian Thomschke (on

But please note that these lists are not part of the Java JRE. Lists are often critical parts of an application (in terms of performance and reliability). Therefore, the best advice might be to tried to work around scenarios as given above, for instance by assigning unique identifiers to objects.

For HashMaps, however, a special implementation utilizing object identity rather than equality checks is provided in the JRE:

IdentityHashMap JRE 1.5.0 Java Doc

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s