Java remove duplicate objects in ArrayList

arraylist, duplicates, java

Solution

Example

List<Item> result = new ArrayList<Item>();
Set<String> titles = new HashSet<String>();

for( Item item : originalList ) {
    if( titles.add( item.getTitle() )) {
        result.add( item );
    }
}

Reference

Set Java Data Structures

Problem

I have a very lengthy ArrayList comprised of objects some of them however, are undoubtedly duplicates. What is the best way of finding and removing these duplicates. Note: I have written a boolean-returning compareObjects() method.

Original source

Related problems