In this post we will see various approaches in deleting duplicate elements from Array List
Input [Jay, Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar, Jay, sridhar] Output Jay, TEJA, SANDEEP, RAMU, VINODD, bhara
Using Distinct in Java 8 – Student Object must implement hashcode and equals method to use collections.
you can access Student.java file below
public class RemoveDuplicates { public static void main(String[] args) { List<Student> sList = getData(); List<Student> newList = sList.stream().distinct().collect(Collectors.toList()); List<String> withDuplicates = sList.stream().map(Student::getName).collect(Collectors.toList()); List<String> withOutDuplicates = newList.stream().map(Student::getName).collect(Collectors.toList()); System.out.println("With Duplicates ->"+withDuplicates); System.out.println("With Out Duplicates ->"+withOutDuplicates); } public static List<Student> getData(){ List<Student> list = new ArrayList<Student>(); Student s1 = new Student("Jay", 30, "TEST"); Student s2 = new Student("Jay", 25, "CSE"); Student s3 = new Student("TEJA", 35, "ECE"); Student s4 = new Student("SANDEEP", 40, "CSE"); Student s5 = new Student("RAMU", 30, "CSE"); Student s6 = new Student("VINODD", 28, "CSE"); Student s7 = new Student("bharath", 27, "ECE"); Student s8 = new Student("Ranga", 30, "MECH"); Student s9 = new Student("RAJA", 31, "MECH"); Student s10 = new Student("Konda", 35, "EEE"); Student s11 = new Student("Pavan", 32, "EEE"); Student s12 = new Student("King", 31, "CSE"); Student s13 = new Student("sridhar", 20, "EEE"); Student s14 = new Student("Jay", 30, "ECE"); Student s15 = new Student("sridhar", 20, "EEE"); list.add(s1);list.add(s2);ist.add(s3);list.add(s4);list.add(s5);list.add(s6); list.add(s7);list.add(s8); list.add(s9);list.add(s10);list.add(s11);list.add(s12);list.add(s13);list.add(s14);ist.add(s15); return list; } } Output: With Duplicates ->[Jay, Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar, Jay, sridhar] With Out Duplicates ->[Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar]
- Using Regular HashSet :
- if you observe the output, the insertion order is not maintained with HashSet . so we will use LinkedHashSet to maintain the insertion order.
List<Student> sList = getData(); //you can see this method above Set<Student> hashSet = new HashSet<Student>(); hashSet.addAll(sList); List<String> withDuplicates = sList.stream().map(Student::getName).collect(Collectors.toList()); List<String> withOutDuplicates = hashSet.stream().map(Student::getName).collect(Collectors.toList()); System.out.println("With Duplicates ->"+withDuplicates); System.out.println("With Out Duplicates ->"+withOutDuplicates); Input->[Jay, Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar, Jay, sridhar] Output->[Jay, TEJA, SANDEEP, RAMU, RAJA, Konda, Pavan, King, sridhar, VINODD, Ranga, bharath]
- Using LinkedHashSet
List<Student> sList = getData(); LinkedHashSet<Student> linkedSet = new LinkedHashSet<>(); linkedSet.addAll(sList); List<String> withDuplicates = sList.stream().map(Student::getName).collect(Collectors.toList()); List<String> withOutDuplicates = linkedSet.stream().map(Student::getName).collect(Collectors.toList()); System.out.println("With Duplicates ->"+withDuplicates); System.out.println("With Out Duplicates ->"+withOutDuplicates); Output: With Duplicates ->[Jay, Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar, Jay, sridhar] With Out Duplicates ->[Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar]
- Removing duplicate elements with out Set
List<Student> sList = getData(); List<Student> aList = new ArrayList<Student>(); sList.forEach(st->{ if(!aList.contains(st)) { aList.add(st); } }); List<String> withDuplicates = sList.stream().map(Student::getName).collect(Collectors.toList()); List<String> withOutDuplicates = aList.stream().map(Student::getName).collect(Collectors.toList()); System.out.println("With Duplicates ->"+withDuplicates); System.out.println("With Out Duplicates ->"+withOutDuplicates); Output: With Duplicates ->[Jay, Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar, Jay, sridhar] With Out Duplicates ->[Jay, TEJA, SANDEEP, RAMU, VINODD, bharath, Ranga, RAJA, Konda, Pavan, King, sridhar]
Student.java
public class Student { private String name; private int age; private String department; public Student(String name, int age, String department) { this.name = name; this.age = age; this.department = department; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public int hashCode() { final int prime = 31; int result = 1 result = prime * result + ((name == null) ? 0 : name.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; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
Thank you for reading tutorial.