您的当前位置:首页正文

JavaxxList.stream().collect(Collectors.groupi。。。

2022-05-04 来源:好走旅游网
JavaxxList.stream().collect(Collectors.groupi。。。

userList.stream().collect(Collectors.groupingBy(User::getCategory));使⽤详解及扩展。直接上代码(底部附带异常)

@Test

void contextLoads1() {

User user0 = new User(); user0.setId(0);

user0.setNickname(\"name0\"); user0.setUsername(\"username0\"); user0.setCategory(\"学⽣\"); User user1 = new User(); user1.setId(1);

user1.setNickname(\"name1\"); user1.setUsername(\"username1\"); user1.setCategory(\"⽼师\"); User user2 = new User(); user2.setId(2);

user2.setNickname(\"name2\"); user2.setUsername(\"username2\"); user2.setCategory(\"教学主任\");

List userList = new ArrayList<>(); userList.add(user0); userList.add(user1); userList.add(user2);

    //跟据某个属性分组

Map> userMap = userList.stream().collect(Collectors.groupingBy(User::getCategory)); System.out.println(userMap);     // 查询结果 

// {学⽣=[User{id=0, nickname=name0, birthday=null, username=username0, password=null, status=null, createTime=null, updateTime=null, remarks=null}], // ⽼师=[User{id=1, nickname=name1, birthday=null, username=username1, password=null, status=null, createTime=null, updateTime=null, remarks=null}],

// 教学主任=[User{id=2, nickname=name2, birthday=null, username=username2, password=null, status=null, createTime=null, updateTime=null, remarks=null}]}    //根据某个属性分组,汇总某个属性

    Map collect2 = userList.stream().collect(Collectors.groupingBy(User::getCategory,Collectors.summingInt(User::getId)));    System.out.println(collect2);    // 查询结果

    // {学⽣=0, ⽼师=1, 教学主任=2}

    //根据某个属性添加条件过滤数据

    userList = userList.stream().filter(u -> !u.getNickname().equals(\"name0\")).collect(Collectors.toList());    System.out.println(userList);    // 查询结果

    // [User{id=1, nickname=name1, birthday=null, username=username1, password=null, status=null, createTime=null, updateTime=null, remarks=null},    // User{id=2, nickname=name2, birthday=null, username=username2, password=null, status=null, createTime=null, updateTime=null, remarks=null}]    //判断⼀组对象⾥⾯有没有这个属性值

    boolean add = userList.stream().anyMatch(m -> \"name2\".equals(m.getNickname()));    System.out.println(add);

    //取出⼀组对象的某个属性组成⼀个新集合

    List Username=userList.stream().map(User::getUsername).collect(Collectors.toList());    System.out.println(Username);    // 查询结果  

    // [username0, username1, username2]

User实体类

public class User implements Serializable { private static final long serialVersionUID=1L; /** * 主键 */

private Integer id; /** * 昵称 */

private String nickname; /** * 类别 */

private String category; /** * ⽣⽇ */

private LocalDate birthday; /**

* ⽤户名 */

private String username; /** * 密码 */

private String password; /**

* 状态 */

private String status;

/**

* 创建时间 */

private LocalDateTime createTime; /**

* 更新时间 */

private LocalDateTime updateTime; /** * 备注 */

private String remarks;

  // 省略set/get }

element cannot be mapped to a null key

/ / 按照 User 分组,此时 getCategory 有空值,就会报错。 // 改为,增加⾮空过滤。

因篇幅问题不能全部显示,请点此查看更多更全内容