java8lambda将list对象改为Map对象

更新时间:2023-05-13 01:40:54 阅读: 评论:0

java8lambda将list对象改为Map对象list转map
java8 lambda将List转换为Map⽐较简单,但是 有坑:
List<Boy> llOutList = wArrayList();
内存检测Boy one = new Boy();
one.tId(34343);
llOutList.add(one);
Boy two = new Boy();
two.tId(34343);
llOutList.add(two);
Map<Integer, Boy> res = llOutList.stream().Map(Boy::getId, Function.identity()));
System.out.Set());
public static class Boy{
int id;
int age;
public int getId() {
return id;
}
抖音好听的歌曲public void tId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void tAge(int age) {
this.age = age;
}
}
注意这⾥使⽤的是:Function.identity();
这⾥运⾏会报错,报错信息:
java.lang.IllegalStateException: Duplicate key com.rviceTest.ServiceTest$Boy@6c6017b9
at java.util.stream.Collectors.lambda$throwingMerger$106(Collectors.java:133)
at java.util.stream.Collectors$$Lambda$4/38625764.apply(Unknown Source)
at java.(HashMap.java:1245)
at java.util.stream.Collectors.lambda$toMap$164(Collectors.java:1320)
at java.util.stream.Collectors$$Lambda$6/940448009.accept(Unknown Source)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.pyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
申请入党at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.llect(ReferencePipeline.java:499)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:483)
stng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
stng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:182)
at stng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:171) flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
我和体育
at flect.Method.invoke(Method.java:483)
stng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:194)
stng.internal.Invoker.invokeMethod(Invoker.java:707)
stng.internal.Invoker.invokeTestMethod(Invoker.java:901)
stng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
stng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
钓大鲫鱼stng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
stng.TestRunner.privateRun(TestRunner.java:767)
猜测的拼音大明宣德香炉stng.TestRunner.run(TestRunner.java:617)
超好看的小说
stng.SuiteRunner.runTest(SuiteRunner.java:334)
stng.SuiteRunner.runSequentially(SuiteRunner.java:329)
stng.SuiteRunner.privateRun(SuiteRunner.java:291)
stng.SuiteRunner.run(SuiteRunner.java:240)
stng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
stng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
stng.TestNG.runSuitesSequentially(TestNG.java:1198)
stng.TestNG.runSuitesLocally(TestNG.java:1123)
stng.TestNG.run(TestNG.java:1031)
stng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
stng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Function.identity() 换成 c->c,(k1, k2)->k2)
正确的写法:
List<Boy> llOutList = wArrayList();
Boy one = new Boy();
one.tId(34343);
llOutList.add(one);
Boy two = new Boy();
two.tId(34343);
llOutList.add(two);
Map<Integer, Boy> res = llOutList.stream().Map(Boy::getId, c->c,(k1, k2)->k2));
System.out.Set());
这样就不会报错了。
参考博客

本文发布于:2023-05-13 01:40:54,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/607159.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:报错   对象   检测   歌曲   鲫鱼
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图