Thursday, February 2, 2023

Java Stream API usage example

 here are a few examples to illustrate its usage:

  1. Filtering a list of employees based on their salary:
List<Employee> employees = ...; employees.stream() .filter(e -> e.getSalary() > 50000) .forEach(System.out::println);
  1. 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);
  1. 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());
  1. 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);
  1. 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));



  1. Summing up a list of numbers:
List<Integer> numbers = ...; int sum = numbers.stream().mapToInt(Integer::intValue).sum(); System.out.println("Sum: " + sum);
  1. 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);
  1. 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);
  1. Removing duplicates from a list:
List<Integer> numbers = ...; List<Integer> result = numbers.stream() .distinct() .collect(Collectors.toList()); System.out.println("Result: " + result);
  1. 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);



  1. 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);
  1. 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);
  1. 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);
  1. 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);
  1. 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);



  1. Sorting a list of strings:
List<String> strings = ...; List<String> result = strings.stream() .sorted() .collect(Collectors.toList()); System.out.println("Result: " + result);
  1. 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);
  1. 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);
  1. 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);
  1. 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);

  1. 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);
  1. Finding the largest number in a list:
List<Integer> numbers = ...; int result = numbers.stream() .max(Comparator.naturalOrder()) .orElse(0); System.out.println("Result: " + result);
  1. Finding the smallest number in a list:
List<Integer> numbers = ...; int result = numbers.stream() .min(Comparator.naturalOrder()) .orElse(0); System.out.println("Result: " + result);
  1. 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);
  1. 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);

  1. Summing the elements of a list of numbers:
List<Integer> numbers = ...; int result = numbers.stream() .mapToInt(Integer::intValue) .sum(); System.out.println("Result: " + result);
  1. 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);
  1. 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);
  1. 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);
  1. 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);

  1. Counting the number of elements in a list:
List<String> strings = ...; long result = strings.stream() .count(); System.out.println("Result: " + result);
  1. 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);
  1. 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);
  1. 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);
  1. 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);

  1. 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);
  1. Converting a list of strings to a set:
List<String> strings = ...; Set<String> result = strings.stream() .collect(Collectors.toSet()); System.out.println("Result: " + result);
  1. 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);
  1. 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);
  1. 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);

  1. 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);
  1. 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);
  1. 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);
  1. 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);
  1. 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