Aşağıdaki sınıfa sahibim.Java 8 Özel koleksiyonlayıcı kullanarak gruplama?
Yaşına göre gruplandırılabilir ve ardından Kişiler nesnesinin kendisi yerine kişi adlarının listesini toplayabilirim; hepsi tek bir güzel lamba ifadesinde.
Tüm bunları basitleştirmek için, gruplandırmanın sonucunu yaşa göre saklayan mevcut çözümümü bağlarım ve daha sonra adları toplamak için bunu yineliyorum.
İdeal Değilve daha zarif ve sahne çözüm olsun isterim öğrenme uğruna
ArrayList<OtherPerson> members = new ArrayList<>();
members.add(new OtherPerson("Fred", IsoChronology.INSTANCE.date(1980, 6, 20), OtherPerson.Sex.MALE, "[email protected]"));
members.add(new OtherPerson("Jane", IsoChronology.INSTANCE.date(1990, 7, 15), OtherPerson.Sex.FEMALE, "[email protected]"));
members.add(new OtherPerson("Mark", IsoChronology.INSTANCE.date(1990, 7, 15), OtherPerson.Sex.MALE, "[email protected]"));
members.add(new OtherPerson("George", IsoChronology.INSTANCE.date(1991, 8, 13), OtherPerson.Sex.MALE, "[email protected]"));
members.add(new OtherPerson("Bob", IsoChronology.INSTANCE.date(2000, 9, 12), OtherPerson.Sex.MALE, "[email protected]"));
Map<Integer, List<Person>> collect = members.stream().collect(groupingBy(Person::getAge));
Map<Integer, List<String>> result = new HashMap<>();
collect.keySet().forEach(key -> {
result.put(key, collect.get(key).stream().map(Person::getName).collect(toList()));
});
.
Ops, bu kolaydı! Şerefe! – lcardito