List Remove Duplicate Elements Java

List Remove Duplicate Elements Java

Note: It is advised to read List Fundamentals before proceeding the following programs.

Four programs are given on List with different functionalities.

  1. List Methods: Uses frequently used methods like size(), add(), get(), contains(), indexOf(), lastIndexOf(), subList(), remove(), clear() and isEmpty().
  2. List Iteration: Comparing two lists with equals(), converting list elements to array with toArray(), printing array elements with foreach loop, printing list elements with Iterator (forward) and ListIterator (forward and backward).
  3. List Reversing Sorting: Reversing and sorting the list elements with methods asList(), sort(), reverseorder.
  4. List Unique Elements: Removing duplicate elements from list, sorting list elements, converting list to Set and again set to list.

In this fourth program, following operations are done on list elements.

  1. Removing duplicate elements from list
  2. Sorting list elements
  3. Converting List to Set
  4. Converting Set to List.
Example on List Remove Duplicate Elements Java

List Remove Duplicate Elements
Output screenshot on List Remove Duplicate Elements

List numList = new ArrayList();
numList.add(10); numList.add(20);
numList.add(10); numList.add(20);

List, being an interface, cannot be instantiated, but its reference can be assigned by assigning its subclass object. We know a subclass object can be assigned to a super class object implicitly. ArrayList is derived from List interface. For all practical purposes, numList can be treated as objects of List. Four elements are added 10 and 20 in duplicates. List accepts duplicates.

Set mySet = new HashSet();
System.out.println(“Set elements: ” + mySet);

To remove duplicates from List, there is no predefined method. To remove, we play a small tick here. We know, the Set interface subclasses like HashSet and TreeSet does not allow duplicate elements. This property is taken advantage and all the list elements are added to Set mySet with addAll() method. Now mySet automatically removes the duplicates of 10 and 20.

List numList1 = new ArrayList();
System.out.println(“List elements without duplicates: ” + numList1);

We have started with list and landed in set. Now, let us convert back to list. With the same addAll() method all the elements of mySet are converted into numList1. Now numList1 does not contain duplicates.

TreeSet ts = new TreeSet(mySet);
System.out.println(“\nSet elements in ascending order: ” + ts);

Now let us do one more operation of printing list elements in ascending order. For this, no predefined method exists. Here, we take the property of TreeSet. TreeSet sorts the elements in ascending order implicitly. To TreeSet constructor, set mySet is passed.

List numList2 = new ArrayList();

TreeSet elements are converted back to list numList2. Now, finally printing numList2 are printed in ascending order without duplicates.

1. How RadomAccessFile is different from other streams?
2. Can you retrieve a disk file metadata with Java program?

Leave a Reply

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