Flask接口签名sign原理与实例代码浅析
1354
2022-11-25
java 实现多个list 合并成一个去掉重复的案例
我就废话不多说了,大家还是直接看代码吧~
public static void main(String[] args){
List list1.add(1); list1.add(2); list1.add(3); list1.add(4); List list2.add(1); list2.add(4); list2.add(7); list2.add(10); List listAll.addAll(list1); listAll.addAll(list2); listAll = new ArrayList System.out.println(listAll); } 输出: [1, 2, 3, 4, 7, 10] 代码要典: 1、合并 使用java.util.List.addAll(Collection extends Integer>) 2、去重,借助LinkedHashSet 补充知识:java8 lambda小试牛刀,利用Stream把list转map,并将两个list的数据对象合并起来 我就废话不多说了,大家还是直接看代码吧~ public static void main(String[] args) { // 集合1 List SkillUpgrade s = new SkillUpgrade(); s.setLv(1); s.setAppearNum(100); lists.add(s); SkillUpgrade s2 = new SkillUpgrade(); s2.setLv(2); s2.setAppearNum(200); lists.add(s2); // 集合1 List SkillUpgrade x = new SkillUpgrade(); x.setLv(1); x.setSelectNum(1100); listx.add(x); SkillUpgrade x2 = new SkillUpgrade(); x2.setLv(2); x2.setSelectNum(1200); listx.add(x2); // 把list转map,{k=lv,vaule=并为自身} . SkillUpgrade->SkillUpgrade或Function.identity() Map .collect(Collectors.toMap(SkillUpgrade::getLv, SkillUpgrade -> SkillUpgrade)); System.out.println("map:="+map); // 合并 lists.forEach(n -> { // 如果等级一致 if (map.containsKey(n.getLv())) { SkillUpgrade obj = map.get(n.getLv()); // 把数量复制过去 n.setSelectNum(obj.getSelectNum()); } }); System.out.println("lists:="+lists); // 重复问题 Map WaZWMkILHj .collect(Collectors.toMap(SkillUpgrade::getLv, Function.identity(), (key1, key2) -> key2)); // 方式二:指定实例的map Map SkillUpgrade -> SkillUpgrade, (key1, key2) -> key2, LinkedHashMap::new)); } /** * output:map:={1=SkillUpgrade [skillId=null, skillName=null, lv=1, persNum=null, selectNum=1100, appearNum=null], 2=SkillUpgrade http://[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]] */ 输出结果: 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]]
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
List
list2.add(1);
list2.add(4);
list2.add(7);
list2.add(10);
List
listAll.addAll(list1);
listAll.addAll(list2);
listAll = new ArrayList
System.out.println(listAll);
}
输出:
[1, 2, 3, 4, 7, 10]
代码要典:
1、合并 使用java.util.List.addAll(Collection extends Integer>)
2、去重,借助LinkedHashSet
补充知识:java8 lambda小试牛刀,利用Stream把list转map,并将两个list的数据对象合并起来
我就废话不多说了,大家还是直接看代码吧~
public static void main(String[] args) {
// 集合1
List
SkillUpgrade s = new SkillUpgrade();
s.setLv(1);
s.setAppearNum(100);
lists.add(s);
SkillUpgrade s2 = new SkillUpgrade();
s2.setLv(2);
s2.setAppearNum(200);
lists.add(s2);
// 集合1
List
SkillUpgrade x = new SkillUpgrade();
x.setLv(1);
x.setSelectNum(1100);
listx.add(x);
SkillUpgrade x2 = new SkillUpgrade();
x2.setLv(2);
x2.setSelectNum(1200);
listx.add(x2);
// 把list转map,{k=lv,vaule=并为自身} . SkillUpgrade->SkillUpgrade或Function.identity()
Map
.collect(Collectors.toMap(SkillUpgrade::getLv, SkillUpgrade -> SkillUpgrade));
System.out.println("map:="+map);
// 合并
lists.forEach(n -> {
// 如果等级一致
if (map.containsKey(n.getLv())) {
SkillUpgrade obj = map.get(n.getLv());
// 把数量复制过去
n.setSelectNum(obj.getSelectNum());
}
});
System.out.println("lists:="+lists);
// 重复问题
Map
WaZWMkILHj .collect(Collectors.toMap(SkillUpgrade::getLv, Function.identity(), (key1, key2) -> key2));
// 方式二:指定实例的map
Map
SkillUpgrade -> SkillUpgrade, (key1, key2) -> key2, LinkedHashMap::new));
}
/**
* output:map:={1=SkillUpgrade [skillId=null, skillName=null, lv=1, persNum=null, selectNum=1100, appearNum=null], 2=SkillUpgrade http://[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]]
*/
输出结果:
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]]
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~