Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a program to remove duplicates from a List.
- Removing duplicate primitive and String values from List is a bit easy. We can use “Set” collection to remove the duplicates(if the insertion order is not important in the List).
Check out the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Create a List of Integers. List<Integer> ints = new ArrayList<>(); ints.add(10); ints.add(6); ints.add(8); ints.add(6); System.out.println("Before removing duplicates:"); System.out.println(ints); //Let's remove duplicates by adding the list to a HashSet Set<Integer> intSet = new HashSet<>(ints); //Now add the unique elements from Set to List ints.clear(); ints.addAll(intSet); System.out.println(); Sycstem.out.println("After removing duplicates:");System.out.println(ints); |
- If we need to remove duplicates from a List object where it contains User Defined Objects, the User Defined Class should override “hashCode()” and “equals()” methods of Object class.
The Set collection uses Hashtable as an underlying data structure. So if two objects return same hashCode() and equals() method returns true for them, the object will not be added to the Set collection.
Let’s create a Person class and override the hashCode() and equals() methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package com.clearurdoubt; public class Person { private String firstname; private String lastname; private int id; public Person(String firstname, String lastname, int id) { this.firstname = firstname; this.lastname = lastname; this.id = id; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "[" + firstname + ", " + lastname + ", " + id + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((firstname == null) ? 0 : firstname.hashCode()); result = prime * result + id; result = prime * result + ((lastname == null) ? 0 : lastname.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (firstname == null) { if (other.firstname != null) return false; } else if (!firstname.equals(other.firstname)) return false; if (id != other.id) return false; if (lastname == null) { if (other.lastname != null) return false; } else if (!lastname.equals(other.lastname)) return false; return true; } } |
Now let’s look at the program to remove duplicates:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.clearurdoubt; 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) { // Create a List of Integers. List<Integer> ints = new ArrayList<>(); ints.add(10); ints.add(6); ints.add(8); ints.add(6); System.out.println("Before removing duplicates:"); System.out.println(ints); //Let's remove duplicates by adding the list to a HashSet Set<Integer> intSet = new HashSet<>(ints); //Now add the unique elements from Set to List ints.clear(); ints.addAll(intSet); System.out.println(); System.out.println("After removing duplicates:"); System.out.println(ints); System.out.println(); System.out.println(); //============================================================================================= // Let's perform duplicate removal for Custom objects //============================================================================================= //Let's create a List of Persons List<Person> persons = new ArrayList<>(); persons.add(new Person("Andy", "Ray", 105)); persons.add(new Person("Andy", "Jason", 101)); persons.add(new Person("John", "Arthur", 102)); persons.add(new Person("Andy", "Jason", 101)); System.out.println("Before removing duplicates:"); System.out.println(persons); //Let's remove duplicates by adding the list to a HashSet Set<Person> personSet = new HashSet<>(persons); //Now add the unique elements from Set to List persons.clear(); persons.addAll(personSet); System.out.println(); System.out.println("After removing duplicates:"); System.out.println(persons); } } |
Output:
Happy learning :).
Please leave a reply in case of any queries.
Hi,
Your way of explanation is very good.Please plan for core java end to end sessions.
Thanks