在Java 8流的过滤中出现了if-else逻辑问题。

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

if-else logic issue in java 8 stream for filter

问题

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Arrays;

public class Player {
    String name;
    int age;
    int runs;

    public Player(String name, int age, int runs) {
        super();
        this.name = name;
        this.age = age;
        this.runs = runs;
    }

    @Override
    public String toString() {
        return "Player [name=" + name + "]";
    }
}

// Logic
List<Player> players = Arrays.asList(
    new Player("Sachin", 45, 10252),
    new Player("Virat", 29, 7000),
    new Player("Dhoni", 38, 4000),
    new Player("Yuvraj", 29, 5500)
);

List<Player> filtered = players.stream()
    .filter(i -> i.runs > 5000 && i.age < 30)
    .collect(Collectors.toList());

if (filtered.isEmpty()) {
    System.out.println("No player selected for world cup");
} else {
    System.out.println(filtered.get(0).name);
}
英文:

Question:

Give a ArrayList of Players - List&lt;Player&gt;. Take a Player POJO with String Name, int age, and int runs. Build a list using list.add.

Find the first player whose age is less than 30 and Runs greater than 5000 and print the name. or else print "No player selected for world cup"

POJO class:

public class Player {
	String name;
	int age;
	int runs;
	
	public Player(String name, int age, int runs) {
		super();
		this.name = name;
		this.age = age;
		this.runs = runs;
	}
	
	@Override
	public String toString() {
		return &quot;Player [name=&quot; + name + &quot;]&quot;;
	}
}

Logic:

List&lt;Player&gt; players = Arrays.asList(
	new Player(&quot;Sachin&quot;,45,10252),
	new Player(&quot;Virat&quot;,29,7000),
	new Player(&quot;Dhoni&quot;,38,4000),
	new Player(&quot;Yuvraj&quot;,29,5500)
);

List&lt;Player&gt; filtered = players.stream()
	.filter(i -&gt; i.runs &gt; 5000 &amp;&amp; i.age &gt; 50)
	.collect(Collectors.toList());

if(filtered == null) {
	System.out.println(&quot;No Player selected for World Cup&quot;);
} else {
	System.out.println(filtered);
}

here the if part is not executing, else is running fine for valid conditions

答案1

得分: 5

使用collect(Collection.toList())时,返回的列表永远不会为null。如果存在没有匹配的参数的过滤器,则列表为空。您应将条件更改为filtered.isEmpty()

但请注意,流中的列表可能为null。

英文:

When using collect(Collection.toList()) the list returned is never null. If there's a filter that no arguments match, the list is empty. You should change your condition to filtered.isEmpty().

Note, however, that the list can be null in a stream.

答案2

得分: 1

这个有效:

        List<Player> players = Arrays.asList(new Player("Sachin", 45, 10252), new Player("Virat", 29, 7000), new Player("Dhoni", 38, 4000),
                new Player("Viraaat", 28, 7000), new Player("Yuvraj", 29, 5500));
        List<Player> filtered = players.stream().filter(i -> i.runs > 5000 && i.age < 30).collect(Collectors.toList());
        if (filtered.isEmpty())
            System.out.println("未选中任何球员参加世界杯");
        else
            System.out.println(filtered.get(0));
英文:

This works:

    List&lt;Player&gt; players = Arrays.asList(new Player(&quot;Sachin&quot;, 45, 10252), new Player(&quot;Virat&quot;, 29, 7000), new Player(&quot;Dhoni&quot;, 38, 4000),
            new Player(&quot;Viraaat&quot;, 28, 7000), new Player(&quot;Yuvraj&quot;, 29, 5500));
    List&lt;Player&gt; filtered = players.stream().filter(i -&gt; i.runs &gt; 5000 &amp;&amp; i.age &lt; 30).collect(Collectors.toList());
    if (filtered.isEmpty())
        System.out.println(&quot;No Player selected for World Cup&quot;);
    else
        System.out.println(filtered.get(0));

答案3

得分: 1

// collect()语句将返回一个数组对象。您必须检查该数组是否为空。

// 另外,如果您愿意,您可以使用.anyMatch()语句来获取一个关于是否与您的filter()匹配的布尔值。

List<Player> players = Arrays.asList(
    new Player("Sachin", 45, 10252),
    new Player("Virat", 29, 7000),
    new Player("Dhoni", 38, 4000),
    new Player("Yuvraj", 29, 5500)
);

boolean hasPlayers = players.stream().anyMatch(i -> i.runs > 5000 && i.age > 50);
if (!hasPlayers) {
    System.out.println("没有选择球员参加世界杯");
}
英文:

The collect() statement will return an array object. You have to check if that array is empty or not.

Also, if you like, you can use the .anyMatch() statement to get a boolean about if there is any match with your filter().

List&lt;Player&gt; players = Arrays.asList(
    new Player(&quot;Sachin&quot;,45,10252),
    new Player(&quot;Virat&quot;,29,7000),
    new Player(&quot;Dhoni&quot;,38,4000),
    new Player(&quot;Yuvraj&quot;,29,5500)
);

boolean hasPlayers = players.stream().anyMatch(i -&gt; i.runs &gt; 5000 &amp;&amp; i.age &gt; 50);
if(!hasPlayers) {
    System.out.println(&quot;No Player selected for World Cup&quot;);
}

huangapple
  • 本文由 发表于 2020年9月5日 19:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63753531.html
匿名

发表评论

匿名网友

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

确定