How to Remove Duplicate Object from ArrayList in Java

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class RemoveDuplicate {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();

list.add(101);
list.add(158);
list.add(136);
list.add(120);// Duplicate Object
list.add(120);
list.add(101);
// Itrate all data
for (Integer i : list) {

System.out.println(i);
}

System.out.println(“——————————————-“);

Set<Integer> set = new LinkedHashSet<>(list);
// Itrate Unique Data
for (Integer i : set) {

System.out.println(i);
}

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *