当前位置:首页 > 编程笔记 > 正文
已解决

【JDK 8 -收集和统计】7.1 collector 收集器

来自网友在路上 158858提问 提问时间:2023-09-25 07:47:13阅读次数: 58

最佳答案 问答题库588位专家为你答疑解惑

一、collect()方法

二、两个重载方法

2.1 方法一

2.2 方法二(常用)

三、Collector 的 作用

四、Collectors 的作用

> Collectors.toList() 

> Collectors.toMap()

> Collectors.toSet()

> Collectors.toCollection() : 用自定义的实现Collection的数据结构收集

五、实战

Stage 1:准备工作,实体类实现Comparable接口

Stage 2:示例

执行结果


一、collect()方法

  • 终端操作用于对流中的数据进行归集操作

  • collect方法接受的参数是一个Collector

二、两个重载方法

  • 在Stream接口里面

2.1 方法一

    <R> R collect(Supplier<R> supplier,BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner);

2.2 方法二(常用)

 <R, A> R collect(Collector<? super T, A, R> collector);

三、Collector 的 作用

  • 就是收集器,也是一个接口,它的工具类Collectors提供了很多工厂方法

四、Collectors 的作用

  • 工具类提供了很多常见的收集器实现

> Collectors.toList() 

  • ArrayList::new,创建一个ArrayList作为累加器

  • List::add,对流中元素的操作就是直接添加到累加器中

  • reduce 操作,对子任务归集结果addAll,后一个子任务的结果直接全部添加到前一个子任务结果中

  • CH_ID 是一个unmodifiableSet集合

Collectors.toMap()

Collectors.toSet()

Collectors.toCollection() : 用自定义的实现Collection的数据结构收集

  • Collectors.toCollection (LinkedList::new)

  • Collectors.toCollection (CopyOnWriteArrayList::new)

  • Collectors.toCollection (TreeSet::new)

五、实战

Stage 1:准备工作,实体类实现Comparable接口

  • TreeSet 的特点是可排序、不重复,即TreeSet要求存放的对象必须是可排序的。如果对象之间不可排序,就会抛出这个异常(cannot be cast to java.lang.Comparable )。

package com.learning.javalearning.lambda.chapter5;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@NoArgsConstructor
@AllArgsConstructor
@Data
public class User implements Comparable<User> {private int id;private String name;private String pwd;private int age;@Overridepublic int compareTo(User o) {int cmp = age - o.age;return cmp != 0 ? cmp : name.compareTo(o.name);}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", pwd='" + pwd + '\'' +", age=" + age +'}';}public User(String name, int age) {this.name = name;this.age = age;}public User(int id, String name, String pwd) {this.id = id;this.name = name;this.pwd = pwd;}public User(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}
}

Stage 2:示例

import com.learning.javalearning.lambda.chapter5.User;import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;public class CollectDemo {public static void main(String[] args) {List<User> list = Arrays.asList(new User(1, "陆小凤", 19),new User(2, "西门吹雪", 20),new User(3, "西门吹雪", 20),//思考为什么返回TreeSet这个User会删除new User(4, "叶孤城", 20));List<User> result = list.stream().collect(Collectors.toList());System.out.println("【不常用】Collectors.toCollection()返回自定义类型:如 Linkedlist");List<User> linkedList = list.stream().collect(Collectors.toCollection(LinkedList::new));List<User> copyOnWriteArrayList = list.stream().collect(Collectors.toCollection(CopyOnWriteArrayList::new));Set<User> treeSet = list.stream().collect(Collectors.toCollection(TreeSet::new));System.out.println("LinkedList:" + linkedList);System.out.println("CopyOnWriteArrayList:" + copyOnWriteArrayList);System.out.println("TreeSet:" + treeSet);System.out.println("【常用】直接返回:Collectors.toXXX()");List<User> list1 = list.stream().collect(Collectors.toList());Set<User> set = list.stream().collect(Collectors.toSet());Map<Integer, String> map = list.stream().collect(Collectors.toMap(User::getId, User::getName));System.out.println("toList:" + list1);System.out.println("toSet():" + set);System.out.println("toMap():" + map);}
}

执行结果

 

六、参考

如何解决 cannot be cast to java.lang.Comparable问题?_msg1_的博客-CSDN博客

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"【JDK 8 -收集和统计】7.1 collector 收集器":http://eshow365.cn/6-13330-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!