Stream has below features :
- No Storage : Streams don't have separate storage like collection, array
- Functional in Nature : Produces a result but does not modify source
- Laziness Seeking : All Intermediate operations are always lazy
- Consumable : Elements of stream are only visited once during life of stream life.
- using stream() or parallelStream()
- Arrays.stream(Object[])
- Stream.of(Object[])
- IntStream.range(int,int)
- Random.ints()
- Return new stream always
- Traverse does not begin until terminal operation of pipeline executed
- These have two main times
- Stateless : Each element processed separately
- Stateful : Complete object processed to generate result. e.g. sort,distinct
- These are always Lazy loaded
- List of Intermediate operations :
- filter
- map
- flatmap
- distinct
- sorted
- limit
- skip
- Returns primitive value, Concrete type like Optional, void
- These are eagerly executed & always last operation
- List of Terminal operations :
- allMatch()
- anyMatch()
- noneMatch()
- collect()
- count()
- forEach()
- min()
- max()
- reduce()
When to use Collection? If you want to represent group of objects as single entity then we should go for collection.
When to use Streams ? If you want to process objects from collection then we should use Streams.
To perform conditional check things always use filter
numbersList.stream().filter(iNum -> iNum % 2 == 0).collect(Collectors.toList())
To perform any logical operation and process collection use map
numbersList.stream().map(iNum -> iNum * 2).collect(Collectors.toList());
To collect the result
numbersList.stream().map(iNum -> iNum * 2).collect(Collectors.toList());
To count the number of objects are there in the matching criteria
list.stream().filter(m->m>40).count()
To sort the result. By Default this method support natural sorting.
list.stream().sorted().collect(Collectors.toList()) -- Natural Sorting
How to reverse the sorted collection order.
1) Use comparator : list.stream().sorted((i1,i2)->(i1<i2)?1:(i1>i2)?-1:0).collect(Collectors.toList())
2) Use -ve operator : list.stream().sorted((i1,i2)-> -i1.compareTo(i2)).collect(Collectors.toList())
3) Reversing order of element : list.stream().sorted((i1,i2)-> i2.compareTo(i1)).collect(Collectors.toList())
Note : Here min and max value change according to way how you implement comparator Ascending or Descending In Short here min means always first element in sorted result and max is last element in sorted result
Note : Here min and max value change according to way how you implement comparator Ascending or Descending In Short here max is last element in sorted result
Using foreach to execute function
1) Inbuild Function
2) Custom Function
e.g
1) names.stream().forEach(System.out::println);
2) numbers.stream().forEach(
i->System.out.println("New Way :: The square of "+i+" is "+(i*i)+".")
);
To convert Collection into array
numbers.stream().toArray(Integer[] ::new);
1)Streams.of(array)
2)Streams.of(1,2,3,4,5,6,7,8,9,0,11,22,33,455,7)
Hello