Created
February 21, 2018 21:17
-
-
Save rdp/6afd7ee4d9a0daee3b734afc47578dc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Comparator<String> byLength = Comparator.comparing(String::length); | |
Map<String, String> longestLastNameByCity | |
= Arrays.asList(new Person("pack", "provo"), new Person("pack2", "provo"), new Person("pack", "oreM")).stream().collect(groupingBy(Person::getCity, | |
reducing("", Person::getLastName, BinaryOperator.maxBy(byLength)))); | |
System.out.println(longestLastNameByCity); | |
} | |
public static class Person { | |
String lastName; | |
String City; | |
public Person(String lastName, String City) { | |
this.lastName = lastName; | |
this.City = City; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public String getCity() { | |
return City; | |
} | |
} |
Author
rdp
commented
Feb 21, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment