我上课
public class Person {
private String name;
private String country;
private String city;
private String pet;
private int totalCountryToCityCount;
private int petCount;
public Person(String name, String country, String city, String pet, int total, int petCount) {
this.name = name;
this.country = country;
this.city = city;
this.pet = pet;
this.totalCountryToCityCount = total;
this.petCount = petCount;
}
public String getName() {
return name;
}
public String getCountry() {
return country;
}
public String getCity() {
return city;
}
public String getPet() {
return pet;
}
public int getPetCount() {
return petCount;
}
public int getTotalCountryToCityCount() {
return totalCountryToCityCount;
}
}
并给定Person类的列表,我根据类的不同属性进行汇总。 例如-
Person person1 = new Person("John", "USA", "NYC", "Max", 1, 2);
Person person2 = new Person("Steve", "UK", "London", "Lucy", 2, 8);
Person person3 = new Person("Anna", "USA", "NYC", "Max", 4, 32);
Person person4 = new Person("Mike", "USA", "Chicago", "Duke", 5, 1);
Person person5 = new Person("Test", "INDIA", "HYD", "Tommy", 4, 32);
Person person6 = new Person("Test1", "INDIA", "HYD", "Tommy", 4, 65);
Person person7 = new Person("Tim", "USA", "Chicago", "Duke", 5, 111);
Person person8 = new Person("Tim", "USA", "Chicago", "Puke", 5, 111);
Person person9 = new Person("Test1", "INDIA", "DELHI", "Tommy", 4, 65);
List<Person> persons = Arrays
.asList(person1, person2, person3, person4, person5, person6, person7, person8,
person9);
现在,我需要得到一个结果,以便我应该基于国家和城市的组合获得总的“ totalCountryToCityCount”,而我应该基于国家,城市和宠物的组合获得总的“ petCount”。我可以使用groupingBy和summingint分别获取它们
private Map<String, Map<String, Integer>> getTotalCountForCountry(List<Person> persons) {
return persons.stream().collect(groupingBy(Person::getCountry, getCityCount()));
}
public Collector<Person, ?, Map<String, Integer>> getCityCount() {
return groupingBy(Person::getCity, summingInt(Person::getTotal));
}
public Map<String, Map<String, Map<String, Integer>>> threeLevelGrouping(List<Person> persons) {
return persons
.stream().collect(
groupingBy(Person::getCountry, groupByCityAndPetName()
)
);
}
private Collector<Person, ?, Map<String, Map<String, Integer>>> groupByCityAndPetName() {
return groupingBy(Person::getCity, groupByPetName());
}
private Collector<Person, ?, Map<String, Integer>> groupByPetName() {
return groupingBy(Person::getPet, summingInt(Person::getPetCount));
}
给出结果
{USA={Chicago={Puke=111, Duke=112}, NYC={Max=34}}, UK={London={Lucy=8}}, INDIA={DELHI={Tommy=65}, HYD={Tommy=97}}}
{USA={Chicago=15, NYC=5}, UK={London=2}, INDIA={DELHI=4, HYD=8}}
但是我想要的实际结果是:-
{USA={Chicago={15,{Puke=111, Duke=112}}, NYC={5,{Max=34} }, UK={London={2, {Lucy=8}}, INDIA={DELHI={4, {Tommy=65}}, , HYD={8,{Tommy=97}}}}
有没有一种方法可以使用Java流API
我也尝试使用代码-
personList.stream().collect(groupingBy(person -> person.getCountry(), collectingAndThen(reducing(
(a, b) -> new Person(a.getName(), a.getCountry(), a.getCity(), a.getPet(),
a.getTotal() + b.getTotal(), a.getPetCount() + b.getPetCount())),
Optional::get)))
.forEach((country, person) -> System.out.println(country + person));
但是得到了结果-
USAPerson{name='John', country='USA', city='NYC'}
UKPerson{name='Steve', country='UK', city='London'}
INDIAPerson{name='Test', country='INDIA', city='HYD'}
计数被意外删除
答案 0 :(得分:0)
您真正要寻找的是Collectors::teeing
,但仅在java-12中可用:
System.out.println(
persons.stream()
.collect(Collectors.groupingBy(
Person::getCountry,
Collectors.groupingBy(
Person::getCity,
Collectors.teeing(
Collectors.summingInt(Person::getTotalCountryToCityCount),
Collectors.groupingBy(
Person::getPet,
Collectors.summingInt(Person::getPetCount)
),
SimpleEntry::new
)
))));
{-8}可以使用Java-8的反向端口。