Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove duplicated elements from an array list [duplicate]

Tags:

java

arraylist

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.

like image 607
Samantha Withanage Avatar asked Sep 20 '26 19:09

Samantha Withanage


1 Answers

just add all element into set. it wont allow duplicate values

   List<Product> list=new ArrayList<>();
   Set<Product> set=new HashSet<>();
   set.addAll(list);
like image 133
shreyansh jogi Avatar answered Sep 23 '26 08:09

shreyansh jogi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!