We can use stream with filters and collections:-
a) Stream provide link with a collection.
b) Filter can filter the data from linked filter
c) Collector can collect the data based on used filter.
Here is the simple demonstration:-
package com.shadab.collections.stream;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class StreamDemo {
public static void main(String[] args) {
List<Integer> listIntegers = Arrays.asList(8, 9, 10, 7, 4,3,2,1,5,6,7,9);
System.out.println("All numbers are:- "+listIntegers);
Set<Integer> uniqueOddNumbers = listIntegers.stream().filter(number -> number % 2 != 0).collect(Collectors.toSet());
System.out.println("Unique odd numbers are:- "+uniqueOddNumbers);
}
}
If you get surprised by seeing number -> number in java code please click here.
Output:-
All numbers are:- [8, 9, 10, 7, 4, 3, 2, 1, 5, 6, 7, 9]
Unique odd numbers are:- [1, 3, 5, 7, 9]
Comments
Post a Comment