Programming |
There are few ways in which we can remove duplicate elements from a List. Let’s take a look:
Program
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author prgrmmng
*
*/
public class ArrayListDuplicates {
public static void main(String[] args) {
// creating a new list
List<String> listOfDuplicates = new ArrayList<>();
// Adding elements with duplicates
listOfDuplicates.add("John");
listOfDuplicates.add("Joel");
listOfDuplicates.add("Emma");
listOfDuplicates.add("Emma");
listOfDuplicates.add("Joel");
listOfDuplicates.add("Elle");
System.out.println("Before removing duplicates: " + listOfDuplicates);
// passing the list to HashSet which takes collection elements in constructor
Set<String> hashSet = new HashSet<>(listOfDuplicates);
// setting the set back to the list
List<String> listWithoutDuplicates = new ArrayList<>(hashSet);
System.out.println("After removing duplicates: " + listWithoutDuplicates);
}
}
Output
Before removing duplicates: [John, Joel, Emma, Emma, Joel, Elle]
After removing duplicates: [Elle, John, Emma, Joel]
Program
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* @author prgrmmng
*
*/
public class ArrayListDuplicates {
public static void main(String[] args) {
// creating a new list
List<String> listOfDuplicates = new ArrayList<>();
// Adding elements with duplicates
listOfDuplicates.add("John");
listOfDuplicates.add("Joel");
listOfDuplicates.add("Emma");
listOfDuplicates.add("Emma");
listOfDuplicates.add("Joel");
listOfDuplicates.add("Elle");
System.out.println("Before removing duplicates: " + listOfDuplicates);
// passing the list to HashSet which takes collection elements in constructor
Set<String> hashSet = new LinkedHashSet<>(listOfDuplicates);
// setting the set back to the list
List<String> listWithoutDuplicates = new ArrayList<>(hashSet);
System.out.println("After removing duplicates: " + listWithoutDuplicates);
}
}
Output
Before removing duplicates: [John, Joel, Emma, Emma, Joel, Elle]
After removing duplicates: [John, Joel, Emma, Elle]
Program
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author prgrmmng
*
*/
public class ArrayListDuplicates {
public static void main(String[] args) {
// creating a new list
List<String> listOfDuplicates = new ArrayList<>();
// Adding elements with duplicates
listOfDuplicates.add("John");
listOfDuplicates.add("Joel");
listOfDuplicates.add("Emma");
listOfDuplicates.add("Emma");
listOfDuplicates.add("Joel");
listOfDuplicates.add("Elle");
System.out.println("Before removing duplicates: " + listOfDuplicates);
// using streams to iterate and distinct() method from Stream class
List<String> listWithoutDuplicates = listOfDuplicates.stream().distinct().collect(Collectors.toList());
System.out.println("After removing duplicates: " + listWithoutDuplicates);
}
}
Output
Before removing duplicates: [John, Joel, Emma, Emma, Joel, Elle]
After removing duplicates: [John, Joel, Emma, Elle]