Reputation: 58083
java8
public class CustomDate{
LocalDateTime datetime;
}
List<CustomDate> list = // 2 dates with multiple times
I want to group dates by day and values as times following gives me CustomDate as values?
Map<LocalDate, List<CustomDate>> map =
list.stream().groupingBy(d-> d.datetime.toLocalDate())
any help is appreciated.
Upvotes: 0
Views: 566
Reputation: 3688
public class Dates {
private static List< CustomDate > list = new ArrayList<>( );
public static void main( String[] args ) {
Map< LocalDate, List< LocalTime > > result2 = list.stream()
.map( CustomDate::getDatetime )
.collect(
HashMap::new,
Dates::accept,
Dates::merge
);
}
private static void accept( HashMap< LocalDate, List< LocalTime > > map, LocalDateTime date ) {
map.putIfAbsent( date.toLocalDate( ), new ArrayList<>())
.add( date.toLocalTime( ) );
}
private static void merge( Map< LocalDate, List< LocalTime > > map1, Map< LocalDate, List< LocalTime > > map2 ) {
map2.forEach( (date,timeList) -> {
map1.merge( date, timeList, ( l1, l2 ) -> {
l1.addAll( l2 );
return l1;
} );
});
}
}
Or simply:
Map< LocalDate, List< LocalTime > > result2 = list.stream()
.map( CustomDate::getDatetime )
.collect(
HashMap::new,
(map,date) -> {
map.putIfAbsent( date.toLocalDate( ), new ArrayList<>( )).add( date.toLocalTime( ) );
},
(map1, map2) -> {
map2.forEach( (date,timeList) -> {
map1.merge( date, timeList, ( l1, l2 ) -> {
l1.addAll( l2 );
return l1;
} );
});
}
);
Doing it not so declaratively as calling 3 times the collectors static methods, gives you much more control to dictate behavior, like using a initial size for your arrays, using specific lists and so on, without losing the sugar coming from the declarative style.
Map< LocalDate, List< LocalTime > > result2 = list.stream()
.map( CustomDate::getDatetime )
.collect(
TreeMap::new,
(map,date) -> {
map.putIfAbsent( date.toLocalDate( ), new ArrayList<>( 800 )).add( date.toLocalTime( ) );
},
(map1, map2) -> {
map2.forEach( (date,timeList) -> {
map1.merge( date, timeList, ( l1, l2 ) -> {
l1.addAll( l2 );
return l1;
} );
});
}
);
Of course if the default behavior is acceptable, this is way simpler:
Map<LocalDate, List<LocalTime>> result = list.stream()
.map( CustomDate::getDatetime )
.collect(groupingBy(LocalDateTime::toLocalDate,
mapping(LocalDateTime::toLocalTime,toList())));
Another way to go is to define your own collector, which is what I usually prefer. By doing that you gain both, control and the ability to code your logic declaratively, and reuse when needed.
Which would reduce you code to:
Map< LocalDate, List< LocalTime > > result2 = list.stream()
.collect(
customDateCollector()
);
If you had something like:
class CustomDateCollector implements Collector<CustomDate, Map< LocalDate, List< LocalTime > >, Map< LocalDate, List< LocalTime > >> {
@Override
public Supplier< Map< LocalDate, List< LocalTime > > > supplier( ) {
return HashMap::new;
}
@Override
public BiConsumer< Map< LocalDate, List< LocalTime > >, CustomDate > accumulator( ) {
int goodStartUsingProblemKnowledge = 30;
return (map, customDate) -> map.putIfAbsent( customDate.getDatetime().toLocalDate(), new ArrayList<>( goodStartUsingProblemKnowledge) )
.add( customDate.getDatetime().toLocalTime() );
}
@Override
public BinaryOperator< Map< LocalDate, List< LocalTime > > > combiner( ) {
return (map1, map2) -> {
map2.forEach( (date,timeList) -> {
map1.merge( date, timeList, ( list1, list2 ) -> {
list1.addAll( list2 );
return list1;
} );
});
return map1;
};
}
@Override
public Function< Map< LocalDate, List< LocalTime > >, Map< LocalDate, List< LocalTime > > > finisher( ) {
return Function.identity();
}
@Override
public Set< Characteristics > characteristics( ) {
return Collections.unmodifiableSet( EnumSet.of( IDENTITY_FINISH, UNORDERED,CONCURRENT) );
}
public static CustomDateCollector customDateCollector(){
return new CustomDateCollector();
}
}
Upvotes: 0
Reputation: 18245
As in answer from Aominè you should use Collectors.groupingBy()
. But I reccomend you to do some additional steps.
1 Create GroupUtils
class with some general code teamplates:
public final class GroupUtils {
public static <K, V> Map<K, List<V>> groupMultipleBy(Collection<V> data, Function<V, K> classifier) {
return groupMultipleBy(data, classifier, Function.identity());
}
public static <K, V, S> Map<K, List<S>> groupMultipleBy(Collection<V> data, Function<V, K> classifier, Function<V, S> mapper) {
return Optional.ofNullable(data).orElse(Collections.emptyList()).stream()
.collect(Collectors.groupingBy(classifier, Collectors.mapping(mapper, Collectors.toList())));
}
}
This is example from one of my project. Here I have more similar methods.
2 In local file, do create concrete method, to make client's code much more readable.
class Client {
public static void main(String... args) {
List<CustomDate> list = Collections.emptyList();
Map<LocalDateTime, List<CustomDate>> map = groupByDatetime(list);
}
private static Map<LocalDateTime, List<CustomDate>> groupByDatetime(List<CustomDate> list) {
return GroupUtils.groupMultipleBy(list, CustomDate::getDatetime);
}
}
I believe this is a little bit more code, but much more readable than:
Map<LocalDate, List<LocalTime>> result = list.stream()
.collect(Collectors.groupingBy(e -> e.getDatetime().toLocalDate(),
Collectors.mapping(e -> e.getDatetime().toLocalTime(),
Collectors.toList())));
Upvotes: 0
Reputation: 56433
It seems like you want to group by the date and have the map values as LocalTime
instead of CustomDate
.
Map<LocalDate, List<LocalTime>> result = list.stream()
.collect(Collectors.groupingBy(e -> e.getDatetime().toLocalDate(),
Collectors.mapping(e -> e.getDatetime().toLocalTime(),
Collectors.toList())));
Upvotes: 4