Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ashfaqbs/java-stream-lambdas
Java Stream and Lambda Code Sample
https://github.com/ashfaqbs/java-stream-lambdas
java8 lambda stream
Last synced: about 1 month ago
JSON representation
Java Stream and Lambda Code Sample
- Host: GitHub
- URL: https://github.com/ashfaqbs/java-stream-lambdas
- Owner: Ashfaqbs
- Created: 2023-12-29T05:27:45.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-08-15T15:16:10.000Z (3 months ago)
- Last Synced: 2024-09-30T14:06:08.427Z (about 2 months ago)
- Topics: java8, lambda, stream
- Language: Java
- Homepage:
- Size: 130 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Java-Stream-Lambdas
## IntStream Operations
- IntStream is a specialized stream for handling sequences of primitive `int` values. Below are some common operations you can perform with IntStream:
### Creating an IntStream
```
import java.util.stream.IntStream;public class IntStreamExample {
public static void main(String[] args) {
IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
// Print all elements
intStream.forEach(System.out::println);int sum = IntStream.of(1, 2, 3, 4, 5)
.sum();
System.out.println("Sum: " + sum);double average = IntStream.of(1, 2, 3, 4, 5)
.average()
.orElse(0.0);
System.out.println("Average: " + average);IntStream stream = IntStream.of(1, 2, 3, 4, 5);
int max = stream.max().orElseThrow();
int min = IntStream.of(1, 2, 3, 4, 5).min().orElseThrow();
System.out.println("Max: " + max);
System.out.println("Min: " + min);int sum = IntStream.of(1, 2, 3, 4, 5)
.reduce(0, Integer::sum);
System.out.println("Sum: " + sum);}
}```
## Stream Operations### Stream handles sequences of String values and provides various operations for transformation and aggregation.
- Creating a String Stream```
public class StringStreamExample {
public static void main(String[] args) {
Stream stringStream = Stream.of("apple", "banana", "cherry");
// Print all elements
stringStream.forEach(System.out::println);List fruits = Arrays.asList("apple", "banana", "cherry");
String result = fruits.stream()
.collect(Collectors.joining(", "));
System.out.println("Joined: " + result);List fruits = Arrays.asList("apple", "banana", "cherry");
List uppercaseFruits = fruits.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("Uppercase: " + uppercaseFruits);List fruits = Arrays.asList("apple", "banana", "cherry");
List longFruits = fruits.stream()
.filter(fruit -> fruit.length() > 5)
.collect(Collectors.toList());
System.out.println("Long Fruits: " + longFruits);List fruits = Arrays.asList("apple", "banana", "cherry");
long count = fruits.stream().count();
System.out.println("Count: " + count);List fruits = Arrays.asList("apple", "banana", "cherry");
Optional firstFruit = fruits.stream().findFirst();
System.out.println("First: " + firstFruit.orElse("None"));List> listOfLists = Arrays.asList(
Arrays.asList("apple", "banana"),
Arrays.asList("cherry", "date")
);
List flatList = listOfLists.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
System.out.println("Flat List: " + flatList);}
}```