Java 正在从具有日期比较的列表中删除

Java 正在从具有日期比较的列表中删除,java,list,iterator,refactoring,Java,List,Iterator,Refactoring,我必须删除param.getFromDateTime之前和params.getToDateTime之后的所有对象。我是这样做的: for (ListIterator<Doc> iterator = doc.listIterator(); iterator.hasNext(); ) { while(iterator.hasNext()){ if(param.getFromDateTime() != null &&

我必须删除param.getFromDateTime之前和params.getToDateTime之后的所有对象。我是这样做的:

for (ListIterator<Doc> iterator = doc.listIterator(); iterator.hasNext(); ) {
    while(iterator.hasNext()){
            if(param.getFromDateTime() != null
              && doc.get(iterator.nextIndex()).getDate().before(params.getFromTime())) {
                iterator.remove();
            }
            if(params.getToDateTime() != null
              && doc.get(iterator.nextIndex()).getDate().after(params.getToDateTime())) {
                iterator.remove();
           }
    }
}

我能做得更好更清晰吗?

您不需要两个循环,也不需要ListIterator。 请注意,这两个条件可能为true,但在任何情况下都只希望删除一个元素,因为调用两次remove将引发异常。 您还可以直接从迭代的Doc对象中提取日期,使其更加清晰:

for (Iterator<Doc> iterator = doc.iterator(); iterator.hasNext(); ) {
      Date date = iterator.next().getDate();

      if( (param.getFromDateTime() != null && date.before(params.getFromTime())) 
          || (params.getToDateTime() != null && date.after(params.getToDateTime()))) {
        iterator.remove();
      }

}

您不需要两个循环,也不需要ListIterator。 请注意,这两个条件可能为true,但在任何情况下都只希望删除一个元素,因为调用两次remove将引发异常。 您还可以直接从迭代的Doc对象中提取日期,使其更加清晰:

for (Iterator<Doc> iterator = doc.iterator(); iterator.hasNext(); ) {
      Date date = iterator.next().getDate();

      if( (param.getFromDateTime() != null && date.before(params.getFromTime())) 
          || (params.getToDateTime() != null && date.after(params.getToDateTime()))) {
        iterator.remove();
      }

}

具有流api的java-8近似:

List<Doc> docs = ...;
Stream<Doc> stream = docs.stream();
LocalDateTime fromDateTime = param.getFromDateTime();
LocalDateTime toDateTime = param.getToDateTime();
if (fromDateTime != null){
    stream = stream.filter(d -> !d.getDate().before(fromDateTime);
}
if (toDateTime != null){
    stream = stream.filter(d -> !d.getDate().after(toDateTime);
}
docs = stream.collect(Collectors.toList());

具有流api的java-8近似:

List<Doc> docs = ...;
Stream<Doc> stream = docs.stream();
LocalDateTime fromDateTime = param.getFromDateTime();
LocalDateTime toDateTime = param.getToDateTime();
if (fromDateTime != null){
    stream = stream.filter(d -> !d.getDate().before(fromDateTime);
}
if (toDateTime != null){
    stream = stream.filter(d -> !d.getDate().after(toDateTime);
}
docs = stream.collect(Collectors.toList());
在使用Java 8的情况下,可能需要使用List.removeIf:

List<Doc> docs = generateSomeValues(); // this ist just an imaginary filling of the list
// check if the params are not null and remove dates that are not in the given interval
docs.removeIf(doc ->
    (params.getFromTime() != null && doc.getDate().before(params.getFromTime()))
    || (params.getToDateTime() != null && doc.getDate().after(params.getToDateTime()))
);
在使用Java 8的情况下,可能需要使用List.removeIf:

List<Doc> docs = generateSomeValues(); // this ist just an imaginary filling of the list
// check if the params are not null and remove dates that are not in the given interval
docs.removeIf(doc ->
    (params.getFromTime() != null && doc.getDate().before(params.getFromTime()))
    || (params.getToDateTime() != null && doc.getDate().after(params.getToDateTime()))
);

为什么调用doc.getiterator.nextIndex而不是iterator.next并重用返回的元素?为什么要使用两个嵌套循环?我认为应该将其发布到,而不是这里。为什么要调用doc.getiterator.nextIndex而不是iterator.next并重用返回的元素?为什么要使用两个嵌套循环?我想你应该把它发布到,而不是这里。两次调用remove时关于异常的警告非常有用!谢谢!不客气。我添加了一个额外的重构。最后,这里不需要Doc变量。两次调用remove时关于异常的警告非常有用!谢谢!不客气。我添加了一个额外的重构。最后,这里不需要Doc变量。