here are a few examples to illustrate its usage:
- Filtering a list of employees based on their salary:
List<Employee> employees = ...;
employees.stream()
.filter(e -> e.getSalary() > 50000)
.forEach(System.out::println);
- Sorting a list of students based on their grades:
List<Student> students = ...;
students.stream()
.sorted((s1, s2) -> s2.getGrade() - s1.getGrade())
.forEach(System.out::println);
- Finding the maximum value in a list of numbers:
List<Integer> numbers = ...;
OptionalInt max = numbers.stream().mapToInt(Integer::intValue).max();
System.out.println("Max value: " + max.getAsInt());
- Counting the number of strings in a list that have a length greater than 5:
List<String> strings = ...;
long count = strings.stream()
.filter(s -> s.length() > 5)
.count();
System.out.println("Count: " + count);
- Grouping a list of products based on their category:
List<Product> products = ...;
Map<String, List<Product>> map = products.stream()
.collect(Collectors.groupingBy(Product::getCategory));
map.forEach((k, v) -> System.out.println(k + ": " + v));
- Summing up a list of numbers:
List<Integer> numbers = ...;
int sum = numbers.stream().mapToInt(Integer::intValue).sum();
System.out.println("Sum: " + sum);
- Finding the first element in a list that matches a condition:
List<Employee> employees = ...;
Employee result = employees.stream()
.filter(e -> e.getSalary() > 50000)
.findFirst()
.orElse(null);
System.out.println("Result: " + result);
- Converting a list of strings to uppercase:
List<String> strings = ...;
List<String> result = strings.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Removing duplicates from a list:
List<Integer> numbers = ...;
List<Integer> result = numbers.stream()
.distinct()
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Skipping the first n elements in a list:
List<Integer> numbers = ...;
List<Integer> result = numbers.stream()
.skip(5)
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Mapping a list of numbers to their squares:
List<Integer> numbers = ...;
List<Integer> result = numbers.stream()
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Reducing a list of numbers to their product:
List<Integer> numbers = ...;
int result = numbers.stream().reduce(1, (a, b) -> a * b);
System.out.println("Result: " + result);
- Flat mapping a list of lists:
List<List<Integer>> lists = ...;
List<Integer> result = lists.stream()
.flatMap(l -> l.stream())
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Partitioning a list of numbers into even and odd numbers:
List<Integer> numbers = ...;
Map<Boolean, List<Integer>> result = numbers.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
System.out.println("Result: " + result);
- Summing up the lengths of strings in a list:
List<String> strings = ...;
int result = strings.stream()
.mapToInt(String::length)
.sum();
System.out.println("Result: " + result);
- Sorting a list of strings:
List<String> strings = ...;
List<String> result = strings.stream()
.sorted()
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Reversing the order of a list of numbers:
List<Integer> numbers = ...;
List<Integer> result = numbers.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Grouping strings by their length:
List<String> strings = ...;
Map<Integer, List<String>> result = strings.stream()
.collect(Collectors.groupingBy(String::length));
System.out.println("Result: " + result);
- Counting the number of strings in a list that start with a given letter:
List<String> strings = ...;
long result = strings.stream()
.filter(s -> s.startsWith("A"))
.count();
System.out.println("Result: " + result);
- Joining all the strings in a list into a single string:
List<String> strings = ...;
String result = strings.stream()
.collect(Collectors.joining(", "));
System.out.println("Result: " + result);
- Finding the first string in a list that starts with a given letter:
List<String> strings = ...;
String result = strings.stream()
.filter(s -> s.startsWith("A"))
.findFirst()
.orElse("");
System.out.println("Result: " + result);
- Finding the largest number in a list:
List<Integer> numbers = ...;
int result = numbers.stream()
.max(Comparator.naturalOrder())
.orElse(0);
System.out.println("Result: " + result);
- Finding the smallest number in a list:
List<Integer> numbers = ...;
int result = numbers.stream()
.min(Comparator.naturalOrder())
.orElse(0);
System.out.println("Result: " + result);
- Computing the average of a list of numbers:
List<Integer> numbers = ...;
double result = numbers.stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0);
System.out.println("Result: " + result);
- Extracting a sublist of the first n elements in a list:
List<Integer> numbers = ...;
List<Integer> result = numbers.stream()
.limit(10)
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Summing the elements of a list of numbers:
List<Integer> numbers = ...;
int result = numbers.stream()
.mapToInt(Integer::intValue)
.sum();
System.out.println("Result: " + result);
- Multiplying all the elements of a list of numbers:
List<Integer> numbers = ...;
int result = numbers.stream()
.reduce(1, (a, b) -> a * b);
System.out.println("Result: " + result);
- Checking if all the strings in a list are equal to a given string:
List<String> strings = ...;
boolean result = strings.stream()
.allMatch(s -> s.equals("A"));
System.out.println("Result: " + result);
- Checking if any of the strings in a list are equal to a given string:
List<String> strings = ...;
boolean result = strings.stream()
.anyMatch(s -> s.equals("A"));
System.out.println("Result: " + result);
- Checking if none of the strings in a list are equal to a given string:
List<String> strings = ...;
boolean result = strings.stream()
.noneMatch(s -> s.equals("A"));
System.out.println("Result: " + result);
- Counting the number of elements in a list:
List<String> strings = ...;
long result = strings.stream()
.count();
System.out.println("Result: " + result);
- Grouping elements of a list by a certain property:
List<Person> people = ...;
Map<String, List<Person>> result = people.stream()
.collect(Collectors.groupingBy(Person::getCountry));
System.out.println("Result: " + result);
- Mapping a list of objects to a list of their properties:
List<Person> people = ...;
List<String> result = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Flattening a list of lists into a single list:
List<List<Integer>> numbers = ...;
List<Integer> result = numbers.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Finding the maximum value in a list of custom objects based on a certain property:
List<Person> people = ...;
Person result = people.stream()
.max(Comparator.comparingInt(Person::getAge))
.orElse(null);
System.out.println("Result: " + result);
- Filtering a list of strings based on their length:
List<String> strings = ...;
List<String> result = strings.stream()
.filter(s -> s.length() > 5)
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Converting a list of strings to a set:
List<String> strings = ...;
Set<String> result = strings.stream()
.collect(Collectors.toSet());
System.out.println("Result: " + result);
- Sorting a list of custom objects based on a certain property:
List<Person> people = ...;
List<Person> result = people.stream()
.sorted(Comparator.comparing(Person::getName))
.collect(Collectors.toList());
System.out.println("Result: " + result);
- Finding the average value of a list of numbers:
List<Integer> numbers = ...;
double result = numbers.stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0);
System.out.println("Result: " + result);
- Summing the lengths of all strings in a list:
List<String> strings = ...;
int result = strings.stream()
.mapToInt(String::length)
.sum();
System.out.println("Result: " + result);
- Splitting a list into multiple lists based on a certain condition:
List<Integer> numbers = ...;
Map<Boolean, List<Integer>> result = numbers.stream()
.collect(Collectors.partitioningBy(n -> n > 5));
System.out.println("Result: " + result);
- Finding the first element in a list that satisfies a certain condition:
List<String> strings = ...;
String result = strings.stream()
.filter(s -> s.length() > 5)
.findFirst()
.orElse(null);
System.out.println("Result: " + result);
- Finding the last element in a list that satisfies a certain condition:
List<String> strings = ...;
String result = strings.stream()
.filter(s -> s.length() > 5)
.reduce((first, second) -> second)
.orElse(null);
System.out.println("Result: " + result);
- Joining the elements of a list into a single string:
List<String> strings = ...;
String result = strings.stream()
.collect(Collectors.joining(", "));
System.out.println("Result: " + result);
- Finding the sum of the lengths of all strings in a list that satisfy a certain condition:
List<String> strings = ...;
int result = strings.stream()
.filter(s -> s.length() > 5)
.mapToInt(String::length)
.sum();
System.out.println("Result: " + result);
No comments:
Post a Comment