Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在提取的实体列表中查找重复项_Java_Java 8 - Fatal编程技术网

Java 在提取的实体列表中查找重复项

Java 在提取的实体列表中查找重复项,java,java-8,Java,Java 8,我得到了上面的方法来测试,如果一个菜单已经指定了给定的食谱(如零食或食谱) 我想知道是否有更优雅/更短的方法来编写函数 您可能应该避免使用溪流 public class Menuplan { private Meal breakfast; private Meal lunch; private Meal dinner; private Recipe snack; } public class Meal { private Recipe recipe; private R

我得到了上面的方法来测试,如果一个菜单已经指定了给定的食谱(如零食或食谱)


我想知道是否有更优雅/更短的方法来编写函数

您可能应该避免使用溪流

public class Menuplan {
  private Meal breakfast;
  private Meal lunch;
  private Meal dinner;
  private Recipe snack;
}

public class Meal {
  private Recipe recipe;
  private Reicpe snack;
}

你可能应该避开溪流

public class Menuplan {
  private Meal breakfast;
  private Meal lunch;
  private Meal dinner;
  private Recipe snack;
}

public class Meal {
  private Recipe recipe;
  private Reicpe snack;
}

在一个流中完成所有工作的解决方案是

private boolean hasDuplicates(Recipe recipe) {
    for (Meal each : Arrays.asList(breakfast, lunch, dinner)) {
        if (each.getRecipe().equals(recipe) || each.getSnack().equals(recipe) {
            return true;
        }
    }
    return snack.equals(recipe);
}
三个流元素
this.早餐、this.午餐、this.晚餐
的处理方式与调用
fine.getRecipe()
fine.getSnack()
的处理方式相同,以形成一个新的流,该流与仅包含
this.snack
的单个元素流连接

anyMatch
一旦找到满足条件的元素,就会返回
true
。否则,它将返回
false

您可以考虑将一个不适合于其他模式的元素移出流操作,而不是:

private boolean hasDuplicates(Recipe recipe) {
    return Stream.concat(
        Stream.of(this.breakfast, this.lunch, this.dinner)
            .flatMap(meal -> Stream.of(meal.getRecipe(), meal.getSnack())),
        Stream.of(this.snack))
   .anyMatch(Predicate.isEqual(recipe));
}
另一种选择是

private boolean hasDuplicates(Recipe recipe) {
    return this.snack.equals(recipe) ||
        Stream.of(this.breakfast, this.lunch, this.dinner)
            .flatMap(meal -> Stream.of(meal.getRecipe(), meal.getSnack())
            .anyMatch(Predicate.isEqual(recipe));
}

在一个流中完成所有工作的解决方案是

private boolean hasDuplicates(Recipe recipe) {
    for (Meal each : Arrays.asList(breakfast, lunch, dinner)) {
        if (each.getRecipe().equals(recipe) || each.getSnack().equals(recipe) {
            return true;
        }
    }
    return snack.equals(recipe);
}
三个流元素
this.早餐、this.午餐、this.晚餐
的处理方式与调用
fine.getRecipe()
fine.getSnack()
的处理方式相同,以形成一个新的流,该流与仅包含
this.snack
的单个元素流连接

anyMatch
一旦找到满足条件的元素,就会返回
true
。否则,它将返回
false

您可以考虑将一个不适合于其他模式的元素移出流操作,而不是:

private boolean hasDuplicates(Recipe recipe) {
    return Stream.concat(
        Stream.of(this.breakfast, this.lunch, this.dinner)
            .flatMap(meal -> Stream.of(meal.getRecipe(), meal.getSnack())),
        Stream.of(this.snack))
   .anyMatch(Predicate.isEqual(recipe));
}
另一种选择是

private boolean hasDuplicates(Recipe recipe) {
    return this.snack.equals(recipe) ||
        Stream.of(this.breakfast, this.lunch, this.dinner)
            .flatMap(meal -> Stream.of(meal.getRecipe(), meal.getSnack())
            .anyMatch(Predicate.isEqual(recipe));
}

return Stream.concat(Stream.of(this.早餐、this.午餐、this.晚餐)、flatMap(膳食->Stream.of(dime.getRecipe()、dime.getsnakp())、Stream.of(this.snakp)).anyMatch(谓词.isEqual(配方))
而不是使用arraylist use
hashSet
并重写其equals&hashcode方法,它将节省此计算的时间。
返回Stream.concat(Stream.of(this.早餐,this.午餐,this.晚餐)。flatMap(餐->Stream.of(餐.getRecipe(),餐.getsnakp()),Stream.of(this.snakp)).anyMatch(谓词.isEqual(食谱))
不使用arraylist use
hashSet
并重写其equals&hashcode方法,这将节省此计算的时间。我为什么要避免流?您要求的是一种更短、更优雅的方法。在这种情况下,流对你没有帮助。实际上我同意,这读起来既漂亮又简洁。不利的一面是,这将很难提高,特别是当未来需要增加更多或更多的条件时。当你将你的代码与霍尔格评论中的方法进行比较时,你会发现他可以很容易地添加新的“东西”,比如额外的零食等等。。。只需在流中添加新元素。我为什么要避免流?你要求的是一种更短、更优雅的方式。在这种情况下,流对你没有帮助。实际上我同意,这读起来既漂亮又简洁。不利的一面是,这将很难提高,特别是当未来需要增加更多或更多的条件时。当你将你的代码与霍尔格评论中的方法进行比较时,你会发现他可以很容易地添加新的“东西”,比如额外的零食等等。。。只需将新元素添加到流中即可。非常整洁。这绝对是最好的答案,应该接受!真漂亮。这绝对是最好的答案,应该接受!