📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Remove Duplicates from a List Using Plain Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* Remove Duplicates from a List Using Java
* @author Ramesh Fadatare
*
*/
public class FindDuplicatesInList {
public static void main(String[] args) {
List <Integer> listWithDuplicates = Arrays.asList(0, 1, 2, 3, 0, 0);
List <Integer> listWithoutDuplicates = new ArrayList <> (
new HashSet <> (listWithDuplicates));
listWithoutDuplicates.forEach(element -> System.out.println(element));
}
}
0
1
2
3
Remove Duplicates from a List Using Java 8 Lambdas
package net.javaguides.jackson;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Remove Duplicates from a List Using Java
* @author Ramesh Fadatare
*
*/
public class FindDuplicatesInList {
public static void main(String[] args) {
List <Integer> listWithDuplicates = Arrays.asList(0, 1, 2, 3, 0, 0);
List <Integer> listWithoutDuplicates = listWithDuplicates.stream()
.distinct()
.collect(Collectors.toList());
listWithoutDuplicates.forEach(element -> System.out.println(element));
}
}
0
1
2
3
Title misleading - does not actually find the duplicates
ReplyDeleteI don't see any issues. Can you provide more details to identify the issue?
Delete