Skip to content

Instantly share code, notes, and snippets.

@rdp
Created February 21, 2018 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdp/6afd7ee4d9a0daee3b734afc47578dc9 to your computer and use it in GitHub Desktop.
Save rdp/6afd7ee4d9a0daee3b734afc47578dc9 to your computer and use it in GitHub Desktop.
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;
}
}
@rdp
Copy link
Author

rdp commented Feb 21, 2018

List<Integer> b = Arrays.asList(2,3,4);
java.util.stream.Stream<Integer> stream = b.stream();
Optional<Integer> maxy = b.stream().collect(Collectors.maxBy((b1, b2) -> 3));
//Optional<Integer> maxz = b.stream().collect(joining());  // string only
Optional<Integer> max = b.stream().max(Integer::compareTo);
Optional<Integer> max2 = b.stream().reduce(Integer::compareTo);
Integer max3 = b.stream().collect(reducing(0, Integer::compareTo));
List<Integer> collect = b.stream().collect(toList());

maxy.ifPresent(System.out::println);

//max.ifPresent(System.out::println);
b.stream().count();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment