在Stream.map中未调用put方法 [英] put method not called in Stream.map
问题描述
我下面的代码无法正常工作,因此在这里我需要一些帮助.我有一个Set<Confirmation> confirmationSet
,List<Import> importsList
,Map<String, Verification> verificationMap
和一个Map<String, String> comprehensiveMap
.
I have a code below which is not working as expected so I would need a little help here. I have a Set<Confirmation> confirmationSet
, List<Import> importsList
, Map<String, Verification> verificationMap
and a Map<String, String> comprehensiveMap
.
我想要实现的是:
- 使用流循环遍历自定义对象集
(Set<Confirmation>) confirmationSet
- 在每次迭代中,我想从每个 String 的Confirmation对象中 getConfirmationId()
- 基于 confirmationId 获取
Verification
对象 - 从给定的 Verification 对象中获得
List<Import> importsList
- 使用流迭代通过
List<Import> importsList
- 检查给定的import.getName()是否已包含在
comprehensiveMap
中
- 如果已经存在,则引发异常.
- 如果不存在,则将这些属性添加到地图中
comprehensiveMap.put(import.getName(), confirmationId))
- Iterate through the Set of custom objects
(Set<Confirmation>) confirmationSet
using streams - In every iteration I want to getConfirmationId() from each Confirmation object which is a String
- Based on confirmationId get
Verification
Object - From the given Verification object get
List<Import> importsList
- Iterate through
List<Import> importsList
using streams - Check if given import.getName() is already contained in the
comprehensiveMap
- If it's already present, throw an Exception.
- If not present add to the map these properties
comprehensiveMap.put(import.getName(), confirmationId))
我尝试过的
confirmationSet.stream()
.map(Confirmation::getConfirmationId)
.map(confId-> verificationMap.get(confId))
.map(verifObj-> verifObj.getImportList())
.stream()
.peek(import -> Optional.of(testMap.containsKey(import.getName()))
.orElseThrow(() -> new CustomException("Map already contains this key")))
.map(import -> comprehensiveMap.put(import.getName(), confirmationId)));
运行此代码后,comprehensiveMap
为空..我在做什么错?
After running this code comprehensiveMap
is empty.. What am I doing wrong?
我的原始代码是:
confirmationSet.foreach(confirmation -> {
String confirmationId = confirmation.getConfirmationId();
Verification verification = verificationMap.get(confirmationId);
ImportList importList = verification.getImportList;
importList.foreach(import -> {
if(!testMap.containsKey(import.getName()){
comprehensiveMap.put(import.getName(), confirmationId));
} else {
throw new CustomException("Map already contains this key")
}
}
});
但是我想让它更漂亮. 谢谢
But I wanted to make it more fancier.. Thanks
推荐答案
map
不是终端操作,它用于获取一个对象并将其变成另一个对象.如果要在传递的函数之外修改某些内容,则可能不应该使用它.仅仅是因为Java Maps的设计方式,put
方法返回一个对象,但这并不总是正确的.要实际运行您的功能,您需要收集Stream或执行其他一些终端操作.
map
is not a terminal operation, it's for taking one object and turning it into another. If you want to modify something outside your passed function, you probably shouldn't use it. It's just because of the way Java Maps are designed that the put
method returns an object, but this won't always be true. To actually run your function, you'd need to collect the Stream or do some other terminal operation.
这是终端操作的列表(源 ):
Here's a list of terminal operations (Source):
- toArray()
- collect()
- count()
- reduce()
- forEach()
- forEachOrdered()
- min()
- max()
- anyMatch()
- allMatch()
- noneMatch()
- findAny()
- findFirst()
- toArray()
- collect()
- count()
- reduce()
- forEach()
- forEachOrdered()
- min()
- max()
- anyMatch()
- allMatch()
- noneMatch()
- findAny()
- findFirst()
但是,我认为map
不是您想要的.由于您要遍历流并为每个元素执行操作,因此应改用forEach
,这在此处最为合适.它将运行您在此处提供的功能. forEach
需要一个Consumer
,所以您应该给它提供不纯"功能,这些功能可以修改外部内容,例如您放入map
的lambda.
However, I think map
is not what you want here. Since you want to go through the stream and perform an action for each element, you should use forEach
instead, which fits best here. It will run the function that you give it right there. forEach
takes a Consumer
, so you're kinda supposed to give it "impure" functions that modify stuff outside, like the lambda you put in your map
.
我还建议顺便将前几个map
调用组合成一个.
I'd also suggest composing those first few map
calls into one, by the way.
我认为您的代码现在应该看起来像这样:
I think your code should look something like this now:
confirmationSet.stream()
.map(Confirmation::getConfirmationId)
.forEach(confId ->
verificationMap
.get(confId)
.getImportList()
.stream()
.forEach(impt -> {
if (!comprehensiveMap.containsKey(impt))
comprehensiveMap.put(impt);
else throw new CustomException();
})
);
这篇关于在Stream.map中未调用put方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!