这是我的程序
package com;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.google.gson.Gson;
public class GroupByDemoInJava8 {
public static void main(String args[]) throws Exception {
try {
List<Person> personList = new ArrayList<>(); // Date Format is MM/DD/YYYY
personList.add(new Person("Mike", "London", 15, "01/01/1981"));
personList.add(new Person("John", "London", 21, "01/02/1981"));
personList.add(new Person("Prasanna", "London", 28, "04/28/1990"));
personList.add(new Person("Monobo", "Tokyo", 34, "04/28/1990"));
personList.add(new Person("Sam", "Paris", 44, "07/12/1992"));
personList.add(new Person("Nadal", "Paris", 5, "04/02/1992"));
String patternInput = "MM/dd/yyyy";
SimpleDateFormat simpleDateFormatInput = new SimpleDateFormat(patternInput);
String outputPattern = "MMM-yy";
SimpleDateFormat simpleDateFormatOutput = new SimpleDateFormat(outputPattern);
Map<String, List<Person>> personByMap = new TreeMap<String, List<Person>>();
for (Person p : personList) {
int sumAge = 0;
Date inputDate = simpleDateFormatInput.parse(p.getDateOfBirth());
String outPutDate = simpleDateFormatOutput.format(inputDate);
if (!personByMap.containsKey(outPutDate)) {
sumAge = sumAge+p.getAge();
System.out.println("Date "+outPutDate+" "+"Age "+sumAge);
}
else
{
personByMap.get(outPutDate).add(p);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}发布于 2019-07-26 21:15:42
像这样的东西
public class GroupByDemoInJava8 {
public static void main(String args[]){
List<Person> personList = new ArrayList<>();
/* INITIALIZATION */
Map<String, Integer> result = personList.stream()
.collect(Collectors.groupingBy(Person::getDateOfBirth,
Collectors.summingInt(Person::getAge));
}
}如上所述,here
发布于 2019-07-26 21:21:08
试试这个,
try {
List<Person> personList = new ArrayList<>(); // Date Format is MM/DD/YYYY
personList.add(new Person("Mike", "London", 15, "01/01/1981"));
personList.add(new Person("John", "London", 21, "01/02/1981"));
personList.add(new Person("Prasanna", "London", 28, "04/28/1990"));
personList.add(new Person("Monobo", "Tokyo", 34, "04/28/1990"));
personList.add(new Person("Sam", "Paris", 44, "07/12/1992"));
personList.add(new Person("Nadal", "Paris", 5, "04/02/1992"));
String patternInput = "MM/dd/yyyy";
SimpleDateFormat simpleDateFormatInput = new SimpleDateFormat(patternInput);
String outputPattern = "MMM-yy";
SimpleDateFormat simpleDateFormatOutput = new SimpleDateFormat(outputPattern);
Map<String, Person> personByMap = new TreeMap<String, Person>();
Map<String, Person> personByResult = new TreeMap<String, Person>();
new ArrayList<>();
for (Person p : personList) {
int sumAge = 0;
Date inputDate = simpleDateFormatInput.parse(p.getDateOfBirth());
String outPutDate = simpleDateFormatOutput.format(inputDate);
if (personByMap.containsKey(outPutDate)) {
sumAge = personByMap.get(outPutDate).getAge() + p.getAge();
personByMap.get(outPutDate).setAge(sumAge);
personByResult.remove(outPutDate);
System.out.println("Date " + outPutDate + " " + "Age " + sumAge);
}
else {
// personListTest.add(p);
// sumAge=p.getAge();
personByMap.put(outPutDate, p);
personByResult.put(outPutDate, p);
}
}
// using for-each loop for iteration over Map.entrySet()
for (Map.Entry<String, Person> entry : personByResult.entrySet())
System.out.println("Date " + entry.getKey() + ", Age " + entry.getValue().getAge());
} catch (Exception e) {
e.printStackTrace();
}https://stackoverflow.com/questions/57220323
复制相似问题