Programming





How to Remove Duplicate elements from a List in Java

How to Remove Duplicate elements from a List in Java


by Administrator | 10-Aug-2020
Java

Tags:  Collections  Program

Share: 


There are few ways in which we can remove duplicate elements from a List. Let’s take a look:

  • HashSet: If the insertion order is not important then HashSet can be used

Program

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

/**
 * @author prgrmmng
 *
 */
public class ArrayListDuplicates {
                public static void main(String[] args) {
                                // creating a new list
                                List<String> listOfDuplicates = new ArrayList<>(); 

                                // Adding elements with duplicates
                                listOfDuplicates.add("John");
                                listOfDuplicates.add("Joel");
                                listOfDuplicates.add("Emma");
                                listOfDuplicates.add("Emma");
                                listOfDuplicates.add("Joel");
                                listOfDuplicates.add("Elle"); 

                                System.out.println("Before removing duplicates: " + listOfDuplicates);

                                // passing the list to HashSet which takes collection elements in constructor
                                Set<String> hashSet = new HashSet<>(listOfDuplicates);

                                // setting the set back to the list
                                List<String> listWithoutDuplicates = new ArrayList<>(hashSet);

                                System.out.println("After removing duplicates: " + listWithoutDuplicates);
                }
}

Output

Before removing duplicates: [John, Joel, Emma, Emma, Joel, Elle]
After removing duplicates: [Elle, John, Emma, Joel]
  • LinkedHashSet: If the requirement is to maintain the insertion order then LinkedHashSet can be used.

Program

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
 * @author prgrmmng
 *
 */
public class ArrayListDuplicates {
                public static void main(String[] args) {
                                // creating a new list
                                List<String> listOfDuplicates = new ArrayList<>(); 

                                // Adding elements with duplicates
                                listOfDuplicates.add("John");
                                listOfDuplicates.add("Joel");
                                listOfDuplicates.add("Emma");
                                listOfDuplicates.add("Emma");
                                listOfDuplicates.add("Joel");
                                listOfDuplicates.add("Elle"); 

                                System.out.println("Before removing duplicates: " + listOfDuplicates); 

                                // passing the list to HashSet which takes collection elements in constructor
                                Set<String> hashSet = new LinkedHashSet<>(listOfDuplicates); 

                                // setting the set back to the list
                                List<String> listWithoutDuplicates = new ArrayList<>(hashSet); 

                                System.out.println("After removing duplicates: " + listWithoutDuplicates);
                }
}

Output

Before removing duplicates: [John, Joel, Emma, Emma, Joel, Elle]
After removing duplicates: [John, Joel, Emma, Elle]
  • Lambda Expression: Post java 8 lambdas can be used too. It maintains the insertion order

Program

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author prgrmmng
 *
 */
public class ArrayListDuplicates {
        public static void main(String[] args) {
                        // creating a new list
                        List<String> listOfDuplicates = new ArrayList<>(); 

                        // Adding elements with duplicates
                        listOfDuplicates.add("John");
                        listOfDuplicates.add("Joel");
                        listOfDuplicates.add("Emma");
                        listOfDuplicates.add("Emma");
                        listOfDuplicates.add("Joel");
                        listOfDuplicates.add("Elle");

                        System.out.println("Before removing duplicates: " + listOfDuplicates);

                         // using streams to iterate and distinct() method from Stream class
                        List<String> listWithoutDuplicates = listOfDuplicates.stream().distinct().collect(Collectors.toList());
 
                        System.out.println("After removing duplicates: " + listWithoutDuplicates);
        }
}

Output

Before removing duplicates: [John, Joel, Emma, Emma, Joel, Elle]
After removing duplicates: [John, Joel, Emma, Elle]

Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.