在两个日期之间筛选一个ArrayList。

huangapple go评论83阅读模式
英文:

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&lt;Receipts&gt; sortListByDate(String date1,String date2,List&lt;Receipts&gt; receipts){

   List&lt;Receipts&gt; sortedList = new ArrayList&lt;&gt;();

  // 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:

  1. Parse the date stings using DateTimeFormatter.
  2. Create a stream from receipts and apply filter with the required criteria.
  3. Sort the resulting stream.
  4. Collect the resulting stream into a List and return the same.

Code:

public List&lt;Receipts&gt; sortListByDate(String date1, String date2, List&lt;Receipts&gt; receipts) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;dd-MM-yyyy&quot;);
    LocalDate localDate1 = LocalDate.parse(date1, formatter);
    LocalDate localDate2 = LocalDate.parse(date2, formatter);

    return receipts.stream()
            .filter(e -&gt; !LocalDate.parse(e.date(), formatter).isBefore(localDate1)
                    &amp;&amp; !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

huangapple
  • 本文由 发表于 2020年8月5日 23:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63268165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定