问题描述
这是我的阵列列表
ArrayList<Courses> list = new ArrayList<Courses>();
list.add( new Courses(350,5/20/2020) );
list.add( new Courses(350,4/20/2019 );
list.add( new Courses(350,3/20/2018) );
list.add( new Courses(360,6/20/2020) );
list.add( new Courses(360,5/20/2019) );
list.add( new Courses(370,5/20/2020) );
list.add( new Courses(370,5/19/2018) );
list.add( new Courses(360,4/10/2016) );
public class Courses{
int coursenum;
Date date;
}
如何删除元素,以便arraylist仅包含具有最新日期的那些元素?
How can I remove elements so that the arraylist contains only those elements with the latest date?
应该这样;
350, 5/20/2020}
360,6/20/2020}
370, 5/20/2020
推荐答案
通过ThreeTen Backport的Java 1.7和java.time
ArrayList<Course> list = new ArrayList<>();
list.add( new Course(350, "5/20/2020") );
list.add( new Course(350, "4/20/2019") );
list.add( new Course(350, "3/20/2018") );
list.add( new Course(360, "6/20/2020") );
list.add( new Course(360, "5/20/2019") );
list.add( new Course(370, "5/20/2020") );
list.add( new Course(370, "5/19/2018") );
list.add( new Course(360, "4/10/2016") );
// Find latest date for each course number
Map<Integer, LocalDate> latestDates = new HashMap<>();
for (Course currentCourse : list) {
LocalDate latestHitherto = latestDates.get(currentCourse.getCourseNumber());
if (latestHitherto == null || currentCourse.getDate().isAfter(latestHitherto)) {
latestDates.put(currentCourse.getCourseNumber(), currentCourse.getDate());
}
}
// Remove courses that haven’t got the latest date
Iterator<Course> courseIterator = list.iterator();
while (courseIterator.hasNext()) {
Course currentCourse = courseIterator.next();
if (currentCourse.getDate().isBefore(latestDates.get(currentCourse.getCourseNumber()))) {
courseIterator.remove();
}
}
// Print result
for (Course currentCourse : list) {
System.out.format("%3d %s%n",
currentCourse.getCourseNumber(),
currentCourse.getDate().format(Course.dateFormatter));
}
此代码段的输出为:
350 5/20/2020
360 6/20/2020
370 5/20/2020
我在jdk1.7.0_67上运行.我添加了ThreeTen Backport 1.3.6.请参阅下面的链接.
I ran on jdk1.7.0_67. I had added ThreeTen Backport 1.3.6. See the link below.
这是我使用的 Course
类:
public class Course {
public static final DateTimeFormatter dateFormatter
= DateTimeFormatter.ofPattern("M/d/u");
private int courseNumber;
private LocalDate date;
public Course(int courseNumber, String dateString) {
this.courseNumber = courseNumber;
this.date = LocalDate.parse(dateString, dateFormatter);
}
public int getCourseNumber() {
return courseNumber;
}
public LocalDate getDate() {
return date;
}
}
如果您坚持使用 java.util.Date
(一个设计不良且过时的类),(1)我不明白为什么会这样,(2)您可以将我的代码修改为改用 Date
.
If you insist on using java.util.Date
— a poorly designed and long outdated class — (1) I would not understand why you would, (2) you can probably modify my code to use Date
instead.
您可能需要考虑升级Java版本.Java 15已经发布,并且是Java 16的早期访问版本.Java8已经出现了流.它们将使您可以用更少的代码行来获得相同的代码:
You may want consider upgrading your Java version. Java 15 is out, and an early access edition of Java 16. Already in Java 8 comes streams. They will allow you to obtain the same in much fewer lines of code:
Collection<Course> filteredCourses = list.stream()
.collect(Collectors.groupingBy(Course::getCourseNumber,
Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(Course::getDate)),
Optional::orElseThrow)))
.values();
List<Course> filteredList = new ArrayList<>(filteredCourses);
filteredList.forEach(c -> System.out.format(
"%3d %s%n", c.getCourseNumber(), c.getDate().format(Course.dateFormatter)));
370 5/20/2020
360 6/20/2020
350 5/20/2020
按照代码的原样,它在结果列表中给出的课程顺序并不相同,并且需要Java 10(此处介绍了覆盖过的no-arg orElseThrow
方法).即使您需要相同的订单,流仍然将非常有帮助.
As the code stands it doesn’t give the same order of courses in the resulting list, and it requires Java 10 (the overlaoded no-arg orElseThrow
method was introduced there). Even if you require the same order, streams will still be extremely helpful.
java.time在旧的和更新的Android设备上均可正常运行.它只需要至少 Java 6 .
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
- 在Java 8和更高版本以及更新的Android设备(API级别26起)中,内置了现代API.
- 在非Android Java 6和7中,获得ThreeTen反向端口,即现代类的反向端口(JSR 310的ThreeTen;请参见底部的链接).
- 在较旧的Android上,请使用废除旧书或Android版本的ThreeTen Backport.称为ThreeTenABP.在后一种情况下,请确保使用子包从
org.threeten.bp
导入日期和时间类.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bp
with subpackages.
- Oracle教程:日期时间解释了如何使用java.time./li>
- Java规范请求(JSR)310 ,其中
java首先描述了.time
. - ThreeTen Backport项目,即
java.time
的反向端口Java 6和7(JSR-310的ThreeTen). - 可以通过重复使用Java 8+ API
- ThreeTenABP ,ThreeTen Backport的Android版本
- 问题:如何在Android Project中使用ThreeTenABP ,非常详尽的解释.
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - Java 8+ APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
这篇关于Java Arraylist对象按日期从arraylist中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!