Monday , December 11 2017
Home / java8 / Java 8 how to remove duplicates from list

Java 8 how to remove duplicates from list

In this tutorials, I am going to show you how to remove duplicate elements from list using Java 8 syntax.

How to remove duplicates from list :

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;
    }

}

Here I am going to prepare a list with duplicate department names and remove those duplicate departments.

Removing Duplicates by assigning List to TreeSet :


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<Department> deptList = new ArrayList<Department>();
        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<Department> deptSet = deptList.stream()
                .collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Department::getDeptName))));
        deptSet.forEach(dept -> System.out.println("DeptId (" + dept.getDeptId() + ") Name :" + dept.getDeptName()));

    }
}

DeptId (4) Name :Development
DeptId (2) Name :HR
DeptId (1) Name :IT

Removing Duplicates using removeIf():


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<Department> deptList = new ArrayList<Department>();
        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<String> 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()));

    }
}

1 : IT
2 : HR
4 : Development

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Java 8 walk How to Read all files in a folder

In this tutorial, I am going to show you how to read all files in …

Leave a Reply

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