-
Notifications
You must be signed in to change notification settings - Fork 0
stream api
Keyhan Hadjari edited this page Sep 6, 2016
·
2 revisions
A stream represents a sequence of elements and supports different kind of operations to perform computation upon those elements. The stream() method returns a sequential stream where as parallelStream() method returns a parallel stream. Look here for good explonation.
List<String> myList =
Arrays.asList("a1", "a2", "b1", "c2", "c1");
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
// C1
// C2- filter
- map
- mapToInt
- mapToLong
- mapToDouble
- flatMap (create one stream of all substreams)
- flatMaptoInt
- flatMaptoLong
- flatMaptoDouble
- distinct (keeps the first appearance of any duplication)
- sorted (sorts according to natural order)
- sorted(comparator) sorted according to comparator
- peek(action) allows peeking into the stream while stream is not final, useful for debugging and logging.
- limit(maxSize) truncates the stream to the maxSize.
- discard(n) discards the first n elements in the stream
- forEach(action) performs an action for each element of this stream.
- forEachOrdered(action) Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
- toArray returns an object array
- toArray(IntFunction<A[]> generator)
- reduce(a,b), example: numbers.stream().reduce(0, Integer::sum); //sums up all values in the stream.