zoukankan      html  css  js  c++  java
  • java stream 集合运算

    1.对列表进行分组,构建成一个map对象。

    键为用户名称,值为用户对象列表。

    Person p1 = new Person("张三", new BigDecimal("10.0"));
    Person p2 = new Person("王五", new BigDecimal("10.0"));
    Person p3 = new Person("李四", new BigDecimal("10.0"));
    Person p4 = new Person("李四", new BigDecimal("10.0"));
    Person p5 = new Person("张三", new BigDecimal("10.0"));
    List<Person> list = new ArrayList<>();
    list.add(p1);
    list.add(p2);
    list.add(p3);
    list.add(p4);
    list.add(p5);

    这个我们之前写代码可以需要写成如下的方式:

    private Map<String,List<Person >> convertToMap(List<Person > list){
    
      Map<String,List<Persion>> map=new HashMap<>();
           for(Person  p: list){
    
              String name=p.getName();
              if(map.containsKey(name){
    
         map.get(name).add(p);
              }
    
        else{
    
               List<Person> list=new ArrayList();
                    list.add(p);
    
                    map.put(name,p);
    
             }
           }
           return map;
    
    }

    写了一大段代码。

    使用steam 就简单了,一行代码解决问题。

    Map<String, List<Person>> collect = list.stream().collect(Collectors.groupingBy(person -> person.getName()));
    System.out.println(collect);

    2.将list 转成 map 对象。

    Person p1 = new Person("1","张三", new BigDecimal("10.0"));
    Person p2 = new Person("2","王五", new BigDecimal("10.0"));
    Person p3 = new Person("3","李四", new BigDecimal("10.0"));
    Person p4 = new Person("4","李四", new BigDecimal("10.0"));
    Person p5 = new Person("5","张三", new BigDecimal("10.0"));
    List<Person> list = new ArrayList<>();
    list.add(p1);
    list.add(p2);
    list.add(p3);
    list.add(p4);
    list.add(p5);

    上面的数据 我们转成 Map<String,Person> 对象。

    Map<String, Person> nodeMap = bpmSolUsergroups.stream().collect(Collectors.toMap(p->p.getId(), p -> p));

     3.对列表进行过滤

    public static void main(String[] args) {
            List<BpmCheckFile> filesInst =new ArrayList<>();
            BpmCheckFile file1=new BpmCheckFile();
            file1.setJumpId("1");
    
    
            BpmCheckFile file11=new BpmCheckFile();
            file11.setJumpId("1");
    
            BpmCheckFile file2=new BpmCheckFile();
            file2.setJumpId("2");
    
            filesInst.add(file1);
            filesInst.add(file11);
            filesInst.add(file2);
    
            List<BpmCheckFile> files= filesInst.stream().filter(p->p.getJumpId()=="1").collect(Collectors.toList());
            System.err.println(files.size());
    
    
        }

    对列表数据进行过滤。

    4.将JSONARRAY 转换成 Map对象

    public static void main(String[] args) {
            String str="[{id:1,name:'ray'},{id:2,name:'zyg'}]";
            JSONArray ary=JSONArray.parseArray(str);
    
            Map<String, JSONObject>   map= ary.stream().collect(Collectors.toMap(i->{JSONObject json=(JSONObject)i;
                return json.getString("id");} , p->{JSONObject json=(JSONObject)p;return json;}));
    
            System.err.println(map);
        }
  • 相关阅读:
    祭蟑螂文
    端午随笔
    有感于一个用户的留言
    jQuery UI dialog插件出错信息:$(this).dialog is not a function
    @@ERROR, BEGIN TRY/CATCH and XACT_ABORT
    检索 COM 类工厂中 CLSID 为{0002450000000000C000000000000046} 的组件时失败,原因是出现以下错误: 80070005。
    【SSB】使用命令行来检测Broker是否正常通信
    C#保留2位小数总结
    引用了混乱的组件或类库可能会导致WCF代理类无法正确生成
    [转]从属性赋值到MVVM模式详解
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/11613563.html
Copyright © 2011-2022 走看看