Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alpha74/java_streams_guide
A shortguide to Java 8 streams on Collections
https://github.com/alpha74/java_streams_guide
java java-8 java-collections java-stream java-stream-api java-stream-collector
Last synced: about 1 month ago
JSON representation
A shortguide to Java 8 streams on Collections
- Host: GitHub
- URL: https://github.com/alpha74/java_streams_guide
- Owner: alpha74
- Created: 2024-08-25T17:12:57.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-08-30T06:01:30.000Z (4 months ago)
- Last Synced: 2024-11-19T10:07:52.062Z (about 1 month ago)
- Topics: java, java-8, java-collections, java-stream, java-stream-api, java-stream-collector
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Java streams() Guide
- A guide to Java 8 streams on Collections
-------
### Overview
A birds-eye view of all major methods based on `Collections` and `stream()`
![image](https://github.com/user-attachments/assets/fa11c742-8bf6-46dd-adbc-04056b48485d)
- [Youtube source](https://www.youtube.com/watch?v=33JrZGtKOEE&list=PLUDwpEzHYYLvTPVqVIt7tlBohABLo4gyg&index=1&ab_channel=SDET-QA)
- `Collections`
- It represents a a group of objects as a single entity.- `Stream`
- It is used to process data from collections.- `Non-terminal Operation`
- It is an operation which adds a listener to the stream which may modify the stream elements.- `Terminal Operation`
- It is an operations which returns a result after stream processing.-------
#### filter()
- Takes a `predicate` as a argument, which returns a `boolean` as output.
- Example of filter() on a List of integers.
```
List numbersList = Arrays.asList(10,15,20,25,30);
``````
// Create list of even numbers: using collect()
List evenList1 = numbersList.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());// Same: Create list of even numbers: using toList() shorthard
List evenList2 = numbersList.stream()
.filter(n -> n % 2 == 0)
.toList();
```- Takes a `consumer` expression as input and does not return anything.
```
// Print even numbers
numbersList.stream().filter(n -> n % 2 == 0).forEach(n -> System.out.println(n));// Same: Print even numbers using shorthand method reference
numbersList.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
```- `System.out` is a static method.
- Example of filter() on a List of strings.
```
List stringList = List.of("OptimusPrime", null, "Megatron", "Bumblebee", "Ratchet", null);// Filter non-null values which have len > 7
List longStringList = stringList.stream().filter(s -> s != null && s.length() > 7).toList();
```-------
#### map()
- It accepts a function or consumer as input.
- Example for converting each string in a List to UpperCase
```
List vehicles = List.of("car", "bus", "train", "place", "ship");// Convert each item to upper case
List vehiclesUpperCase = vehicles.stream()
.map(name -> name.toUpperCase())
.collect(Collectors.toList());
```- Example for calculating len of each string and appending to same item
```
// Find length of each element and append to same item
List vehicleLenList = vehicles.stream()
.map(name -> name + " len is " + name.length())
.collect(Collectors.toList());
```-------
#### flatMap()
- Given a collection of objects, `map()` takes one input and returns one output, whereas `flatMap()` takes one input, but returns a stream of objects.
- Example to combine a list of list of objects into single list using `flatMap()`
```
List list1 = Arrays.asList(1,2);
List list2 = Arrays.asList(3,4);
List list3 = Arrays.asList(5,6);List> listList = Arrays.asList(list1, list2, list3);
// Combine all lists into single list
List combinedList = listList.stream().flatMap(x -> x.stream()).collect(Collectors.toList());// Output: [1,2,3,4,5,6]
``````
// Operating on each item in each list and combining to single list
combinedList = listList.stream().flatMap(x -> x.stream()).map(n -> n +10).collect(Collectors.toList());
```-------
#### distinct()
- Find distinct elements from a given collection.
```
List vehicleList = Arrays.asList("car", "bike", "car", "bike", "truck", "ship");
List uniqueVehicleList = vehicleList.stream().distinct().collect(Collectors.toList());
```#### count() & limit()
- `count()` returns count of objects as `long`.
```
List vehicleList = Arrays.asList("car", "bike", "car", "bike", "truck", "ship");
long count = vehicleList.stream().distinct().count(); // Returns 4
```- `limt()` is used to collect a limited number of objects from a stream.
- Takes `maxsize` as input param.```
long count = vehicleList.stream().distinct().limit(2).count(); // Returns 2
```-------
#### min() & max()
- Takes a `comparator()` as input, and returns an `Optional<>` object.
```
List numList = Arrays.asList(1,1,2,2,3,4);Optional minVal = numList.stream().min((val1, val2) -> {return val1.compareTo(val2);});
System.out.println(minVal.get()); // 1Optional maxVal = numList.stream().max((val1, val2) -> {return val1.compareTo(val2);});
System.out.println(maxVal.get()); // 4
```-------
#### reduce()
- Takes input as an `identity` and `accumulator` as params.
- Reduces all elements in the stream to a single object.```
List numList = Arrays.asList(1,1,2,2,3,4);// Calculate sum
Optional sumVal = numList.stream().reduce((val, combinedVal) -> {
return val + combinedVal;
});List vehicleList = Arrays.asList("car", "bike", "car", "bike", "truck", "ship");
// Append all strings to single string
Optional appended = vehicleList.stream().reduce((val, combined) -> {
return val+combined;
});
```-------
#### toArray()
- It converts the stream of objects to array.
```
List numList = Arrays.asList(1,1,2,2,3,4);// Convert to array
Object arr[] = numList.stream().toArray();
```-------
#### sorted()
- Used to sort a stream of objects.
```
List numList = Arrays.asList(2,5,1,6,3,7);// Sort in ascending order
List ascSorted = numList.stream().sorted().collect(Collectors.toList());// Sort in descending order
List descSorted = numList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
```-------
#### anyMatch()
- Returns `true` if condition matches any value in the stream.
```
Set fruits = new HashSet<>();fruits.add("21 mangoes");
fruits.add("31 apples");
fruits.add("412 oranges");
fruits.add("13 guavas");boolean res1 = fruits.stream().anyMatch(val -> {return val.contains("4");}); // true
```#### allMatch()
- Returns `true` if all elements match the condition, else `false`.
```
boolean res2 = fruits.stream().allMatch(val -> {return val.contains("1");}); // true
```#### noneMatch()
- Returns `true` if none of the elements match the conditions, else `false`.
```
boolean res3 = fruits.stream().noneMatch(val -> {return val.contains("s");}); // false
```-------
#### findAny()
- Returns `Optional<>`, or `NoSuchElementException` if no element is found.
- It may or may not return the first matched element.```
List numList = Arrays.asList(2,5,1,6,3,7);Optional any = numList.stream().findAny(); // 2 (but non-deterministic, helpful in parallelStream())
```#### findFirst()
- Returns `Optional<>`, or `NoSuchElementException` if no element is found.
- It strictly returns the first matched element.```
List numList = Arrays.asList(2,5,1,6,3,7);Optional first = numList.stream().findFirst(); // 2
```-------
##### Stream.concat()
- Used to concatenate two `Stream` objects into single `Stream`.
```
List list1 = Arrays.asList("dog", "cat", "rat");
List list2 = Arrays.asList("eagle", "sparrow", "hen");Stream stream1 = list1.stream();
Stream stream2 = list2.stream();List concatenated = Stream.concat(stream1, stream2).collect(Collectors.toList());
System.out.println(concatenated);
```-------
#### Parallel Streams
- All collections support `parallelStream()` method that returns a possibly parallel stream.
- When a stream executes in parallel, the Java Runtime divides the stream into multiple substreams.
- The aggregate operators iterates over these sub-streams in parallel and then combine the result. This can improve the performance.- Eg: `List parallelStream = list1.parallelStream()`
- Using `stream()`, operations are performed sequentially, but using `parallelStream()`, operations are performed parallely.
```
List vehicleList = Arrays.asList("car", "bike", "car", "bike", "truck", "ship");// Using stream()
List filterSeq = vehicleList.stream().filter(x -> x.contains("a")).collect(Collectors.toList());// Using parallelStream()
List filterPar = vehicleList.parallelStream().filter(x -> x.contains("a")).collect(Collectors.toList());// Convert stream to parallel stream
List filterPar2 = vehicleList.stream().parallel().filter(x -> x.contains("a")).collect(Collectors.toList());
```