英文:
Filter an ArrayList to between two dates
问题
Here's the translated code portion:
我有一个ArrayList "List<Receipts>"
Receipts{
int invoice;
String date;
double amount;
....
}
我想要筛选List<Receipts>在两个日期之间。
public List<Receipts> sortListByDate(String date1, String date2, List<Receipts> receipts){
List<Receipts> sortedList = new ArrayList<>();
// 如果receipts.date()在date1和date2之间(包括这两个日期),则将该对象添加到sortedList
return sortedList;
}
receipt.date(),date1和date2的格式都是字符串 "dd-MM-yyyy"
This is the translated code portion as requested.
英文:
I have a ArrayList "List<Receipts>"
Receipts{
int invoice;
String date;
double amount;
....
}
i want to filter the List<Receipts> to between two dates.
public List<Receipts> sortListByDate(String date1,String date2,List<Receipts> receipts){
List<Receipts> sortedList = new ArrayList<>();
// if receipts.date() is between(or including) date1 and date2 then sortedList.add(that object)
return sortedList;
}
receipt.date() , date1 and date2 are in the format String "dd-MM-yyyy"
Help me to filter (that is, to complete the "sortListByDate" function)
答案1
得分: 1
Sure, here's the translated code snippet:
public List<Receipts> sortListByDate(String date1, String date2, List<Receipts> receipts) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate1 = LocalDate.parse(date1, formatter);
LocalDate localDate2 = LocalDate.parse(date2, formatter);
return receipts.stream()
.filter(e -> !LocalDate.parse(e.date(), formatter).isBefore(localDate1)
&& !LocalDate.parse(e.date(), formatter).isAfter(localDate2))
.sorted()
.collect(Collectors.toList());
}
Please note that the translation might not be perfect due to the limitations of automated translation. It's recommended to review the code to ensure accuracy.
英文:
From the stream of receipts
, you need to filter the elements based on dates (i.e. not before date1
and not after date2
), sort the resulting stream, collect it into a List
and return the same.
Steps:
- Parse the date stings using
DateTimeFormatter
. - Create a stream from
receipts
and applyfilter
with the required criteria. - Sort the resulting stream.
- Collect the resulting stream into a
List
and return the same.
Code:
public List<Receipts> sortListByDate(String date1, String date2, List<Receipts> receipts) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate1 = LocalDate.parse(date1, formatter);
LocalDate localDate2 = LocalDate.parse(date2, formatter);
return receipts.stream()
.filter(e -> !LocalDate.parse(e.date(), formatter).isBefore(localDate1)
&& !LocalDate.parse(e.date(), formatter).isAfter(localDate2))
.sorted()
.collect(Collectors.toList());
}
答案2
得分: 1
你可以在Receipts类中实现Comparable,并覆盖compareTo方法,可以按照你想要的方式来实现。然后在你的列表上调用sort方法。详细信息可以查看文档:
https://www.javatpoint.com/Comparable-interface-in-collection-framework
英文:
you can implements Comparable in Receipts class and overRide the compareTo method by any thing you want
and after that just call sort method on your list
see the docs for more information to how to do it:
https://www.javatpoint.com/Comparable-interface-in-collection-framework
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论