// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Set the predicate or the condition for filtering the elements.
Predicate<Integer> moreThan2Pred = (p) -> (p > 1);
// Use Filter to refine the element set, sort to Sort and Collectors.toSet to get a set out of Stream.
intSet = intSet.stream().filter(moreThan2Pred).sorted().collect(Collectors.toSet());
System.out.println(intSet); // Prints [2, 3, 4]
|