第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何根據(jù)日期進(jìn)行分組并添加其他屬性

如何根據(jù)日期進(jìn)行分組并添加其他屬性

BIG陽 2023-04-26 10:49:35
我有 Person DTO 有 4 個(gè)屬性class Person {            private String name;            private String city;            private int age;            private String dateOfBirth;            // Setters and getters }// sample data      personList.add(new Person("Mike", "London", 38, "01/01/1981"));                personList.add(new Person("John", "London", 38, "01/02/1981"));                personList.add(new Person("John", "Bristol",38, "01/06/1981"));                personList.add(new Person("Steve", "Paris",06, "03/07/2013"));我正在嘗試根據(jù) dateOfBirth 屬性對上述列表中的數(shù)據(jù)進(jìn)行分組。(僅考慮月份和年份,日期被忽略)如果用戶屬于同一月份和年份,則添加年齡,下面的示例輸出
查看完整描述

2 回答

?
紫衣仙女

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊

為了使這個(gè)簡單,首先我將流式傳輸List<Person然后將它們收集到Map<String, List<Person>>使用基于Date輸出模式的 groupingBy 中outputPattern,因?yàn)镾impleDateFormat這是我想使用的遺產(chǎn)LocalDate


 String outDate = LocalDate.parse(p1.getDateOfBirth(),

                                  DateTimeFormatter.ofPattern(patternInput))

                                 .format(DateTimeFormatter.ofPattern(outputPattern)); 

現(xiàn)在將其與groupingBy


 Map<String, List<Person>> groupResult = personList.stream()

            .collect(Collectors.groupingBy(p->LocalDate.parse(p.getDateOfBirth(),

                           DateTimeFormatter.ofPattern(patternInput))

                               .format(DateTimeFormatter.ofPattern(outputPattern))));

現(xiàn)在使用forEach并以所需格式打印輸出


  groupResult.forEach((k,v)->System.out.println("Date : "+ k+

        ", Age : " + v.stream().mapToInt(Person::getAge).sum()+

        ", UniqueCityCount : "+v.stream().map(Person::getCity).distinct().count()));

最后輸出


Date : Mar-19, Age : 34, UniqueCityCount : 1

Date : Jan-81, Age : 97, UniqueCityCount : 2

您也可以使用YearMonth,但如果您需要自己的格式,它仍然是一回事


  String ym = YearMonth.parse(p1.getDateOfBirth(), 

                  DateTimeFormatter.ofPattern(patternInput))

                  .format(DateTimeFormatter.ofPattern(outputPattern));

輸出


Mar-19

按默認(rèn)年月返回此格式ISO-8601 日歷系統(tǒng)中的年月,例如 2007-12。


查看完整回答
反對 回復(fù) 2023-04-26
?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊

Map< YearMonth , Set < Person > >

對于日期時(shí)間工作,您使用的是多年前隨著 JSR 310 的采用而被取代的可怕的舊類。您應(yīng)該改用java.time類。

對于班級中的出生日期成員,請使用數(shù)據(jù)類型LocalDate。這表示沒有時(shí)間和時(shí)區(qū)的日期。

提示:使用ISO 8601字符串將日期時(shí)間值序列化為文本。對于僅限日期的值,則為 YYYY-MM-DD。

LocalDate?ld?=?LocalDate.parse(?"2019-01-23"?)?;

對于按年-月的聚合,請使用該類YearMonth

YearMonth?ym?=?YearMonth.from(?ld?)?;

為什么要將年齡傳遞給構(gòu)造函數(shù)?您傳遞生日,因此可以計(jì)算年齡。年齡應(yīng)該通過動態(tài)計(jì)算經(jīng)過的年數(shù)的 getter 方法返回,從不存儲。

此外,您的示例數(shù)據(jù)毫無意義,年齡與生日不符。

Person?p?=?new?Person(?"Alice"?,?"Johnson"?,?"1981-01-02"?)?;
int?age?=?p.getAge()?;

實(shí)施Person::getAge( ZoneId )方法。

確定今天的日期需要時(shí)區(qū)。對于任何給定時(shí)刻,日期在全球范圍內(nèi)因地區(qū)而異。

ZoneId?z?=?ZoneId.of(?"America/Montreal"?)?;

將區(qū)域傳遞給getAge方法。

public int getAge( ZoneId z ) {

? ? LocalDate today = LocalDate.now( z ) ;

? ? Period age = Period.between( p.getBirthDate() , today ) ;

? ? int years = age.getYears() ;

? ? return years ;

}

或者,將LocalDate(今天的日期)傳遞給getAge而不是時(shí)區(qū)。


將您的地圖定義為將年月映射到一組人。


Map< YearMonth , Set< Person > > = new TreeMap<>() ;

對于每個(gè)Person對象,獲取YearMonth其出生日期。對于每個(gè)這樣的年月,Person如果尚不存在,則創(chuàng)建一組對象。并將此人添加到集合中。


完成后,循環(huán)地圖以檢索每組人。循環(huán)Person該組中的每個(gè)對象。計(jì)算年齡,求和。


如果您想花哨一點(diǎn),可以使用流來完成報(bào)告年齡和代碼。


查看完整回答
反對 回復(fù) 2023-04-26
  • 2 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號