【问题标题】:How to select only certain items from a collection using Streams in Java [closed]如何使用 Java 中的 Streams 从集合中仅选择某些项目 [关闭]
【发布时间】:2020-07-15 19:42:06
【问题描述】:

我正在尝试使用流从集合中仅选择名称:这些是条目:

Person{id=4, name=E, color=red}] 
[Person{id=0, name=A, color=blue}
Person{id=1, name=B, color=green}
Person{id=2, name=D, color=pink}
Person{id=0, name=C, color=yellow 

我尝试过使用 map,但它似乎并没有改变列表,因为我将它映射到的函数应该是一个 getter 方法。

list.stream().map(p -> getter.apply(p))collect(Collectors.toList());

【问题讨论】:

  • 使用filter 操作..?
  • 提供更多信息,使用提供的详细信息无法解决您的问题。
  • @bunny 请edit你的问题澄清你到底想要什么。包括一个 minimal reproducible example 演示问题以及示例输入和输出。
  • @bunny 您的问题已经说明了这一点,因此如果我们已经感到困惑,显然使用相同的词重述您的问题并不能帮助澄清事情(您的问题标题建议“过滤”但您的问题正文建议“地图”)。请注意,我们都在这里说代码;提供minimal reproducible example 和示例输入和输出将有助于我们理解。
  • collect(toList()) 返回一个新列表。它不会更新您的原始列表。您忽略了返回值。

标签: java collections java-stream


【解决方案1】:

如果您为 Person 的字段定义了 getter,则可以映射名称:

List<String> personNames = list.stream().map(Person::getName).collect(Collectors.toList());

【讨论】:

    【解决方案2】:

    使用.filter,就像网站向您解释的那样,而不是.map

    【讨论】:

      【解决方案3】:

      要过滤name 上的say,请这样做。

      Person{id=4, name=E, color=red}] 
      [Person{id=0, name=A, color=blue}
      Person{id=1, name=B, color=green}
      Person{id=2, name=D, color=pink}
      Person{id=0, name=C, color=yellow 
      

      假设List&lt;Person&gt;

      获取所有具有特定名称的 Person 对象

      List<Person> specific = list
                         .stream()
                         .filter(person->person.name.equals("E"))
                         .collect(Collectors.toList());
      
      

      仅获取名称

      List<String> names = list
                     .stream()
                     .map(person->person.name)
                     .collect(Collectors.toList());
      

      【讨论】:

      • 我不确定这就是 OP 想要的。更像是 Alex Rudenko 的回答
      • 是吗?它仍然说他们只想选择名称,对吧?
      【解决方案4】:

      这是一个使用过滤器的通用代码:

      import java.util.ArrayList;
      import java.util.List;
      import java.util.stream.Collectors;
      
      class Person {
        int id;
        String name;
        String color;
        
        @Override
        public String toString() {
          // TODO Auto-generated method stub
          return id + ":" + name + ":" + color;
        }
        public Person(int id, String name, String color) {
          super();
          this.id = id;
          this.name = name;
          this.color = color;
        }
        public int getId() {return id;}
        public void setId(int id) {this.id = id;}
        public String getName() {return name;}
        public void setName(String name) {this.name = name;}
        public String getColor() {return color;}
        public void setColor(String color) {this.color = color;}
      }
      
      public class Test {
        
        public static void main(String[] args) {
          List<Person> list = new ArrayList<Person>();
          list.add(new Person(1,"Ana","red"));
          list.add(new Person(2,"tna","orange"));
          list.add(new Person(3,"Hna","green"));
          list.add(new Person(4,"Ana","blue"));
          
          List<Person> t = list.stream().filter(p->p.getName().equals("Ana")).collect(Collectors.toList());
          System.out.println(t);
        }
      
      }
      

      对于这个特定的问题,建议使用过滤器而不是地图。因为我们只对过滤流以获得所需的输出感兴趣。 另一方面,如果我们需要进一步操作数据,我们可以在终端操作之前应用更多功能,则在流上使用映射操作(提供示例)。

      通过使用映射,您可以转换对象值。

      map 操作允许我们应用一个函数,该函数接受一种类型的参数,并返回其他值。

      举个例子:

      Stream students = persons.stream()
            .filter(p -> p.getAge() > 18)
            .map(new Function<Person, Customer>() { 
                        @Override
                        public Customer apply(Person person) {
                           return new Customer(person);
                        }
                    });
      

      Filter 用于过滤数据,它总是返回布尔值。如果返回 true,则将该项目添加到列表中,否则将其过滤掉。

      要深入了解,请参阅文档 link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        • 2010-10-26
        • 2018-10-02
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        相关资源
        最近更新 更多