首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何总结年龄?

如何总结年龄?
EN

Stack Overflow用户
提问于 2019-07-26 20:41:59
回答 2查看 161关注 0票数 0

这是我的程序

代码语言:javascript
复制
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();
                }

            }
        }
EN

回答 2

Stack Overflow用户

发布于 2019-07-26 21:15:42

像这样的东西

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2019-07-26 21:21:08

试试这个,

代码语言:javascript
复制
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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57220323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档