如何使用Java Stream来过滤对象?

huangapple go评论87阅读模式
英文:

How to use java stream to filter object?

问题

以下是翻译好的内容:

如何在Java中使用过滤器来过滤完整对象。
我有一个包含nameamount的ArrayList,还有另一个包含一些字符串的字符串数组。

现在,如何过滤并创建一个新的ArrayList,其中只包含具有arr元素的这两个对象。

我尝试过使用 .equals.contains 两种方法,但都没有产生输出。

List<Person> people = new ArrayList<>();
people.add(new Person("Warren Buffett", 90));
people.add(new Person("Jeff Bezos", 180));
people.add(new Person("Bill Gates", 140));
people.add(new Person("Mark", 120));

String arr[] = {"Mark", "Bill Gates"};

List<Person> newPeople = people.stream().filter(person -> Arrays.asList(arr).contains(person.name)).collect(Collectors.toList());

newPeople.forEach(person -> System.out.println(person.name));

编辑:

我应该得到一个对象数组的输出,其中应该包含Mark和Bill Gates的姓名和他们的金额。

英文:

How to use the filter in java to filter the complete object.
I am having an ArrayList that contains name and amount, I have another array of string that contains some string.

Now here how to filter and create a new ArrayList which contain only these two object which has arr elements.

I tried to use two methods .equals and .contains but it does not produce output.

List&lt;Person&gt; people = new ArayList&lt;&gt;();
people.add(new Person(&quot;Warren Buffett&quot; , 90));
people.add(new Person(&quot;Jeff Bezos&quot; , 180));
people.add(new Person(&quot;Bill Gates&quot; , 140));
people.add(new Person(&quot;Mark&quot; , 120));

String arr[]={&quot;Mark&quot; , &quot;Bill Gates&quot;};

List&lt;Person&gt; newPeople = people.stream().filter(person -&gt; person.name.equals(arr))
List&lt;Person&gt; newPeople = people.stream().filter(person -&gt; person.name.contains(arr)).collect(collectors.toList());

newPeople.forEach(person -&gt; System.out.println(person.name));

Edit:

I should get the output as an array of an object that should contain Mark and Bill gates name and their amount

答案1

得分: 1

下面是一个可行的解决方案。使用流来迭代你的参数数组,以找到第一个匹配的参数。

List<Person> newPeople = people.stream()
        .filter(person -> Stream.of(arr).anyMatch(s -> person.name.equals(s)))
        .collect(Collectors.toList());

newPeople.forEach(person -> System.out.println(person.name));
// Bill Gates
// Mark
英文:

Below you can find working solution. Use streams to iterate by your args array to find first matching arg.

List&lt;Person&gt; newPeople = people.stream()
            .filter(person -&gt; Stream.of(arr).anyMatch(s -&gt; person.name.equals(s)))
            .collect(Collectors.toList());



newPeople.forEach(person -&gt; System.out.println(person.name));
// Bill Gates
// Mark

答案2

得分: 0

你不能这样做 person.name.contains(arr),而应该这样做 person.name.contains(arr[0]) 或者 person.name.contains(arr[1])
为了实现这一点,你可能需要进行嵌套迭代。

英文:

You cannot do this person.name.contains(arr), rather you should be doing person.name.contains(arr[0]) OR person.name.contains(arr[1]).<br>
In order to do so, you might want to do nested iteration.

答案3

得分: 0

你可以将数组转换为列表,然后使用 List.contains() 来检查 person.name 是否在该列表中。

首先进行数组转换。

String arr[] = {"Mark", "Bill Gates"};
List<String> names = Arrays.asList(arr);

现在,使用 List.contains()

List<Person> newPeople = people.stream()
    .filter(person -> names.contains(person.name))
    .collect(Collectors.toList());

以下是更新后的完整方法:

public static void main(String[] args) {

    List<Person> people = new ArrayList<>();
    people.add(new Person("Warren Buffett", 90));
    people.add(new Person("Jeff Bezos", 180));
    people.add(new Person("Bill Gates", 140));
    people.add(new Person("Mark", 120));

    String arr[] = {"Mark", "Bill Gates"};
    List<String> names = Arrays.asList(arr);

    List<Person> newPeople = people.stream()
        .filter(person -> names.contains(person.name))
        .collect(Collectors.toList());

    newPeople.forEach(person -> System.out.println(person.name));

}
英文:

You can convert the array to a list; then use the List.contains() to check if the person.name is available in that list.

First convert the array.

String arr[]={&quot;Mark&quot; , &quot;Bill Gates&quot;};
List&lt;String&gt; names = Arrays.asList(arr);

Now, use the List.contains()

List&lt;Person&gt; newPeople 
   = people.stream().filter(
                             person -&gt; names.contains(person.name)).collect(Collectors.toList()
                           );

Below is the full method after the update.

public static void main(String[] args) {

    List&lt;Person&gt; people = new ArrayList&lt;&gt;();
    people.add(new Person(&quot;Warren Buffett&quot; , 90));
    people.add(new Person(&quot;Jeff Bezos&quot; , 180));
    people.add(new Person(&quot;Bill Gates&quot; , 140));
    people.add(new Person(&quot;Mark&quot; , 120));

    String arr[]={&quot;Mark&quot; , &quot;Bill Gates&quot;};
    List&lt;String&gt; names = Arrays.asList(arr);

    List&lt;Person&gt; newPeople = people.stream().filter(person -&gt; names.contains(person.name)).collect(Collectors.toList());

    newPeople.forEach(person -&gt; System.out.println(person.name));

}

答案4

得分: -3

所以你想要一个新的列表,只包含在arr中出现过名字的Person实例?尝试这样做:

List<Person> newPeople = people.stream()
    .filter(person -> arr.contains(person.name))
    .collect(Collectors.toList());

lambda变量"person"指的是people列表中包含的单个对象,而不是列表本身。因此,当你使用person.name.equals(...)或person.name.contains(...)时,你在一个字符串上调用这些方法。像我的示例中那样反过来做,将会得到你想要的结果;如果不是,请让我知道,我们会深入探讨一下。

编辑:

我假设arr是一个List,所以在使用我的代码之前,你应该考虑执行以下操作:

List<String> names = Arrays.asList(arr);

然后

List<Person> newPeople = people.stream()
    .filter(person -> names.contains(person.name))
    .collect(Collectors.toList());

假设你是通过其他方式传递这个数组的,如果你是在创建它,你可以直接创建一个列表,跳过数组的“步骤”。

英文:

So you want a new list containing only the Person instancences which names appears in arr? Try do this:

List&lt;Person&gt; newPeople = people.stream()
    .filter(person -&gt; arr.contains(person.name))
    .collect(collectors.toList());

The lambda variable "person" is referring to a single object contained in the people list, not to the list itself. So when you use person.name.equals(...) or person.name.contains(...) your are calling those methods on a string. Doing the reverse, as in my example, will give you the desired result; if not, let know and we'll dig dipper

edit:

I was assuming arr was a List, so before using my code you should consider to do te following

List&lt;String&gt; names = Arrays.asList(arr);

And then

List&lt;Person&gt; newPeople = people.stream()
.filter(person -&gt; names.contains(person.name))
.collect(collectors.toList());

Assuming you're having that array passed by something else, if you're creating it you could just create a list and skipping the array "step"

huangapple
  • 本文由 发表于 2020年10月27日 17:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64551119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定