我正在尝试使用流重构一些不那么优雅的代码。我有一个包含字符串和MyObjects的HashMap,并且当前使用for循环迭代它,如下所示:
Map<String, MyObject> map = new HashMap<>();
Map<String, MyObject> objectsToAdd = new HashMap<>();
for(MyObject object : map.values()){
String idToAdd = object.getConnectedToId();
if(StringUtils.isEmpty(idToAdd) {
continue;
}
if(idToAdd.substring(0,1).equals("i")){ // connected to an ICS
MyObject newObject = service1.someMethod(idToAdd);
if(newObject != null) {
objectsToAdd.put(newObject.getId(), newObject);
}
} else if (idToAdd.substring(0,1).equals("d")){ // connected to a device
MyObject newObject = service2.someMethod(idToAdd);
if(newObject != null) {
objectsToAdd.put(newObject.getId(), newObject);
}
}
}
map.putAll(objectsToAdd);
由于我只关心id,所以我首先使用map操作来获取id,然后使用filter操作来消除空ID。
下一部分是我遇到的麻烦。我尝试的第一件事是使用Collectors groupingBy操作,以便我可以根据id的第一个字符对项目进行分组,最后我得到了这个:
map.values().stream()
.map(myObject -> myObject.getConnectedToId()) // get a map of all the ids
.filter(StringUtils::isNotEmpty) // filter non empty ones
.collect(
Collectors.mapping(
MyObject::getId,
Collectors.toList())),
Collectors.groupingBy(
s -> s.substring(0,1));
此链接有助于使用Stream收集器减少:Stream Reduction
我们在这段代码中至少有两个问题:1)collect是一个terminal operation,它将关闭流,我们还没有完成,2)我们仍然需要原始对象,但现在已经减少了到connectedToIds的地图。
Q1)是否有中间操作允许我们根据id的第一个字符对对象进行分组?
Q2)如何在不将收集仅减少到ID的情况下完成此操作?
Q3)最后,一旦集合被分组(将有两个),我们如何在原始代码中对每个组执行单独的功能?
最终解决方案(感谢@Holger&amp; @Flown提供帮助)
Map<Character, Function<String, MyObejct>> methodMapping = new HashMap<>();
methodMapping.put('i', service1::method1);
methodMapping.put('d', service2::method2);
Map<String, MyObject> toAdd = map.values().stream().map(MyObject::getConnectedToId)
.filter(StringUtils::isNotEmpty)
.map(id -> methodMapping.getOrDefault(id.charAt(0), i -> null).apply(id))
.filter(Objects::nonNull)
.collect(Collectors.toMap(MyObject::getId, Function.identity(), (mo1, mo2) -> mo2));
map.putAll(toAdd);
为了避免并发修改异常,有必要在执行流操作时首先将对象存储在临时映射中,然后一旦完成,将它们添加到最终映射中。
答案 0 :(得分:3)
您的Stream
方法和您的常用方法在退货类型方面有很大差异。因此,我将您之前的方法转换为Stream
API。
要减少部分代码,首先应构建Map<Character, Function<String, MyObject>>
以在映射步骤中进行简明查找。
看起来像这样:
Map<Character, Function<String, MyObject>> serviceMapping = new HashMap<>();
serviceMapping.put('i', service1);
serviceMapping.put('d', service2);
管道如何运作?
MyObject
- &gt; MyObject::getConnectedToId
Strings
serviceMap
中执行查找。如果存在,则返回Function<String, MyObject>
,否则返回id -> null
null
值Map<String, MyObject> toAdd = map.values().stream().map(MyObject::getConnectedToId)
.filter(StringUtils::isEmpty)
.map(id -> serviceMapping.getOrDefault(id.charAt(0), i -> null).apply(id))
.filter(Objects::nonNull)
.collect(Collectors.toMap(MyObject::getId, Function.identity(), (mo1, mo2) -> mo2));
map.putAll(toAdd);
也可以使用map
操作将计算值直接添加到forEach
。
map.values().stream().map(MyObject::getConnectedToId)
.filter(StringUtils::isEmpty)
.map(id -> serviceMapping.getOrDefault(id.charAt(0), i -> null).apply(id))
.filter(Objects::nonNull)
.forEach(mo -> map.put(mo.getId(), mo));