本文主要是介绍java Stream collect()使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Stream的collect()方法是一个非常强大的终端操作,用于将流中的元素收集到一个结果容器中。collect()方法通常接收一个Collector接口的实现作为参数。
toList():将流中的元素收集到一个List中
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class CollectToListExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);List<Integer> evenNumbers = numbers.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());System.out.println(evenNumbers);}}
toSet():将流中的元素收集到一个Set中,去除重复元素。
import java.util.Arrays;import java.util.Set;import java.util.stream.Collectors;public class CollectToSetExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 3, 5);Set<Integer> distinctNumbers = numbers.stream().collect(Collectors.toSet());System.out.println(distinctNumbers);}}
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper):将流中的元素收集到一个Map中,通过指定键和值的映射函数。
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class CollectToMapExample {public static void main(String[] args) {List<Person> people = Arrays.asList(new Person("Alice", 25),new Person("Bob", 30),new Person("Charlie", 28));Map<String, Integer> nameToAgeMap = people.stream().collect(Collectors.toMap(Person::getName, Person::getAge));System.out.println(nameToAgeMap);}static class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;}}}
joining():将流中的元素连接成一个字符串
import java.util.Arrays;import java.util.stream.Collectors;public class CollectJoiningExample {public static void main(String[] args) {List<String> words = Arrays.asList("Hello", "World", "Java");String joinedString = words.stream().collect(Collectors.joining(", "));System.out.println(joinedString);}}
summingInt(ToIntFunction<? super T> mapper)、summingLong(ToLongFunction<? super T> mapper)、summingDouble(ToDoubleFunction<? super T> mapper):分别用于计算流中元素的整数、长整数和双精度浮点数属性的总和。
import java.util.Arrays;import java.util.stream.Collectors;public class CollectSummingExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);int sum = numbers.stream().collect(Collectors.summingInt(Integer::intValue));System.out.println(sum);}}
averagingInt(ToIntFunction<? super T> mapper)、averagingLong(ToLongFunction<? super T> mapper)、averagingDouble(ToDoubleFunction<? super T> mapper):分别用于计算流中元素的整数、长整数和双精度浮点数属性的平均值。
import java.util.Arrays;import java.util.stream.Collectors;public class CollectAveragingExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);double average = numbers.stream().collect(Collectors.averagingInt(Integer::intValue));System.out.println(average);}}
groupingBy(Function<? super T,? extends K> classifier):根据指定的分类函数对流中的元素进行分组,返回一个Map<K, List>。
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class CollectGroupingByExample {public static void main(String[] args) {List<Person> people = Arrays.asList(new Person("Alice", 25),new Person("Bob", 30),new Person("Charlie", 28),new Person("David", 25));Map<Integer, List<Person>> ageGroupMap = people.stream().collect(Collectors.groupingBy(Person::getAge));System.out.println(ageGroupMap);}static class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;}}}
partitioningBy(Predicate<? super T> predicate):根据指定的谓词对流中的元素进行分区,返回一个Map<Boolean, List>,其中键为true和false,分别对应满足和不满足谓词的元素列表。
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class CollectPartitioningByExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);Map<Boolean, List<Integer>> evenOddMap = numbers.stream().collect(Collectors.partitioningBy(n -> n % 2 == 0));System.out.println(evenOddMap);}}
这篇关于java Stream collect()使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!