在Stream.map中未调用put方法 [英] put method not called in Stream.map

查看:164
本文介绍了在Stream.map中未调用put方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的代码无法正常工作,因此在这里我需要一些帮助.我有一个Set<Confirmation> confirmationSetList<Import> importsListMap<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.

我想要实现的是:

  1. 使用流循环遍历自定义对象集(Set<Confirmation>) confirmationSet
  2. 在每次迭代中,我想从每个 String
  3. 的Confirmation对象中 getConfirmationId()
  4. 基于 confirmationId 获取Verification对象
  5. 从给定的 Verification 对象中获得List<Import> importsList
  6. 使用流迭代通过List<Import> importsList
  7. 检查给定的import.getName()是否已包含在comprehensiveMap
  8. 如果已经存在,则引发异常.
  9. 如果不存在,则将这些属性添加到地图中comprehensiveMap.put(import.getName(), confirmationId))
  1. Iterate through the Set of custom objects (Set<Confirmation>) confirmationSet using streams
  2. In every iteration I want to getConfirmationId() from each Confirmation object which is a String
  3. Based on confirmationId get Verification Object
  4. From the given Verification object get List<Import> importsList
  5. Iterate through List<Import> importsList using streams
  6. Check if given import.getName() is already contained in the comprehensiveMap
  7. If it's already present, throw an Exception.
  8. If not present add to the map these propertiescomprehensiveMap.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆