如何使用lambdajava8转换代码

如何使用lambdajava8转换代码,lambda,Lambda,如何使用lambdajava8简化以下代码? 我对兰姆达还不熟悉,还在学习 public boolean isValuePresent(String id, Optional<String> value) { ClassConfig config = configStorage.getConfig(); Map<String, FeatureClass> map = config.getConfigMap(); if (ma

如何使用lambdajava8简化以下代码? 我对兰姆达还不熟悉,还在学习

public boolean isValuePresent(String id, Optional<String> value) {
        ClassConfig config = configStorage.getConfig();
        Map<String, FeatureClass> map = config.getConfigMap();
        if (map.containsKey(id)) {
            Set<String> set = map.get(id).getset();
            if (!set.isEmpty()) {
                if (value.isPresent()) {
                    if (set.contains(value.get())) {
                        log.info(String.format("value present for id %s", id));
                        return true;
                    }
                }
            }
        }
        return false;
    }
public boolean isValuePresent(字符串id,可选值){
ClassConfig=configStorage.getConfig();
Map Map=config.getConfigMap();
if(地图容器(id)){
Set Set=map.get(id).getset();
如果(!set.isEmpty()){
if(value.isPresent()){
if(set.contains(value.get())){
log.info(String.format(“id%s的值存在”,id));
返回true;
}
}
}
}
返回false;
}

此代码是否适用于您

for(Map.Entry<String, String> entry : map.entrySet()) {
     if(entry.getKey().equals(id)) {
         if (value.isPresent() && entry.getValue().equals(value.get())) {
             System.out.println(String.format("value present for id %s", id));
             return true;
         }
     }
}
return false;
编辑: 我找到了一种方法^^

return map.entrySet().stream().anyMatch(entry -> entry.getKey().equals(id) && value.isPresent() && entry.getValue().equals(value.get()));

简言之,没有。这段代码本身没有任何错误或复杂之处,可以用lambdas来解决。您应该做的是检查这段代码的实际功能,并从那里简化它。 我的看法是,如果您无法更改
isValuePresent
的签名,请执行以下操作:

boolean isValuePresent(String id, Optional<String> option) {
  if (!option.isPresent()) { // this already saves us loading the config if we've got no value to check against
    return false;
  }
  FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
  if (features == null) { // replaces containsKey().get() with one call. It is hardly an improvement, just my stylistic choice
    return false;
  }
  String value = option.get();
  if (features.getset().contains(value) { // we actually have no need to check if set is empty or not, we rather have it handle that itself
    log.info(String.format("value present for id %s", id));
    return true;
  }
  return false;
}
boolean isValuePresent(String id, String value) {
  FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
  if (features == null) {
    return false;
  }
  if (features.getset().contains(value)) {
    log.info(<snip>);
    return true;
  }
  return false;
}
String id = loadId();
Optional<String> myValue = loadMyValue(id);
return myValue.map(value -> Checker.isValuePresent(id, value)).orElse(false);
布尔值isValuePresent(字符串id,可选选项){
如果(!option.isPresent()){//如果没有可检查的值,则这已经节省了加载配置的时间
返回false;
}
FeatureClass features=configStorage.getConfig().getConfigMap().get(id);
如果(features==null){//用一个调用替换containsKey().get()。这并不是一个改进,只是我的风格选择
返回false;
}
字符串值=option.get();
如果(features.getset().contains(value){//我们实际上不需要检查集合是否为空,而是让它自己处理它
log.info(String.format(“id%s的值存在”,id));
返回true;
}
返回false;
}
或者,如果你可以更改签名,我认为最好有这样的东西:

boolean isValuePresent(String id, Optional<String> option) {
  if (!option.isPresent()) { // this already saves us loading the config if we've got no value to check against
    return false;
  }
  FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
  if (features == null) { // replaces containsKey().get() with one call. It is hardly an improvement, just my stylistic choice
    return false;
  }
  String value = option.get();
  if (features.getset().contains(value) { // we actually have no need to check if set is empty or not, we rather have it handle that itself
    log.info(String.format("value present for id %s", id));
    return true;
  }
  return false;
}
boolean isValuePresent(String id, String value) {
  FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
  if (features == null) {
    return false;
  }
  if (features.getset().contains(value)) {
    log.info(<snip>);
    return true;
  }
  return false;
}
String id = loadId();
Optional<String> myValue = loadMyValue(id);
return myValue.map(value -> Checker.isValuePresent(id, value)).orElse(false);
布尔值isValuePresent(字符串id、字符串值){
FeatureClass features=configStorage.getConfig().getConfigMap().get(id);
如果(功能==null){
返回false;
}
if(features.getset()包含(值)){
log.info();
返回true;
}
返回false;
}
这样称呼它:

boolean isValuePresent(String id, Optional<String> option) {
  if (!option.isPresent()) { // this already saves us loading the config if we've got no value to check against
    return false;
  }
  FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
  if (features == null) { // replaces containsKey().get() with one call. It is hardly an improvement, just my stylistic choice
    return false;
  }
  String value = option.get();
  if (features.getset().contains(value) { // we actually have no need to check if set is empty or not, we rather have it handle that itself
    log.info(String.format("value present for id %s", id));
    return true;
  }
  return false;
}
boolean isValuePresent(String id, String value) {
  FeatureClass features = configStorage.getConfig().getConfigMap().get(id);
  if (features == null) {
    return false;
  }
  if (features.getset().contains(value)) {
    log.info(<snip>);
    return true;
  }
  return false;
}
String id = loadId();
Optional<String> myValue = loadMyValue(id);
return myValue.map(value -> Checker.isValuePresent(id, value)).orElse(false);
String id=loadId();
可选myValue=loadMyValue(id);
返回myValue.map(value->Checker.isValuePresent(id,value)).orElse(false);
正如您所看到的,没有lambda,因为不需要它们-在这里我们没有必要改变我们的行为,在这个方法中没有可能从外部插入的策略

现在,关于我为什么选择不在参数中使用可选选项:

  • 它为方法的调用者提供了更大的灵活性。如果他们已经具有value
    String
    表单,则在调用方法之前,他们不需要将其包装为无用的
    Optional
  • 使用
    Optional
    作为方法参数类型看起来很难看,因为该类是为用作返回值而设计的,并且除了
    isPresent()
    get()
    之外,没有提供太多的参数类型。将其用作返回值时,将其与大量受控值提取方法进行比较(举几个例子,
    orElse
    orElseGet
    map
    flatMap
    ,Java 9中还有更多)
  • java.util.Optional
    不可
    序列化
    ,这就结束了通过远程接口调用您的方法(当然,这意味着您关心通过远程接口调用它)

  • 您已经完成了外部循环,并且希望使用流API,它为我们提供了专注于内部循环逻辑的能力

    在流中,有三个部分。一个是源,这里是map.keySet(),第二个是一系列中间操作,即filter()、map()等。所有操作都会创建新的流,并且不会更改源,第三个是终端操作,即forEach()、collect()、anyMatch()、allMatch()等

    流具有可消耗和延迟执行属性。这意味着,在我们使用任何终止操作之前,流尚未执行。并且,一旦应用了终止运算符,流已消耗并且不能进一步消耗

    boolean retval = map.keySet().stream()
             .filter(x -> x.equals(id))
             .filter(x -> !map.get(x).getset().isEmpty())
             .filter(x -> value.isPresent())
             .anyMatch(x -> map.get(x).getset().contains(value.get()));
    
    java8中的集合API实现了流接口。Map不是集合API的一部分,因此我们使用Map.keySet()和Application stream()获取集合。这就是我们的源代码。 过滤操作接受任何布尔表达式,这里我们比较映射键和id参数,并检查值param是否存在。
    anyMatch是终止操作,如果map中的任何键满足上述条件,它将返回true。

    对于初学者,如果您发现嵌套了两个以上的if语句,请为块创建一个新函数。为什么要将
    可选的
    作为方法输入?当然您可以事先检查它是否存在,并假设它存在“值不存在”?您迭代整个映射以查找与谓词匹配的值,如果
    value,则该谓词可能永远不会返回true。isPresent()==false
    。您读过他的代码吗?他的方法甚至被称为“isValuePresent”。他想要测试值是合乎逻辑的。isPresent()…@MikhailProkhorovsure。但是他还有一个映射,对于键查找来说可能具有
    O(1)
    复杂性,并且
    可选
    ,如果选项中没有值,他必须在逻辑上立即返回false。相反,您的解决方案会生成
    O(n)
    操作,尤其是在没有值的情况下。你的意思是我的解决方案迭代整个映射,即使可选中没有值?它迭代整个映射,尤其是在可选中没有值的情况下,因为
    anyMatch()
    在第一件事被发现后,它会收缩短路,如果你在谓词中检查它,情况就不会这样了。它会在映射上迭代,而不是像你建议的那样使用效率更高的
    map::get
    @Filnor我已经添加了流解释。要打印记录器,我们可以使用peek()操作,而