本篇內(nèi)容主要講解“怎么解決Java8 stream操作toMap的key重復(fù)問(wèn)題”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么解決Java8 stream操作toMap的key重復(fù)問(wèn)題”吧!
“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是成都創(chuàng)新互聯(lián)的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個(gè)不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對(duì)成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站開(kāi)發(fā)、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無(wú)止境。
準(zhǔn)備以下User對(duì)象集合 ,構(gòu)造方法User(Long Id, String username)
List<User> userList = new ArrayList<>(); userList.add(new User(1L, "aaa")); userList.add(new User(2L, "bbb")); userList.add(new User(3L, "ccc")); userList.add(new User(2L, "ddd")); userList.add(new User(3L, "eee"));
當(dāng)進(jìn)行普通toMap操作時(shí)
Map<Long, String> map = userList.stream() .collect(Collectors.toMap(User::getId, User::getUsername);
就會(huì)報(bào)錯(cuò),說(shuō)明處理到已存在的key,其對(duì)應(yīng)value為bbb
java.lang.IllegalStateException: Duplicate key bbb at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133) at java.util.HashMap.merge(HashMap.java:1253) at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320) at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169) at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
解決方式就在Java8提供的Collectors.toMap() 方法中,其第三個(gè)參數(shù)就是當(dāng)出現(xiàn) duplicate key的時(shí)候的處理方案
方案一: 出現(xiàn)重復(fù)時(shí),取前面value的值,或者取后面放入的value值,則覆蓋先前的value值
Map<Long, String> map = userList.stream() .collect(Collectors.toMap(User::getId, User::getUsername, (v1, v2) -> v1));
Map<Long, String> map = userList.stream() .collect(Collectors.toMap(User::getId, User::getUsername, (v1, v2) -> v2));
方案二: Map的value可以儲(chǔ)存一個(gè)list,把重復(fù)key的值放入list,再存到value中
userList.stream().collect(Collectors.toMap(User::getId, e -> Lists.newArrayList(e.getUsername()), (List<String> oldList, List<String> newList) -> { oldList.addAll(newList); return oldList; }));
到此,相信大家對(duì)“怎么解決Java8 stream操作toMap的key重復(fù)問(wèn)題”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享題目:怎么解決Java8stream操作toMap的key重復(fù)問(wèn)題
當(dāng)前鏈接:http://jinyejixie.com/article4/gdppoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、靜態(tài)網(wǎng)站、網(wǎng)站策劃、商城網(wǎng)站、外貿(mào)建站、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)