No duplicate arraylist java

Remove Duplicate Items from a List in Java

Learn to remove duplicate elements from a List in Java using Collection.removeIf(), LinkedHashSet and Stream APIs.

1. Using Collection.removeIf()

The removeIf() method removes all of the elements of this collection that satisfy a specified Predicate. Each matching element is removed using Iterator.remove(). If the collection’s iterator does not support removal, then an UnsupportedOperationException will be thrown on the first matching element.

In the following code, the predicate adds the current element to a HashSet. As HashSet does not allow duplicate items, the add() method returns false for them. All such duplicate items are removed from the List, and finally, the List contains only the unique items.

List items = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); Set set = new HashSet<>(items.size()); items.removeIf(p -> !set.add(p)); System.out.println(items); //[1, 2, 3, 4, 5, 6, 7, 8]

We can use the Java 8 Stream. distinct() method which returns a stream consisting of the distinct elements compared by object’s equals() method. Finally, collect all district elements as List using Collectors.toList().

ArrayList numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); List listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList()); System.out.println(listWithoutDuplicates); //[1, 2, 3, 4, 5, 6, 7, 8]

The LinkedHashSet is another good approach for removing duplicate elements in an ArrayList. LinkedHashSet does two things internally :

Читайте также:  Csharp list of lists

In the given example, items in the ArrayList contain integers; some 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.

ArrayList numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); LinkedHashSet hashSet = new LinkedHashSet<>(items); ArrayList listWithoutDuplicates = new ArrayList<>(hashSet); System.out.println(listWithoutDuplicates); //[1, 2, 3, 4, 5, 6, 7, 8]

Drop me your questions related to how to remove duplicate objects in arraylist in Java.

Источник

1. Stream distinct() method

3. Custom object – remove duplicates using Comparator

  • Student class defined with 4 attributes namely id, name, percentage, rank and constructor, getter/setters & toString() method
  • In the Student list, there are 5 Student objects and 1 duplicate Student with Id=2
  • To remove duplicate Student, we are converting original list into set which doesn’t allow duplicate by comparing Student’s Id attribute
  • Then we are printing again by comparing Student’s rank attribute in ascending order

Student.Java

package net.bench.resources.java8; // Student class class Student < // private member variables private int id; private String name; private double percentage; private int rank; // public 4-arg constructor // getters & setters // toString() method >

TestCustomObject.java

package net.bench.resources.java8; 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 TestCustomObject < public static void main(String[] args) < // create a list of Student objects ListstudentList = new ArrayList<>(); // add student object to List studentList.add(new Student(1,"Arun", 67.36, 2)); 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 // pretty print System.out.println("1. Original Student list with duplicates :\n"); studentList.forEach(student -> System.out.println(student)); // Java 8 - Collector.toCollection() Set uniqueStudentSet = studentList .stream() // get stream for original list .collect(Collectors.toCollection(//distinct elements stored into new SET () -> new TreeSet<>(Comparator.comparing(Student::getId))) ); //Id comparison // pretty print System.out.println("\n2. New SET with unique Student objects" + " in natural order of Id :\n"); uniqueStudentSet.forEach(uniqueStudent -> System.out.println(uniqueStudent)); // Java 8 - sorting in ascending order of Student's Rank List sortedList = uniqueStudentSet .stream() // get stream for unique SET .sorted(Comparator.comparing(Student::getRank)) // rank comparing .collect(Collectors.toList()); // elements stored to new list // pretty print System.out.println("\n3. Sorted according to ascending order" + " of Student's Rank :\n"); sortedList.forEach(sortedStudent -> System.out.println(sortedStudent)); > >

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 Student objects in natural order of Id : 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] 3. Sorted according to ascending order of Student's Rank : Student [id=2, name=Sethu, percentage=88.58, rank=1] Student [id=1, name=Arun, percentage=67.36, rank=2] Student [id=4, name=Vikcy, percentage=61.32, rank=3] Student [id=3, name=Ajith, percentage=55.74, rank=4]

4. Custom object – remove duplicate by overriding equals/hashCode

  • Student class defined with 4 attributes namely id, name, percentage, rank and constructor, getter/setters & toString(), equals(), hashCode() method
  • Here, we are overriding equals() & hashCode() methods based on Student’s Id attribute so as to prevent storing duplicate Student object in the Set
  • In the Student list, there are 6 Student objects and 2 duplicate Student with Id=2 & Id=3
  • To remove duplicate Student, we are using Stream‘s distinct() method and then collecting into Set which stores Student object in natural sorting order of Student’s Id attribute

Student.java

package net.bench.resources.java8; class Student < // private member variables private int id; private String name; private double percentage; private int rank; // public 4-arg constructor // getters & setters // toString() method // hashCode() method @Override public int hashCode() < final int prime = 31; int result = 1; result = prime * result + id; return result; >// equals() method @Override public boolean equals(Object obj) < if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id != other.id) return false; return true; >>

TestStudentObject.java

package net.bench.resources.java8; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class TestStudentObject < public static void main(String[] args) < // create a list of Student objects ListstudentList = new ArrayList<>(); // add student object to List studentList.add(new Student(1,"Arun", 67.36, 2)); 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 studentList.add(new Student(4,"Vikcy", 61.32, 3)); // duplicate Vicky // pretty print System.out.println("1. Original Student list with duplicates :\n"); studentList.forEach(student -> System.out.println(student)); // Java 8 - Collector.toCollection() Set uniqueStudentSet = studentList .stream() // get stream for original list .distinct() // removes duplicate .collect(Collectors.toSet()); // pretty print System.out.println("\n2. New SET with unique Student objects" + " in natural order of Id :\n"); uniqueStudentSet.forEach(uniqueStudent -> System.out.println(uniqueStudent)); > >

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] Student [id=4, name=Vikcy, percentage=61.32, rank=3] 2. New SET with unique Student objects in natural order of Id : 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 !!

Источник

Remove Duplicates from ArrayList in Java

In this tutorial, we will learn how to remove duplicate elements from an ArrayList in Java.

Let’s know something about ArrayList in Java.

ArrayList is defined as a dynamic array in Java that is present in the Collection Framework of java.util package. It works quite a bit more flexibly than arrays but it is slower than arrays. It is very helpful when manipulation of data in the array is needed.

Approaches to remove Duplicates from ArrayList in Java

There are many methods to remove the duplicate element in the ArrayList in Java. In this article, we will learn three of those methods.

Method 1: Using only ArrayList

The first method is a simple approach to removing the duplicate elements.

1: Create an ArrayList arr with duplicate elements.

2: Create an empty ArrayList new_arr of the same type as the previous one.

3: Traverse the ArrayList arr and check whether the ArrayList new_arr has the same element in it or not. If the condition is false i.e, the indicated element is new then add it to the new_arr or else continue.

4: Print the new_arr ArrayList and you will see that no duplicate element is present.

import java.io.*; import java.util.*; class arrayListDuplication < public static void main(String[] args)throws IOException< ArrayListarr=new ArrayList(Arrays.asList(1,2,3,3,4,1,1,4,5,5,6,7,2,3,5,7,6,6,6)); //Create a new ArrayList ArrayList new_arr=new ArrayList(); for(int i=0;i //Original Array System.out.println("Original Array -> "+ arr); //New Array System.out.println("\nNew Array with No duplicate Elements -> "+new_arr+"\n"); > >
Original Array -> [1, 2, 3, 3, 4, 1, 1, 4, 5, 5, 6, 7, 2, 3, 5, 7, 6, 6, 6] New Array with no Duplicate elements -> [1 ,2 ,3 ,4 ,5 ,6 ,7]

Method 2: Using LinkedHashset

In this method, we will use linkedHashset. But why do we need to use this because Set allows only unique element inside itself and no duplicates is added in it.

1: Create an ArrayList arr with duplicate elements.

2: Create an empty linked HashSet new_arr of the same type as the previous one.

3: use the addall() function to add all the elements in the ArrayList arr to Set new_arr. According to the Property LinkedHashSet creates a unique set in it.

Step 4: Clear the ArrayList arr and add the element of the Set into the arrayList.

Step 5: Print the ArrayList and you will see that the elements are all unique.

import java.io.*; import java.util.*; class arrayListDuplication < public static void main(String[] args)throws IOException< ArrayListarr=new ArrayList(Arrays.asList(1,2,3,3,4,1,1,4,5,5,6,7,2,3,5,7,6,6,6)); //Original Array System.out.println("Original Array -> "+ arr); //Create a new ArrayList Set new_arr=new LinkedHashSet<>(); new_arr.addAll(arr); arr.clear(); arr.addAll(new_arr); //New Array System.out.println("\nNew Array with No duplicate Elements -> "+new_arr+"\n"); > >
Original Array -> [1, 2, 3, 3, 4, 1, 1, 4, 5, 5, 6, 7, 2, 3, 5, 7, 6, 6, 6] New Array with no Duplicate elements -> [1 ,2 ,3 ,4 ,5 ,6 ,7]

Method 3: Using HashMap

The last method we will be talking about is of using HashMap. By using the hashmap will create a key-value pair and counter the duplicate elements and then print only the keys.

1: Create an ArrayList arr with duplicate elements.

2: Create an empty Hashmap map (key value pair).

3: Traverse the ArrayList and add the element in the hashmap. So if the element is new then create a new key or else counter the value of the existing key.

4: Traverse the HashMap and print the key from the HashMap.

import java.io.*; import java.util.*; import java.util.Map.Entry; class arrayListDuplication < public static void main(String[] args)throws IOException< ArrayListarr=new ArrayList(Arrays.asList(1,2,3,3,4,1,1,4,5,5,6,7,2,3,5,7,6,6,6)); //Original Array System.out.println("Original Array -> "+ arr); //Create a new ArrayList HashMap map=new HashMap(); for(int i=0;i else < int d=map.get(arr.get(i)); d++; map.put(arr.get(i),d); >> System.out.print("\nNew Array with no Duplicate elements -> "); for (Entry e : map.entrySet()) < System.out.print(e.getKey()+" "); >System.out.println("\n"); > >
Original Array -> [1, 2, 3, 3, 4, 1, 1, 4, 5, 5, 6, 7, 2, 3, 5, 7, 6, 6, 6] New Array with no Duplicate elements -> 1 2 3 4 5 6 7

So these were the 3 methods by which we can remove duplicate elements from the arrayList. Hope you like my explaination.

Источник

Оцените статью