【问题标题】:Find first matching entry and assign the matching entry value to variable找到第一个匹配的条目并将匹配的条目值分配给变量
【发布时间】:2020-06-28 09:45:20
【问题描述】:

想要通过使用过滤器将值分配给变量并首先查找(以避免匹配剩余并保存迭代)。

String output = ""

Map<String, Map<String, CustomObj>> finalMap = new LinkedHashMap<>();;

Map<String, CustomObj> isinMap = new HashMap<>();
isinMap.put("REL", new CustomObj("RELIANCE ISIN"));
isinMap.put("AXIS", new CustomObj("AXISBANK ISIN"));

Map<String, CustomObj> cusipMap = new HashMap<>();
cusipMap.put("CREL", new CustomObj("RELIANCE CUSIP"));
cusipMap.put("CAXIS", new CustomObj("AXISBANK CUSIP"));


finalMap.put("ISIN",isinMap);
finalMap.put("CUSIP",cusipMap);


Map<String,String> tradeDetails = new HashMap<>();
tradeDetails.put("ISIN","AXIS");
tradeDetails.put("CUSIP","AXIS2");


finalMap.entrySet().stream().filter(finalMapEntry-> {
String value = tradeDetails.get(finalMapEntry.getKey());
return finalMapEntry.getValue().containsKey(value);

        // I want following
        //output = finalMapEntry.getValue().get(value).get(); //output should be AXISBANK ISIN
    }
).findFirst();

我知道findFirst() 将返回我在Optional 中的第一个条目,但我希望将内部映射键的值分配给输出变量。 如果在第 n 次尝试中找到匹配项,是否可以在流式传输时以某种方式执行此操作并跳过其他迭代?

【问题讨论】:

  • Optional#get()?
  • @donquih0te Optional.get 将返回我 Map> 所以我必须再次迭代内部映射并匹配键并获取他的值

标签: java dictionary collections java-8


【解决方案1】:

你可以这样做:

tradeDetails.entrySet().stream()
       .map(entry -> new AbstractMap.SimpleEntry<>(entry.getValue(),
                                                   finalMap.get(entry.getKey())))
       .map(entry -> entry.getValue().get(entry.getKey()))
       .filter(Objects::nonNull)
       .map(CustomObj::getName)
       .findFirst().orElse("");

finalMap.entrySet().stream() 
         // .filter(entry -> tradeDetails.keySet().contains(entry.getKey()))
            .map(m -> m.getValue().get(tradeDetails.get(m.getKey())))
            .filter(Objects::nonNull)
            .findFirst()
            .map(CustomObj::getName)
            .orElse("");

【讨论】:

  • 两个复杂的.maps :) ...可以简化using getOrDefault
  • @Naman getOrDefault 处理缺失的条目。这个复杂的答案甚至没有。这很复杂,没有任何好处。
【解决方案2】:

使用地图?

Optional<CustomObj> cObjectOpt = finalMap.keySet().stream().filter(k->finalMap.get(k).containsKey(tradeDetails.get(k))).findFirst().map(fk->finalMap.get(fk).get(tradeDetails.get(fk)));
        if(cObjectOpt.isPresent()) {
            System.out.println(cObjectOpt.get().get()); 
        }

【讨论】:

    【解决方案3】:

    这里可以有效地利用getOrDefault API:

    String value = tradeDetails.entrySet()
            .stream()
            .map(trade -> finalMap.getOrDefault(trade.getKey(), Collections.emptyMap())
                    .get(trade.getValue()))
            .filter(Objects::nonNull)
            .findFirst() // find first non null element (existence in Map as key-value)
            .map(CustomObj::getDesc) // as you need the description
            .orElse("");
    

    【讨论】:

    • @I'mNoBody 没找到你吗?重复任何其他答案?可以分享一下链接吗?
    猜你喜欢
    • 2018-08-03
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2017-01-31
    • 2020-09-12
    • 1970-01-01
    • 2015-09-18
    相关资源
    最近更新 更多