[英]How to get number of occurrences of a month from a list of dates in java
Java: I have a list of String which holds Date column data from database. Java:我有一个字符串列表,其中包含数据库中的日期列数据。 I want the count of each month.我想要每个月的计数。
Say I have data: 2021-07-01 2021-08-20 2021-08-25 2021-09-05 2022-01-06 2022-06-07说我有数据: 2021-07-01 2021-08-20 2021-08-25 2021-09-05 2022-01-06 2022-06-07
I want to retrieve value 1 for July,2 for August, 1 for September, 1 for January, 1 for June.我想检索值 1 为 7 月,2 为 8 月,1 为 9 月,1 为 1 月,1 为 6 月。
How can I use stream API to achieve this?如何使用 stream API 来实现这一点? Or any other method to retrieve vthis data或任何其他检索 vthis 数据的方法
List
.of( "2021-07-01", "2021-08-20", "2021-08-25", "2021-09-05", "2022-01-06", "2022-06-07" )
.stream()
.map( input -> LocalDate.parse( input ) ) // Parse each element as a `LocalDate` object, for a new stream.
.collect(
Collectors.groupingBy( date ->
YearMonth :: from , // Extract a `YearMonth` from each `LocalDate` as the key.
Collectors.counting() // Count occurrences.
)
) // Return a `Map`.
Make example data.制作示例数据。
List< String > inputs = List.of( "2021-07-01", "2021-08-20", "2021-08-25", "2021-09-05", "2022-01-06", "2022-06-07" );
Make a stream of those inputs.制作这些输入的 stream。 Parse each element as a LocalDate
object, for a new stream.将每个元素解析为LocalDate
object,以获得新的 stream。 Produce a key-value Map < YearMonth, Integer >
object by extracting a YearMonth
from each LocalDate
as the key, and summing one as a count of occurrences for the value.生成一个键值Map < YearMonth, Integer >
object 通过从每个LocalDate
中提取YearMonth
作为键,并将其相加作为出现次数。
Map< YearMonth , Integer > monthCounts =
inputs
.stream()
.map( input -> LocalDate.parse( input ) ) // Parse each element as a `LocalDate` object, for a new stream.
.collect(
Collectors.groupingBy( date ->
YearMonth :: from , // Extract a `YearMonth` from each `LocalDate` as the key.
Collectors.counting() // Count occurrences.
)
) // Return a `Map`.
;
See this code run live at Ideone.com .请参阅在 Ideone.com 上实时运行的代码。
{2021-09=1, 2021-08=2, 2021-07=1, 2022-06=1, 2022-01=1} {2021-09=1, 2021-08=2, 2021-07=1, 2022-06=1, 2022-01=1}
NavigableMap
按年月排序: NavigableMap
You might want to keep the entries in your resulting map sorted by the YearMonth
keys.您可能希望将生成的 map 中的条目按YearMonth
键排序。 If so:如果是这样:
Map
to NavigableMap
interface.将您声明的 map 类型从Map
更改为NavigableMap
接口。TreeMap:: new
to instantiate an objecting implementing NavigableMap
.为TreeMap:: new
添加一个参数以实例化实现NavigableMap
的对象。NavigableMap < YearMonth, Long > monthCounts =
inputs
.stream()
.map( input -> LocalDate.parse( input ) )
.collect(
Collectors.groupingBy(
YearMonth :: from ,
TreeMap::new,
Collectors.counting()
)
);
I wrote up a process to allow you to get your desired goal using Streams and Lambdas.我编写了一个流程,让您可以使用 Streams 和 Lambdas 实现您想要的目标。
Parse your dates into an Array of LocalDates.将您的日期解析为 LocalDates 数组。
Define a Predicate for your criteria.为您的条件定义一个谓词。
Filter your Stream using your Predicate and get the count().使用您的谓词过滤您的 Stream 并获取 count()。
String[] dates = { "2021-07-01", "2021-08-20", "2021-08-25", "2021-09-05", "2022-01-06", "2022-06-07" }; LocalDate[] localDates = new LocalDate[dates.length]; for(int i = 0; i < dates.length; i++){ localDates[i] = LocalDate.parse(dates[i]); } Predicate<LocalDate> month = localDate -> localDate.getMonth().equals(Month.AUGUST); long count = Arrays.stream(localDates).filter(month).count();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.