+ 2
Does anyone have a good way of explaining Java Streams with some examples?
Java Streams .stream().filter()
2 Réponses
+ 3
I found a really good explanation with many useful examples here:
https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
I wanted to get familiar with the topic as well, anyway. Streams are extremely useful when you need to perform a sequence of operations or transformations on a collection (array or list). They use ideas from functional programming.
+ 2
For example with an ArrayList of Integers:
If you want to filter all negative Numbers out of the list you need to keep the positive numbers. You an use lambda expressions in filter to decide what elements should be kept in the list.
If you already have the initialized ArrayList<Integer> list:
Arraylist<Integer> newlist =
list.stream().filter((x) -> x >=0 ).collect.toCollection(Arraylist::new);
After using filter you still have a stream on which you can use another filter oder other stream operations like map. If you want a List again you need to use collect and with (Arraylist::new) you can build a new Arraylist.
(Not sure but i think you need to save this List in a new list and cant use list after that again)
(I hope this syntax is correct, this was only how i remember it)