java8
public class CustomDate{
LocalDateTime datetime;
}
List<CustomDate> list = // 2 dates with multiple times
我想按天分组日期和以下时间的值将CustomDate作为值?
Map<LocalDate, List<CustomDate>> map =
list.stream().groupingBy(d-> d.datetime.toLocalDate())
感谢任何帮助。
答案 0 :(得分:4)
您似乎希望按日期分组,并将地图值设为LocalTime
而不是CustomDate
。
Map<LocalDate, List<LocalTime>> result = list.stream()
.collect(Collectors.groupingBy(e -> e.getDatetime().toLocalDate(),
Collectors.mapping(e -> e.getDatetime().toLocalTime(),
Collectors.toList())));
答案 1 :(得分:0)
正如Aominè的回答一样,您应该使用Collectors.groupingBy()
。但我建议你做一些额外的步骤。
1 使用一些常规代码teamplates创建GroupUtils
类:
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())));
}
}
这是我的一个项目的例子。在这里,我有更多类似的方法。
2 在本地文件中,创建具体方法,使客户端代码更具可读性。
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);
}
}
我相信这是一些代码,但更具可读性:
Map<LocalDate, List<LocalTime>> result = list.stream()
.collect(Collectors.groupingBy(e -> e.getDatetime().toLocalDate(),
Collectors.mapping(e -> e.getDatetime().toLocalTime(),
Collectors.toList())));
答案 2 :(得分:0)
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;
} );
});
}
}
或者简单地说:
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;
} );
});
}
);
这样做不像声明那样调用收集器静态方法的3倍,给你更多的控制来指示行为,比如使用数组的初始大小,使用特定的列表等,而不会丢失来自声明的糖风格。
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;
} );
});
}
);
当然,如果默认行为可以接受,那就更简单了:
Map<LocalDate, List<LocalTime>> result = list.stream()
.map( CustomDate::getDatetime )
.collect(groupingBy(LocalDateTime::toLocalDate,
mapping(LocalDateTime::toLocalTime,toList())));
另一种方法是定义自己的收藏家,这是我通常喜欢的。通过这样做,您可以获得控制权和以声明方式编写逻辑的能力,并在需要时重用。
这会将您的代码缩减为:
Map< LocalDate, List< LocalTime > > result2 = list.stream()
.collect(
customDateCollector()
);
如果你有类似的话:
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();
}
}