1. Remove duplicate elements in arraylist using LinkedHashSetThe LinkedHashSet is the best approach for removing duplicate elements in an arraylist. LinkedHashSet does two things internally : Show
Java example to remove duplicates in arraylist using LinkedHashSet. In given example, numbersList is an arraylist containing integers and some of them are duplicate numbers e.g. 1, 3 and 5. We add the list to LinkedHashSet, and then get back the content back into the list. The result arraylist does not have duplicate integers. Program Output. 1. IntroductionIn this quick tutorial, we're going to learn how to clean up the duplicate elements from a List. First, we'll use plain Java, then Guava, and finally, a Java 8 Lambda-based solution. This tutorial is part of the “Java – Back to Basic” series here on Baeldung. Java Program to Remove duplicate elements from ArrayListIn this example, we will learn to convert the duplicate element from the ArrayList in Java. To understand this example, you should have the knowledge of the following Java programming topics:
Java Program to Remove duplicates from ArrayListHere is our sample program to learn how to remove duplicates from ArrayList. The steps followed in the below example are:
You can further join Java Programming Masterclass for a Software Engineerscourse to learn more about the ArrayList class and different algorithms to remove duplicate objects. Please find below the complete code : import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; /** * Java Program to remove repeated elements from ArrayList in Java. * * @author WINDOWS 8 */ public class ArrayListDuplicateDemo{ public static void main(String args[]){ // creating ArrayList with duplicate elements List<Integer> primes = new ArrayList<Integer>(); primes.add(2); primes.add(3); primes.add(5); primes.add(7); //duplicate primes.add(7); primes.add(11); // let's print arraylist with duplicate System.out.println("list of prime numbers : " + primes); // Now let's remove duplicate element without affecting order // LinkedHashSet will guaranteed the order and since it's set // it will not allow us to insert duplicates. // repeated elements will automatically filtered. Set<Integer> primesWithoutDuplicates = new LinkedHashSet<Integer>(primes); // now let's clear the ArrayList so that we can // copy all elements from LinkedHashSet primes.clear(); // copying elements but without any duplicates primes.addAll(primesWithoutDuplicates); System.out.println("list of primes without duplicates : " + primes); } } Output list of prime numbers : [2, 3, 5, 7, 7, 11] list of primes without duplicates : [2, 3, 5, 7, 11] In this example, you can see we have created an ArrayList and added numbers into it, all prime numbers. We added '7' twice, so that it become duplicate. Now we print the ArrayList and you can see that it contains number 7 twice. After that we created a LinkedHashSet from our ArrayList, clear our original ArrayList and then added all elements from set to the list. This time we should not have any duplicates because Set doesn't allow them and they should have filtered when elements copied from ArrayList to HashSet by Java. This is proved by printing the ArrayList again, now it doesn't contain 7 twice, but only once. That's all about how to remove duplicates from ArrayList in Java. Though there are multiple ways to do this, I think using LinkedHashSet is the simplest one because its simple and also preserve the order of elements. Further Learning Java In-Depth: Become a Complete Java Engineer Java Fundamentals: Collections Data Structures and Algorithms: Deep Dive Using Java If you are interested in learning ArrayList, you should check out my following tutorials :
Thanks for reading this ArrayList tutorial. You have seen how easy it is to remove duplicate elements from ArrayList in Java, you just need to change the data structure. In fact, if you need unique elements to prefer a Set vs List but sometimes you also need benefits of ArrayList like fast search in constant time and that's where this technique can help you. 1. Stream distinct() method
package net.bench.resources.java8;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicatesFromArrayList {
public static void main(String[] args) {
// create test data
List<String> students = new ArrayList<String>();
// add values to list
students.add("Roger");
students.add("Rafael");
students.add("Djokovic");
students.add("Roger");
students.add("Murray");
students.add("Rafael");
// pretty print
System.out.println("1. Original list with duplicate values :\n");
students.forEach(student -> System.out.println(student));
// Java 8 - distinct() method
List<String> uniqueList = students
.stream() // get stream for original list
.distinct() // distinct method removes duplicates
.collect(Collectors.toList()); // distinct elements stored to new list
// pretty print
System.out.println("\n2. New list with unique values"
+ " maintaining original insertion order:\n");
uniqueList.forEach(uniqueStudent -> System.out.println(uniqueStudent));
// Java 8 - sorting
List<String> sortedList = students
.stream() // get stream for original list
.distinct() // distinct method removes duplicates
.sorted() // uses natural-ordering to sort
.collect(Collectors.toList()); // distinct elements stored to new list
// pretty print
System.out.println("\n3. New list with unique values"
+ " in natural sorting order :\n");
sortedList.forEach(sortedStudent -> System.out.println(sortedStudent));
}
}
Output: 1. Original list with duplicate values :
Roger
Rafael
Djokovic
Roger
Murray
Rafael
2. New list with unique values maintaining original insertion order:
Roger
Rafael
Djokovic
Murray
3. New list with unique values in natural sorting order :
Djokovic
Murray
Rafael
Roger
|