fork download
  1.  
  2. import java.time.LocalDate;
  3. import java.time.chrono.IsoChronology;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import static java.util.stream.Collectors.groupingBy;
  10. import static java.util.stream.Collectors.toList;
  11.  
  12. class Person {
  13.  
  14. String name;
  15. LocalDate birthday;
  16. Sex gender;
  17. String emailAddress;
  18.  
  19. Person(String nameArg, LocalDate birthdayArg, Sex genderArg, String emailArg) {
  20. name = nameArg;
  21. birthday = birthdayArg;
  22. gender = genderArg;
  23. emailAddress = emailArg;
  24. }
  25.  
  26. public enum Sex {
  27. MALE, FEMALE
  28. }
  29.  
  30. public void printPerson() {
  31. System.out.println(name + ", " + this.getAge());
  32. }
  33.  
  34. public int getAge() {
  35. return birthday.until(IsoChronology.INSTANCE.dateNow()).getYears();
  36. }
  37.  
  38. public Sex getGender() {
  39. return gender;
  40. }
  41.  
  42. public String getName() {
  43. return name;
  44. }
  45.  
  46. public String getEmailAddress() {
  47. return emailAddress;
  48. }
  49. }
  50.  
  51. class JavaLambda {
  52. private static ArrayList<Person> members = new ArrayList<>();
  53.  
  54. public static void main(String[] args) throws java.lang.Exception {
  55.  
  56. members.add(new Person("Fred", IsoChronology.INSTANCE.date(1980, 6, 20), Person.Sex.MALE, "fred@example.com"));
  57. members.add(new Person("Jane", IsoChronology.INSTANCE.date(1990, 7, 15), Person.Sex.FEMALE, "jane@example.com"));
  58. members.add(new Person("Mark", IsoChronology.INSTANCE.date(1990, 7, 15), Person.Sex.MALE, "mark@example.com"));
  59. members.add(new Person("George", IsoChronology.INSTANCE.date(1991, 8, 13), Person.Sex.MALE, "george@example.com"));
  60. members.add(new Person("Bob", IsoChronology.INSTANCE.date(2000, 9, 12), Person.Sex.MALE, "bob@example.com"));
  61.  
  62. Map<Integer, List<String>> collect =
  63. members.stream()
  64. .collect(groupingBy(
  65. Person::getAge,
  66. mapping(Person::getName, toList()))
  67. );
  68. System.out.println(collect);
  69. }
  70. }
Compilation error #stdin compilation error #stdout 0.28s 320896KB
stdin
Standard input is empty
compilation info
Main.java:66: error: cannot find symbol
                           mapping(Person::getName, toList()))
                           ^
  symbol:   method mapping(Person::getName,Collector<Object,CAP#1,List<Object>>)
  location: class JavaLambda
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
Main.java:66: error: invalid method reference
                           mapping(Person::getName, toList()))
                                   ^
  non-static method getName() cannot be referenced from a static context
Main.java:65: error: invalid method reference
                           Person::getAge,
                           ^
  non-static method getAge() cannot be referenced from a static context
3 errors
stdout
Standard output is empty