I'm trying to iterate over a list of a list of elements to test for a given condition. While it is possible with Java 7, I'm interested to know how to do it with Java 8 using the Stream API.
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) { //"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;
}
}
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);
}
}
A Team contains a list of Player
s, a Player
can play in many positions.
Question:
Given a team and a list of positions, I need to know if every position of the list can be played at least by one player.
I have a solution with Java 7 that goes through all positions and checks if at least one player can play in it.
I'm interested in a Java 8 solution using a stream.
Use flatMap to combine all the players’ positions into a single Stream:
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));
}