streams

public class streams {
  @Data
  @Builder
  static class SmCameraVO {
    public String name;
    public String sex;
    public int age;
    public String conf;
  }

  public static void main(String[] args) {
    //
    SmCameraVO build0 = SmCameraVO.builder().name("张三").sex("男").age(18).conf("0.18").build();
    SmCameraVO build1 = SmCameraVO.builder().name("李四").sex("男").age(28).conf("0.28").build();
    SmCameraVO build2 = SmCameraVO.builder().name("张三风").sex("女").age(18).conf("0.18").build();

    List<SmCameraVO> smCameraVOS = Arrays.asList(build0, build1, build2);
    smCameraVOS.forEach(System.out::println);

    // 获取做大年龄数据
    Optional<SmCameraVO> max = smCameraVOS.stream().max((o1, o2) -> o1.getAge() - o2.getAge());
    log.info("{}", max.get().getAge());

    // 过滤后转换成集合
    List<String> likeZ =
        smCameraVOS.stream()
            .filter(item -> item.getName().contains("张"))
            .map(SmCameraVO::getName)
            .collect(Collectors.toList());
    log.info("likeZ:{}", likeZ);

    // 告警信息转换成 map(key list)
    Map<String, List<SmCameraVO>> hikIdMapLists =
        smCameraVOS.stream().collect(Collectors.groupingBy(SmCameraVO::getSex));
    log.info("hikIdMapLists:{}", hikIdMapLists);

    // 告警信息转换成 map(key object)
    Map<String, SmCameraVO> mapObject =
        smCameraVOS.stream().collect(toMap(SmCameraVO::getName, task -> task));
    log.info("mapObject:{}", mapObject);

	String strs = "[{\"userID\":\"zhuyijia3\",\"username\":\"zhuyijia3\"},{\"userID\":\"liudanfeng6\",\"username\":\"liudanfeng6\"}]";
	List<user> users = JSON.parseArray(strs, user.class);
	List<String> userIds = 	users.stream().map(user::getUserID).collect(Collectors.toList());
	userIds.forEach(System.out::println);
	String collect = userIds.stream().collect(Collectors.joining(" "));
	System.out.println(collect);

  }
}
posted @ 2023-04-19 15:34  微辰  阅读(73)  评论(0)    收藏  举报