您好,登錄后才能下訂單哦!
利用java 實現(xiàn)合并多個list 并去重?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
public static void main(String[] args){ List<Integer> list1 = new ArrayList<Integer>(); list1.add(1); list1.add(2); list1.add(3); list1.add(4); List<Integer> list2 = new ArrayList<Integer>(); list2.add(1); list2.add(4); list2.add(7); list2.add(10); List<Integer> listAll = new ArrayList<Integer>(); listAll.addAll(list1); listAll.addAll(list2); listAll = new ArrayList<Integer>(new LinkedHashSet<>(listAll)); System.out.println(listAll); }
輸出:
[1, 2, 3, 4, 7, 10]
代碼要典:
1、合并 使用java.util.List.addAll(Collection<? extends Integer>)
2、去重,借助LinkedHashSet
補充知識:java8 lambda小試牛刀,利用Stream把list轉(zhuǎn)map,并將兩個list的數(shù)據(jù)對象合并起來
我就廢話不多說了,大家還是直接看代碼吧~
public static void main(String[] args) { // 集合1 List<SkillUpgrade> lists = new ArrayList<>(); 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> listx = new ArrayList<>(); 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轉(zhuǎn)map,{k=lv,vaule=并為自身} . SkillUpgrade->SkillUpgrade或Function.identity() Map<Integer, SkillUpgrade> map = listx.stream() .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()); // 把數(shù)量復(fù)制過去 n.setSelectNum(obj.getSelectNum()); } }); System.out.println("lists:="+lists); // 重復(fù)問題 Map<Integer, SkillUpgrade> keyRedo = listx.stream() .collect(Collectors.toMap(SkillUpgrade::getLv, Function.identity(), (key1, key2) -> key2)); // 方式二:指定實例的map Map<Integer, SkillUpgrade> linkedHashMap = listx.stream().collect(Collectors.toMap(SkillUpgrade::getLv, 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 [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]] */
輸出結(jié)果:
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]]
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。