Skip to content

Instantly share code, notes, and snippets.

@gabfssilva
Created July 4, 2017 13:16
Show Gist options
  • Save gabfssilva/19a8530ab4c1eed635fcdcae4bf986d4 to your computer and use it in GitHub Desktop.
Save gabfssilva/19a8530ab4c1eed635fcdcae4bf986d4 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
class Player {
Integer id;
String team;
public Player(Integer id, String team) {
this.id = id;
this.team = team;
}
public Integer getId() {
return id;
}
public String getTeam() {
return team;
}
@Override
public String toString() {
return "{" + "player.id=" + id + ", player.team='" + team + '\'' + '}';
}
}
final List<Player> players =
Arrays
.asList(new Player(1, "palmeiras"),
new Player(2, "palmeiras"),
new Player(3, "sp"),
new Player(4, "palmeiras"),
new Player(5, "palmeiras"),
new Player(6, "sp"),
new Player(7, "sem time"),
new Player(8, "sem time"),
new Player(9, "sp"));
final Map<String, List<Player>> teams = players.stream().collect(Collectors.groupingBy(p -> p.getTeam()));
System.out.println("sem time: " + teams.get("sem time"));
System.out.println("palmeiras: " + teams.get("palmeiras"));
System.out.println("sp: " + teams.get("sp"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment