英文:
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 Player
s - List<Player>
. 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 "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 > 50)
.collect(Collectors.toList());
if(filtered == null) {
System.out.println("No Player selected for World Cup");
} 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<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("No Player selected for World Cup");
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<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("No Player selected for World Cup");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论