When ever we operate on data we will have basic CURD operation. Let's see how we are doing CURD in streams:
1) Create a stream:
List<Integer> lstInt = Arrays.asList(1,2,3,4,5,6,7,8);
lstInt.stream().forEach(System.out::println);
Stream has 2 steps - Configuring and Processing
In configuring phase, we would apply non terminal operations - filters and mappings
In processing phase, operation applied and result is produced
List<Integer> lstInt = Arrays.asList(1,2,3,4,5,6,7,8);
lstInt.stream().filter( item -> item > 5 ).forEach(System.out::println);
filter() - non terminal operations
forEach() - terminal operations
2) Update a stream: Updation is not removing actual elements from stream but filtered and givin what is expected. Once terminal operation is specified we can't use them again
3) Read a stream: All terminal operation which prints out elements are read operation - forEach(), collect()
4) Delete stream:
removeIf uses an Iterator to iterate over the list and match the elements using the predicate.
removeAll uses a simple for-loop to remove all the matching elements in the list.
Non-Terminal Operations:
filter()
map()
flatMap()
distinct()
limit()
peek()
Terminal Operations:
anyMatch()
allMatch()
noneMatch()
collect()
count()
findAny()
findFirst()
forEach()
min()
max()
reduce()
toArray()
Non-Terminal Operations:
filter()
map()
flatMap()
distinct()
limit()
peek()
Terminal Operations:
anyMatch()
allMatch()
noneMatch()
collect()
count()
findAny()
findFirst()
forEach()
min()
max()
reduce()
toArray()
No comments:
Post a Comment