Situation:
You have a list with duplicate names and need to remove those duplicates.
Solution:
List<String> names = Arrays.asList("Alice", "Bob", "Alice", "Charlie");
List<String> distinctNames = names.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(distinctNames);
Explanation:
The distinct
method removes duplicate elements from the stream before collecting them into a list.
Result:
[Alice, Bob, Charlie]
Duplicates removed! Your list is now distinct and clean. Code on!
Leave a Reply