遍历列表列表并使用 java 8 检查条件
Iterate over a list of lists and check for a condition using java 8
我正在尝试遍历元素列表以测试给定条件,而使用 Java 7 是可能的,我很想知道如何使用 java 8 流.
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class Main2 {
static String[] positionsThatWillBeCovered = new String[]{"RB", "LB", "MD"};
static String[] positionsThatWillNotBeCovered = new String[]{"RB", "LB", "MD", "GK"};
public static void main(String[] args) {
Position p1 = new Position("RB");
Position p2 = new Position("LB");
Position p3 = new Position("MD");
Player alves = new Player(Arrays.asList(p1, p2));
Player alba = new Player(Arrays.asList(p2));
Player xavi = new Player(Arrays.asList(p3));
Team team = new Team(Arrays.asList(alves, alba, xavi));
//should print true
System.out.println(testAllPositionsAreCovered(team, positionsThatWillBeCovered));
//false
System.out.println(testAllPositionsAreCovered(team, positionsThatWillNotBeCovered));
}
private static boolean testAllPositionsAreCovered(Team team, String[] positionsToCover) {
for (String positionToCover : positionsToCover) {
boolean positionWasCovered = false;
A : for (Player player : team.getPlayerList()) {
for (Position playerPosition : player.getPositions()) {
if(playerPosition.getPosition().equals(positionToCover))
{
positionWasCovered = true;
break A;
}
}
}
if (!positionWasCovered) return false;
}
return true;
}
}
class Team {
private List<Player> playerList;
Team(List<Player> playerList){
this.playerList = playerList;
}
public List<Player> getPlayerList() {
return playerList;
}
}
class Player {
private List<Position> positions;
Player(List<Position> positions){
this.positions = positions;
}
public List<Position> getPositions() {
return positions;
}
}
class Position {
private String position;
//some other fields...
public Position(String position) {
this.position = position;
}
public String getPosition() {
return position;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position1 = (Position) o;
return Objects.equals(position, position1.position);
}
@Override
public int hashCode() {
return Objects.hash(position);
}
}
一个Team包含一个Players列表,一个Player可以打多个位置
问题:
给定一支球队和一份位置列表,我需要知道是否列表中的每个位置都至少可以由一名球员担任。
我有一个 Java 7 的解决方案,我们可以遍历所有位置并检查是否至少有一个玩家可以玩。
我对 java 8 流解决方案感兴趣。
谢谢。
编辑:
顺便说一下,在@Holger 的评论之后,我更新了我的 java 7 版本的解决方案,它是一种更好的方式。
这是我更改的部分:
private static boolean testAllPositionsAreCovered(Team team, String[] positionsToCover) {
for (String positionToCover : positionsToCover) { //"RB", "LB", "MD"
if (!isThePositionCoveredByAPlayer(team, positionToCover))
return false;
}
return true;
}
private static boolean isThePositionCoveredByAPlayer(Team team, String positionToCover) {
for (Player player : team.getPlayerList()) {
for (Position playerPosition : player.getPositions()) {
if(playerPosition.getPosition().equals(positionToCover))
return true;
}
}
return false;
}
使用flatMap将所有玩家的位置合并为一个流:
public boolean testAllPositionsAreCovered(Team team,
String[] positions) {
Set<String> positionsOfTeams = team.getPlayers().stream()
.flatMap(p -> p.getPositions().stream())
.map(Position::getPosition)
.collect(Collectors.toSet());
return positionsOfTeams.containsAll(Arrays.asList(positions));
}
我正在尝试遍历元素列表以测试给定条件,而使用 Java 7 是可能的,我很想知道如何使用 java 8 流.
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class Main2 {
static String[] positionsThatWillBeCovered = new String[]{"RB", "LB", "MD"};
static String[] positionsThatWillNotBeCovered = new String[]{"RB", "LB", "MD", "GK"};
public static void main(String[] args) {
Position p1 = new Position("RB");
Position p2 = new Position("LB");
Position p3 = new Position("MD");
Player alves = new Player(Arrays.asList(p1, p2));
Player alba = new Player(Arrays.asList(p2));
Player xavi = new Player(Arrays.asList(p3));
Team team = new Team(Arrays.asList(alves, alba, xavi));
//should print true
System.out.println(testAllPositionsAreCovered(team, positionsThatWillBeCovered));
//false
System.out.println(testAllPositionsAreCovered(team, positionsThatWillNotBeCovered));
}
private static boolean testAllPositionsAreCovered(Team team, String[] positionsToCover) {
for (String positionToCover : positionsToCover) {
boolean positionWasCovered = false;
A : for (Player player : team.getPlayerList()) {
for (Position playerPosition : player.getPositions()) {
if(playerPosition.getPosition().equals(positionToCover))
{
positionWasCovered = true;
break A;
}
}
}
if (!positionWasCovered) return false;
}
return true;
}
}
class Team {
private List<Player> playerList;
Team(List<Player> playerList){
this.playerList = playerList;
}
public List<Player> getPlayerList() {
return playerList;
}
}
class Player {
private List<Position> positions;
Player(List<Position> positions){
this.positions = positions;
}
public List<Position> getPositions() {
return positions;
}
}
class Position {
private String position;
//some other fields...
public Position(String position) {
this.position = position;
}
public String getPosition() {
return position;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position1 = (Position) o;
return Objects.equals(position, position1.position);
}
@Override
public int hashCode() {
return Objects.hash(position);
}
}
一个Team包含一个Players列表,一个Player可以打多个位置
问题: 给定一支球队和一份位置列表,我需要知道是否列表中的每个位置都至少可以由一名球员担任。
我有一个 Java 7 的解决方案,我们可以遍历所有位置并检查是否至少有一个玩家可以玩。
我对 java 8 流解决方案感兴趣。
谢谢。
编辑: 顺便说一下,在@Holger 的评论之后,我更新了我的 java 7 版本的解决方案,它是一种更好的方式。
这是我更改的部分:
private static boolean testAllPositionsAreCovered(Team team, String[] positionsToCover) {
for (String positionToCover : positionsToCover) { //"RB", "LB", "MD"
if (!isThePositionCoveredByAPlayer(team, positionToCover))
return false;
}
return true;
}
private static boolean isThePositionCoveredByAPlayer(Team team, String positionToCover) {
for (Player player : team.getPlayerList()) {
for (Position playerPosition : player.getPositions()) {
if(playerPosition.getPosition().equals(positionToCover))
return true;
}
}
return false;
}
使用flatMap将所有玩家的位置合并为一个流:
public boolean testAllPositionsAreCovered(Team team,
String[] positions) {
Set<String> positionsOfTeams = team.getPlayers().stream()
.flatMap(p -> p.getPositions().stream())
.map(Position::getPosition)
.collect(Collectors.toSet());
return positionsOfTeams.containsAll(Arrays.asList(positions));
}