Java-Stream - 使用过滤器和 forEach 填充集合

标签 java collections java-stream side-effects java-16

我有一个Set<Person>我需要过滤内容,然后从给定的集合中填充另一个集合。

我对流还很陌生。

我能想到的第一个解决方案是链接 stream().filter().foreach()

解决方案 1:

private Set<Person> mapPerson(final Set<Student> student) {
        final Set<Person> person = new HashSet<>();
        student.stream()
            .filter(student1 -> Objects.nonNull(student.getAge()))
            .forEach(student1 -> person.add(Person.builder()
                .id(student1.getId())
                .status(student1.getStatus()))
                .build()));

        return person;
    }

但我不确定这是否可以通过链接 stream().filter().map() 来完成?

希望得到任何帮助,如果可以两种方式都可以,哪一种是首选方式?

最佳答案

为此,您不需要手动创建一个集合然后对其进行变异。您可以获得一个集合作为流管道执行的结果。

can somehow be done by chaining stream().filter().map() ?

首先改造学生流Stream<Student>进入人物对象流 Stream<Person>通过应用 map() 操作,它需要一个将流的元素转换为所需类型的另一个对象的函数。因此,您需要放置创建 Person 的逻辑。 forEach() 内部使用的对象进入map() .

然后申请collect作为参数传递Collectors.toSet() .

private Set<Person> mapPerson(final Set<Student> students) {

  return students.stream()
          .filter(student -> Objects.nonNull(student.getAge()))
          .map(student -> Person.builder()
                          .id(student.getId())
                          .status(student.getStatus())
                          .build())
          .collect(Collectors.toSet());
}

if it is possible to do it both ways, which is the preferred way?

documentation 不鼓励您正在使用的方法。 。看看下面的引用(强调是我的)。

As an example of how to transform a stream pipeline that inappropriately uses side-effects to one that does not, the following code searches a stream of strings for those matching a given regular expression, and puts the matches in a list.

 ArrayList<String> results = new ArrayList<>();
 stream.filter(s -> pattern.matcher(s).matches())
       .forEach(s -> results.add(s));  // Unnecessary use of side-effects!

... the forEach() can simply be replaced with a reduction operation that is safer, more efficient, and more amenable to parallelization:

 List<String> results =
     stream.filter(s -> pattern.matcher(s).matches())
           .toList();  // No side-effects!

您不应该使用forEach (以及 peek )只有在有其他方法可以达到相同结果的情况下才会产生副作用。

在这种情况下,您可以使用 collect() 来完成此操作(如上所示)或使用简单的 for循环。

关于Java-Stream - 使用过滤器和 forEach 填充集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71489557/

相关文章:

java - 同一类位于多个 jar 中

java - 在opencv java包装器中使用方法convertTo

java - 如何在牛轧糖中以编程方式获取 SdCard cid 号码?

python - 元组的 SortedCollection,其中 'contains' 仅考虑每个元组的第一部分

java - 在Java中对单个流的元素执行多个不相关的操作

java - 映射几个流

java - iBatis - 加载对象列表

ios - 检索 UserDefaults 后刷新 viewdidload 上的 collectionView

java - 使用干净的代码基于 "template" HashMap 动态创建 HashMap

java - 标准 Kotlin 库中有哪些 Java 8 Stream.collect 等效项?