程序题:
如下代码会如何进行输出:
public static void main(String[] args) {
HashMap<Object, Object> map = Maps.newHashMap();
map.put("1", null);
map.put("2", null);
Collection<Object> values = map.values();
System.out.println(values);
values.remove(null);
System.out.println(values);
System.out.println(values.size());
values.remove(null);
System.out.println(values);
System.out.println(values.size());
}
输出:
[null, null]
[null]
1
[]
0
结果出乎我的意料,
起因:
第一次异常信息:
java.lang.NullPointerException
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[?:1.8.0_73]
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1612) ~[?:1.8.0_73]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[?:1.8.0_73]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[?:1.8.0_73]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[?:1.8.0_73]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:1.8.0_73]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[?:1.8.0_73]
at com.ejlerp.pms.provider.service.UniqueCodeServiceImpl.translateSkuInfo(UniqueCodeServiceImpl.java:78) ~[?:?]
第一次bugfix:
无效果依旧报错
java.lang.NullPointerException
at com.ejlerp.pms.provider.service.UniqueCodeServiceImpl.lambda$translateSkuInfo$111(UniqueCodeServiceImpl.java:81) ~[?:?]
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174) ~[?:1.8.0_73]
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1612) ~[?:1.8.0_73]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[?:1.8.0_73]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[?:1.8.0_73]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[?:1.8.0_73]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:1.8.0_73]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[?:1.8.0_73]
at com.ejlerp.pms.provider.service.UniqueCodeServiceImpl.translateSkuInfo(UniqueCodeServiceImpl.java:81) ~[?:?]
第二次bugfix:
第三次bugfix:
结论:
需要探究HashMap的内部实现,待补充