Java 8 how to remove duplicates from list - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorials, I am going to show you how to remove duplicate elements from the list using Java 8 syntax.
Here I am creating a Department class, it holds department details.
Department.java
public class Department {
    private int deptId;
    private String deptName;

    public Department(int deptId, String deptName) {
        super();
        this.deptId = deptId;
        this.deptName = deptName;
    }

    public int getDeptId() {
        return deptId;
    }

    public void setDeptId(int deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

}
We can remove the duplicate elements from a list in following 2 different ways.
Preparing a list containing duplicate Department objects, and remove those duplicate departments using Java8 stream.
Here to remove the duplicate elements, by assigning a list to TreeSet. As TreeSet doesn’t allow the duplicate elements, it removes.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class RemoveDuplicates {
    public static void main(String[] args) {
        List deptList = new ArrayList();
        deptList.add(new Department(1, "IT"));
        deptList.add(new Department(2, "HR"));
        deptList.add(new Department(1, "IT"));
        deptList.add(new Department(4, "Development"));
        deptList.add(new Department(2, "HR"));

        // Removing the Elements by assigning list to TreeSet
        Set deptSet = deptList.stream()
                .collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Department::getDeptName))));
        deptSet.forEach(dept -> System.out.println("DeptId (" + dept.getDeptId() + ") Name :" + dept.getDeptName()));

    }
}
Output :
DeptId (4) Name :Development
DeptId (2) Name :HR
DeptId (1) Name :IT
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RemoveDuplicates {
    public static void main(String[] args) {
        List deptList = new ArrayList();
        deptList.add(new Department(1, "IT"));
        deptList.add(new Department(2, "HR"));
        deptList.add(new Department(1, "IT"));
        deptList.add(new Department(4, "Development"));
        deptList.add(new Department(2, "HR"));

        Set deptSet = new HashSet<>();

        // directly removing the elements from list if already existed in set
        deptList.removeIf(p -> !deptSet.add(p.getDeptName()));

        deptList.forEach(dept->System.out.println(dept.getDeptId() +" : "+dept.getDeptName()));

    }
}
Output :
1 : IT
2 : HR
4 : Development
Happy Learning 🙂

Resolve NullPointerException in Collectors.toMap
Spring Collection Dependency List Example
How to Merge Streams in Java 8
How to Filter null values from Java8 Stream
@Qualifier annotation example in Spring
How to Rotate Elements in List
How to calculate Employees Salaries Java 8 summingInt
How to get Stream count in Java 8
Difference between HashSet vs TreeSet in Java
Spring Collection Map Dependency Example
TreeSet with Customizable Sorting in Java
Java how to convert ArrayList to Array Example
Python – How to remove duplicate elements from List
Java 8 How to convert Stream to List
How to convert List to Map in Java 8

Resolve NullPointerException in Collectors.toMap
Spring Collection Dependency List Example
How to Merge Streams in Java 8
How to Filter null values from Java8 Stream
@Qualifier annotation example in Spring
How to Rotate Elements in List
How to calculate Employees Salaries Java 8 summingInt
How to get Stream count in Java 8
Difference between HashSet vs TreeSet in Java
Spring Collection Map Dependency Example
TreeSet with Customizable Sorting in Java
Java how to convert ArrayList to Array Example
Python – How to remove duplicate elements from List
Java 8 How to convert Stream to List
How to convert List to Map in Java 8
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String