java8 lambda小试牛刀,利用Stream把list转map,并将两个list的数据对象合并起来

java8 lambda小试牛刀,利用Stream把list转map,并将两个list的数据对象合并起来

[java] 
view plain
 copy

  1. public static void main(String[] args) {  
  2.         // 集合1  
  3.         List<SkillUpgrade> lists = new ArrayList<>();  
  4.         SkillUpgrade s = new SkillUpgrade();  
  5.         s.setLv(1);  
  6.         s.setAppearNum(100);  
  7.         lists.add(s);  
  8.         SkillUpgrade s2 = new SkillUpgrade();  
  9.         s2.setLv(2);  
  10.         s2.setAppearNum(200);  
  11.         lists.add(s2);  
  12.         // 集合1  
  13.         List<SkillUpgrade> listx = new ArrayList<>();  
  14.         SkillUpgrade x = new SkillUpgrade();  
  15.         x.setLv(1);  
  16.         x.setSelectNum(1100);  
  17.         listx.add(x);  
  18.         SkillUpgrade x2 = new SkillUpgrade();  
  19.         x2.setLv(2);  
  20.         x2.setSelectNum(1200);  
  21.         listx.add(x2);  
  22.         // 把list转map,{k=lv,vaule=并为自身}  . SkillUpgrade->SkillUpgrade或Function.identity()  
  23.         Map<Integer, SkillUpgrade> map = listx.stream()  
  24.                 .collect(Collectors.toMap(SkillUpgrade::getLv, SkillUpgrade -> SkillUpgrade));  
  25.         System.out.println(“map:=”+map);  
  26.         // 合并  
  27.         lists.forEach(n -> {  
  28.             // 如果等级一致  
  29.             if (map.containsKey(n.getLv())) {  
  30.                 SkillUpgrade obj = map.get(n.getLv());  
  31.                 // 把数量复制过去  
  32.                 n.setSelectNum(obj.getSelectNum());  
  33.             }  
  34.         });  
  35.         System.out.println(“lists:=”+lists);  
  36.         // 重复问题  
  37.         Map<Integer, SkillUpgrade> keyRedo = listx.stream()  
  38.                 .collect(Collectors.toMap(SkillUpgrade::getLv, Function.identity(), (key1, key2) -> key2));  
  39.         // 方式二:指定实例的map  
  40.         Map<Integer, SkillUpgrade> linkedHashMap = listx.stream().collect(Collectors.toMap(SkillUpgrade::getLv,  
  41.                 SkillUpgrade -> SkillUpgrade, (key1, key2) -> key2, LinkedHashMap::new));  
  42.     }  
  43.       
  44.     /** 
  45.      * output:map:={1=SkillUpgrade [skillId=null, skillName=null, lv=1, persNum=null, selectNum=1100, appearNum=null], 2=SkillUpgrade [skillId=null, skillName=null, lv=2, persNum=null, selectNum=1200, appearNum=null]} 
  46.      *        lists:=[SkillUpgrade [skillId=null, skillName=null, lv=1, persNum=null, selectNum=1100, appearNum=100], SkillUpgrade [skillId=null, skillName=null, lv=2, persNum=null, selectNum=1200, appearNum=200]] 
  47.      */  

输出结果:

map:={1=SkillUpgrade [skillId=null, skillName=null, lv=1, persNum=null, selectNum=1100, appearNum=null], 2=SkillUpgrade [skillId=null, skillName=null, lv=2, persNum=null, selectNum=1200, appearNum=null]}
lists:=[SkillUpgrade [skillId=null, skillName=null, lv=1, persNum=null, selectNum=1100, appearNum=100], SkillUpgrade [skillId=null, skillName=null, lv=2, persNum=null, selectNum=1200, appearNum=200]]

    原文作者:qq_32440951
    原文地址: https://blog.csdn.net/qq_32440951/article/details/78030160
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞