Removing List Duplicates using Set Object

In this tutorial, I am sharing simple program about Removing List Duplicates using Set Object. The java example program was tested and output is shared in the same post.

RemoveDuplicateList.java

package com.dineshkrish;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class RemoveDuplicateList {
public static void main(String[] args) {
// Defining Array List Object
List<Integer> numberList = new ArrayList<Integer>();
numberList.add(1);
numberList.add(2);
numberList.add(2);
numberList.add(3);
numberList.add(4);
numberList.add(4);
numberList.add(4);
numberList.add(1);
numberList.add(6);
numberList.add(6);
System.out.println("Before Removing Duplications in the List");
System.out.println("----------------------------------------");
System.out.println(numberList);
// Defining Set Object
Set<Integer> set = new TreeSet<Integer>(numberList);
System.out.println("\nAfter Removing Duplications in the List");
System.out.println("------------------------------------------");
System.out.println(set);
}
}

Output

—————–
Before Removing Duplications in the List
—————————————-
[1, 2, 2, 3, 4, 4, 4, 1, 6, 6]

After Removing Duplications in the List
——————————————
[1, 2, 3, 4, 6]

Tags:

No responses yet

Leave a Reply

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