list.stream().collect例子

复制代码
// 已有股票ID列表
List<String> existList = Arrays.asList("001", "003", "005");

// 待处理的股票列表
List<Stock> stocks = Arrays.asList(
        new Stock("001", "腾讯", 350.0),
        new Stock("002", "阿里", 180.0),
        new Stock("003", "百度", 120.0),
        new Stock("004", "京东", 80.0),
        new Stock("005", "美团", 150.0)
);

// 分组:是否存在于 existList 中
Map<Boolean, List<Stock>> grouped = stocks.stream()
        .collect(Collectors.groupingBy(
                stock -> existList.contains(stock.getStkId())
        ));

System.out.println("已存在的股票: " + grouped.get(true));


List<Stock> stocks1 = grouped.get(true);
for(Stock stock : stocks1){
    System.out.println(stock.getStkId());//已存在的股票:  001   003 005
}
System.out.println("不存在的股票: " + grouped.get(false));
List<Stock> stocks2 = grouped.get(false);
for(Stock stock : stocks2){
    System.out.println(stock.getStkId());//不存在的股票:002  004
}
复制代码
  List<Person> people = Arrays.asList(
          new Person("Alice", 25, "IT"),
          new Person("Bob", 30, "HR"),
          new Person("Charlie", 25, "IT"),
          new Person("David", 30, "HR")
  );

  // 按部门分组
  Map<String, List<Person>> byDept = people.stream()
          .collect(Collectors.groupingBy(Person::getDepartment));
   // 结果: {IT=[Alice, Charlie], HR=[Bob, David]}
  System.out.println(byDept); //{HR=[Person.Person@7ab2bfe1, Person.Person@497470ed], IT=[Person.Person@63c12fb0, Person.Person@b1a58a3]}
  for(String dept : byDept.keySet()){
      System.out.println(dept);
      for(Person p : byDept.get(dept)){
          System.out.print(p.getName()+"   ");
      }
      System.out.println();
  }
 /* HR
  Bob   David
  IT
  Alice   Charlie*/


  // 分组后对值进行操作
  Map<String, Long> countByDept = people.stream()
          .collect(Collectors.groupingBy(
                  Person::getDepartment,
                  Collectors.counting()
          ));
  System.out.println(countByDept);//{HR=2, IT=2}

  // 分组后提取特定字段
  Map<String, Set<String>> namesByDept = people.stream()
          .collect(Collectors.groupingBy(
                  Person::getDepartment,
                  Collectors.mapping(Person::getName, Collectors.toSet())
          ));
  System.out.println(namesByDept);//{HR=[Bob, David], IT=[Alice, Charlie]}




  List<String> names = Arrays.asList( "David","Alice", "Charlie", "Bob","David","Alice","Aa");

  // 转换为 List
  List<String> list = names.stream()
          .filter(name -> name.length() > 3)
          .collect(Collectors.toList());
  System.out.println(list); // [David, Alice, Charlie, David, Alice]

// 使用 toCollection 指定具体实现
  LinkedList<String> linkedList = names.stream()
          .collect(Collectors.toCollection(LinkedList::new));
  System.out.println(linkedList);//[David, Alice, Charlie, Bob, David, Alice, Aa]

// 转换为 Set(自动去重)
  Set<String> set = names.stream()
          .collect(Collectors.toSet());
  System.out.println(set); // [Aa, Bob, Alice, Charlie, David]

 // 使用 toCollection 指定 TreeSet
  Set<String> treeSet = names.stream()
          .collect(Collectors.toCollection(TreeSet::new));
  System.out.println(treeSet);//[Aa, Alice, Bob, Charlie, David]


  // 简单连接
  String result1 = names.stream()
          .collect(Collectors.joining());
  System.out.println(result1); // DavidAliceCharlieBobDavidAliceAa


 // 带分隔符
  String result2 = names.stream()
          .collect(Collectors.joining(", "));
  System.out.println(result2); // David, Alice, Charlie, Bob, David, Alice, Aa

  // 带前缀和后缀
  String result3 = names.stream()
          .collect(Collectors.joining(", ", "[", "]"));
  System.out.println(result3); // [David, Alice, Charlie, Bob, David, Alice, Aa]
相关推荐
rannn_11112 分钟前
【苍穹外卖|Day4】套餐页面开发(新增套餐、分页查询、删除套餐、修改套餐、起售停售)
java·spring boot·后端·学习
qq_124987075316 分钟前
基于JavaWeb的大学生房屋租赁系统(源码+论文+部署+安装)
java·数据库·人工智能·spring boot·计算机视觉·毕业设计·计算机毕业设计
短剑重铸之日22 分钟前
《设计模式》第十一篇:总结
java·后端·设计模式·总结
若鱼19191 小时前
SpringBoot4.0新特性-Observability让生产环境更易于观测
java·spring
觉醒大王1 小时前
强女思维:着急,是贪欲外显的相。
java·论文阅读·笔记·深度学习·学习·自然语言处理·学习方法
努力学编程呀(๑•ี_เ•ี๑)1 小时前
【在 IntelliJ IDEA 中切换项目 JDK 版本】
java·开发语言·intellij-idea
码农小卡拉1 小时前
深入解析Spring Boot文件加载顺序与加载方式
java·数据库·spring boot
向上的车轮1 小时前
为什么.NET(C#)转 Java 开发时常常在“吐槽”Java:checked exception
java·c#·.net
Dragon Wu1 小时前
Spring Security Oauth2.1 授权码模式实现前后端分离的方案
java·spring boot·后端·spring cloud·springboot·springcloud
跳动的梦想家h1 小时前
环境配置 + AI 提效双管齐下
java·vue.js·spring