Java 从列表中检索多个最小事件

Java 从列表中检索多个最小事件,java,java-8,java-stream,Java,Java 8,Java Stream,我有一个自定义对象列表: List<CustomObject> customObjects; 我能够通过自定义比较器函数成功地在列表中找到最早日期的对象,如下所示: class CustomObject { LocalDateTime date; public LocalDateTime getDateTime() { return date; } } private static LocalDateTime getDate(CustomObje

我有一个自定义对象列表:

List<CustomObject> customObjects;
我能够通过自定义比较器函数成功地在列表中找到最早日期的对象,如下所示:

class CustomObject {
   LocalDateTime date;

   public LocalDateTime getDateTime() {
       return date;
   }
}
private static LocalDateTime getDate(CustomObject customObject) {
        return customObject.getDateTime();
}

CustomObject customObjectMin = customObjects.stream().
           min(Comparator.comparing(MyUtilClass::getDate));
List<CustomObject> customObjectsMin = customObjects.stream().
           minWithAllOccurences(Comparator.comparing(MyUtilClass::getDate));
但是,可以有多个具有相同日期的自定义对象,但在使用
min
的情况下,似乎无法获得多个实例。是否有一个简单的解决方案来查找列表中设置了最早日期的所有对象?大概是这样的:

class CustomObject {
   LocalDateTime date;

   public LocalDateTime getDateTime() {
       return date;
   }
}
private static LocalDateTime getDate(CustomObject customObject) {
        return customObject.getDateTime();
}

CustomObject customObjectMin = customObjects.stream().
           min(Comparator.comparing(MyUtilClass::getDate));
List<CustomObject> customObjectsMin = customObjects.stream().
           minWithAllOccurences(Comparator.comparing(MyUtilClass::getDate));
List customObjectsMin=customObjects.stream()。
minWithAlloccurrences(Comparator.comparing(MyUtilClass::getDate));

您可以进行两种选择

  • 一个是查找最小日期
  • 一个找到那个日期的人
e、 g

LocalDate min=customObjects.stream()
.map(CustomObject::getDateTime)
.min(Comparator.naturalOrder());
List objs=customObjects.stream()
.filter(c->min.equals(c.getDateTime()))
.collect(Collectors.toList());

或者你可以使用Collectors.groupingBy进入一个树状图,并获取第一个条目。

除了Peter Lawrey的优秀答案之外,我想指出的是,在避免将每个元素收集到树状图中的内存开销的同时,也可以使用单个流来实现这一点。怎么用?一种方法是使用
reduce()
,如下所示:

List<SampleJava> customObjectsMin = customObjects.stream()
        .reduce(new ArrayList<>(), // identity
        (List<SampleJava> list, SampleJava item) -> { // accumulate
            if(list.isEmpty() ||  getDate(item).compareTo(getDate(list.get(0))) < 0) {
                return new ArrayList<>(Arrays.asList(item));
            } else if(getDate(item).equals(getDate(list.get(0)))) {
                list.add(item);
            }
            return list;
        },
        (list1, list2) -> { // combine
            if(list1.isEmpty()) return list2; 
            if(list2.isEmpty()) return list1; 
            int cmp = getDate(list1.get(0)).compareTo(getDate(list2.get(0)));
            if(cmp < 0) return list1;
            if(cmp > 0) return list2;
            list1.addAll(list2);
            return list1;
        });
List customObjectsMin=customObjects.stream()
.reduce(新的ArrayList(),//标识
(列表,SampleJava项)->{//累积
if(list.isEmpty()| | getDate(item).compareTo(getDate(list.get(0))<0){
返回新的ArrayList(Arrays.asList(item));
}else if(getDate(item).equals(getDate(list.get(0))){
列表。添加(项目);
}
退货清单;
},
(列表1,列表2)->{//combine
if(list1.isEmpty())返回list2;
if(list2.isEmpty())返回list1;
int cmp=getDate(list1.get(0)).compareTo(getDate(list2.get(0));
if(cmp<0)返回列表1;
如果(cmp>0)返回列表2;
列表1.addAll(列表2);
返回列表1;
});