In my program I have an array list containing product objects in it. I want to remove duplicated product objects from it. Is there any efficient way other than looping over each element and compare them.
7 Answers
You can just put element into Set. Set keep unique values only.
List<String> list=new ArrayList<>();
Set<String> set=new HashSet<>();
set.addAll(list); // now you have unique value set
If you want to final result as unique value List just you need to get this Set as List
List<String> uniqueValList=new ArrayList<>(set);
Comments
The advice above to use Set is good - but if you need to keep the order just use a LinkedHashSet http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html
List<String> list = ...
Set<String> set = new LinkedHashSet<>(list);
list.clear();
list.addAll(set);
That will preserve order and remove all duplicates.
The result will be case sensitive though in the case of strings.