Listnumbers = Arrays.asList(1, 2, 3, 4, 5, 6); Set evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toSet()); System.out.println(evenNumbers); // Output: [2, 4, 6]
Liststrings = Arrays.asList("apple", "banana", "orange", "apple", "pear"); Set uniqueStrings = strings.stream() .distinct() .collect(Collectors.toSet()); System.out.println(uniqueStrings); // Output: [apple, banana, orange, pear]
import java.util.stream.Collectors;