To remove duplicates from a List in Java, there are several approaches you can take depending on your requirements. Here are three common approaches:
Using a Set: One approach is to use a Set to store the unique elements from the List. A Set does not allow duplicates, so adding the elements of the List to a Set will automatically remove any duplicates. You can then create a new List from the Set to preserve the order of the original List.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Example {
public static void main(String[] args) {
List<String> listWithDuplicates = new ArrayList<>();
listWithDuplicates.add("a");
listWithDuplicates.add("b");
listWithDuplicates.add("c");
listWithDuplicates.add("a");
listWithDuplicates.add("b");
Set<String> setWithoutDuplicates = new HashSet<>(listWithDuplicates);
List<String> listWithoutDuplicates = new ArrayList<>(setWithoutDuplicates);
System.out.println("List with duplicates: " + listWithDuplicates);
System.out.println("List without duplicates: " + listWithoutDuplicates);
}
}
In this example, we create a List with some duplicate elements and then use a HashSet to remove the duplicates. We then create a new List from the HashSet to preserve the order of the original List.
Using Java 8 Stream API: Another approach is to use Java 8 Stream API’s distinct() method to remove duplicates from the List.
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> listWithDuplicates = new ArrayList<>();
listWithDuplicates.add("a");
listWithDuplicates.add("b");
listWithDuplicates.add("c");
listWithDuplicates.add("a");
listWithDuplicates.add("b");
List<String> listWithoutDuplicates = listWithDuplicates.stream()
.distinct()
.collect(Collectors.toList());
System.out.println("List with duplicates: " + listWithDuplicates);
System.out.println("List without duplicates: " + listWithoutDuplicates);
}
}
In this example, we create a List with some duplicate elements and then use the distinct() method to remove duplicates from the List. We then collect the distinct elements into a new List.
Using a for loop: A third approach is to use a for loop to iterate over the List and remove any duplicates.
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<String> listWithDuplicates = new ArrayList<>();
listWithDuplicates.add("a");
listWithDuplicates.add("b");
listWithDuplicates.add("c");
listWithDuplicates.add("a");
listWithDuplicates.add("b");
List<String> listWithoutDuplicates = new ArrayList<>();
for (String element : listWithDuplicates) {
if (!listWithoutDuplicates.contains(element)) {
listWithoutDuplicates.add(element);
}
}
System.out.println("List with duplicates: " + listWithDuplicates);
System.out.println("List without duplicates: " + listWithoutDuplicates);
}
}
In this example, we create a List with some duplicate elements and then use a for loop to iterate over the List and add each unique element to a new List.
In summary, there are several approaches to remove duplicates