Java 8 – How to remove duplicates from LinkedList

In this article, we will discuss how to remove duplicate element/objects from LinkedList with examples using different approaches

Also read How to remove duplicates from ArrayList

1. Java 8 Stream – distinct() method

  • Stream’s distinct() method returns a stream consisting of the distinct elements according to Object.equals(Object) of this stream
  • For ordered streams, the selection of distinct elements is stable
  • For duplicated elements, the element appearing first in the encounter order is preserved
  • For unordered streams, no stability guarantees are made
  • This is stateful intermediate operation which means it is interfering with other elements in the stream to remove duplicates
  • Method signature :- Stream distinct()

1.1 Remove duplicate elements from LinkedList

package net.bench.resources.stream.distinct.linkedlist;

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

public class RemoveDuplicatesFromLinkedList {

	public static void main(String[] args) {

		// 1. LinkedList object
		List<String> studentList = new LinkedList<String>();

		// 1.1 add String items to LinkedList
		studentList.add("Tendulkar");
		studentList.add("Kohli");
		studentList.add("Ganguly");
		studentList.add("Kohli");
		studentList.add("Tendulkar");
		studentList.add("Kohli");
		studentList.add("Ganguly");


		// 1.2 pretty print
		System.out.println("1. Original LinkedList with duplicate values :- \n");
		studentList.forEach(student -> System.out.println(student));


		// 2. Java 8 - distinct() method
		List<String> uniqueList = studentList
				.stream() // get sequential stream
				.distinct() // distinct method
				.collect(Collectors.toList()); // collected to new unique list


		// 2.1 pretty print to console
		System.out.println("\n2. New list with unique values"
				+ " maintaining original insertion order :- \n");
		uniqueList.forEach(uniqueStudent -> System.out.println(uniqueStudent));
	}
}

Output:

1. Original LinkedList with duplicate values :- 

Tendulkar
Kohli
Ganguly
Kohli
Tendulkar
Kohli
Ganguly

2. New list with unique values maintaining original insertion order :- 

Tendulkar
Kohli
Ganguly

2. Using Set approach

  • Set allows only unique items to be stored/collected
  • So, when we try to insert/add/store duplicate items, old element get replaced with new element
  • add() method of Set interface helps to store/insert items to set and returns true/false based on whether item is new or duplicate

2.1 Remove duplicates from LinkedList

  • A LinkedList contains 7 String elements with duplicates
  • First, get stream from original LinkedList
  • And using this stream convert it into Set using Stream’s collect() method passing Collectors.toSet() as argument
  • Finally, when we iterate/print newly created Set of String elements using Stream’s forEach() method, we will get only unique elements in random order, as Set doesn’t maintain any order
package net.bench.resources.stream.distinct.linkedlist;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class RemoveDuplicatesUsingSetApproach {

	public static void main(String[] args) {

		// 1. LinkedList object
		List<String> studentList = new LinkedList<String>();

		// 1.1 add String items to LinkedList
		studentList.add("Tendulkar");
		studentList.add("Kohli");
		studentList.add("Ganguly");
		studentList.add("Kohli");
		studentList.add("Tendulkar");
		studentList.add("Kohli");
		studentList.add("Ganguly");


		// 1.2 pretty print
		System.out.println("1. Original LinkedList with duplicate values :- \n");
		studentList.forEach(student -> System.out.println(student));


		// 2. Java 8 - Collector.toSet()
		Set<String> uniqueSet = studentList
				.stream() // get sequential stream
				.collect(Collectors.toSet()); // collect distinct elements


		// 2.1 pretty print to console
		System.out.println("\n2. New SET with unique values"
				+ " which doesn't maintains order:- \n");
		uniqueSet.forEach(uniqueStudent -> System.out.println(uniqueStudent));
	}
}

Output:

1. Original LinkedList with duplicate values :- 

Tendulkar
Kohli
Ganguly
Kohli
Tendulkar
Kohli
Ganguly

2. New SET with unique values which doesn't maintains order:- 

Ganguly
Kohli
Tendulkar

3. Java 8 Collectors – toCollection() method

  • We can pass any Collection classes like ArrayList, LinkedList, HashSet, LinkedHashSet, TreeSet or PriorityQueue as argument to Collectors.toCollection() method which after collecting elements/objects converts to that particular implementation class
  • Like in the below example, we are converting it into TreeSet as it removes duplicates and also stores according to natural order of integer numbers
  • A Student class defined with 4 attributes namely id, name, percentage, rank

Student.java

package net.bench.resources.stream.distinct.linkedlist;

class Student {

	// member variables
	private int id;
	private String name;
	private double percentage;
	private int rank;

	// 4-arg parameterized constructor

	// getters & setters

	// toString() method
}

3.1 Remove duplicate Student

  • In Student list, there are 5 Student objects defined with one being duplicate for Student with Id=1
  • To remove duplicate Student, we are converting original LinkedList into TreeSet which doesn’t allow duplicate by comparing Student’s Id attribute

RemoveDuplicateStudentFromLinkedList.java

package net.bench.resources.stream.distinct.linkedlist;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class RemoveDuplicateStudentFromLinkedList {

	public static void main(String[] args) {

		// 1. create LinkedList of Student objects
		List<Student> studentList = new LinkedList<>();

		// 1.1 add student object to List
		studentList.add(new Student(1,"Arun", 67.36, 2)); // duplicate Arun
		studentList.add(new Student(2,"Sethu", 88.58, 1));
		studentList.add(new Student(3,"Ajith", 55.74, 4));
		studentList.add(new Student(4,"Vikcy", 61.32, 3));
		studentList.add(new Student(1,"Arun", 67.36, 2)); // duplicate Arun


		// 1.2 pretty print
		System.out.println("1. Original Student list with duplicates :\n");
		studentList.forEach(student -> System.out.println(student));


		// 2. Java 8 - Collector.toCollection()
		Set<Student> uniqueStudentSet = studentList
				.stream() // get sequential stream
				.collect(Collectors.toCollection(
						() -> new TreeSet<>(Comparator.comparing(Student::getId)))
						); //Id comparison


		// 2.1 pretty print to console
		System.out.println("\n2. New SET with unique Students :\n");
		uniqueStudentSet.forEach(System.out::println);
	}
}

Output:

1. Original Student list with duplicates :

Student [id=1, name=Arun, percentage=67.36, rank=2]
Student [id=2, name=Sethu, percentage=88.58, rank=1]
Student [id=3, name=Ajith, percentage=55.74, rank=4]
Student [id=4, name=Vikcy, percentage=61.32, rank=3]
Student [id=1, name=Arun, percentage=67.36, rank=2]

2. New SET with unique Students :

Student [id=1, name=Arun, percentage=67.36, rank=2]
Student [id=2, name=Sethu, percentage=88.58, rank=1]
Student [id=3, name=Ajith, percentage=55.74, rank=4]
Student [id=4, name=Vikcy, percentage=61.32, rank=3]

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to sort ArrayList using Stream
Java 8 - Stream flatMapToDouble() method