-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Kervin edited this page May 31, 2019
·
13 revisions
Welcome to the complex-system wiki!
- Java8 lambda 使用Collectors.toMap方法时的两个问题:
- 1)当key重复时,会抛出异常:java.lang.IllegalStateException: Duplicate key **
- 2)当value为null时,会抛出异常:java.lang.NullPointerException
以下两种解决方式
List<RealtimeTag> tags;
tagValues = tags.stream().filter(tag->StringUtils.isNotBlank(tag.getTag())&&Objects.nonNull(tag.getValue())).collect(Collectors.toMap(RealtimeTag::getTag, RealtimeTag::getValue,(v1,v2)->v2));
tagValues = tags.stream().collect(Collector.of(HashMap::new, (m,tag)->m.put(tag.getTag(),tag.getValue()), (v1,v2)->v2, Characteristics.IDENTITY_FINISH));
以上均属自主研发,版权归个人所属