简体   繁体   中英

what is the best way to maintain a list of Transactions in java (transaction object having fields like id, date, amount etc)

What is the best way to maintain a list of Transactions (transaction object having fields like id, date, amount etc) so that i can fetch results based on different criteria like

  • last 10 transactions
  • transaction between 2 dates
  • transactions by year
  • etc..

You can use the Java 8 Stream API.

Say transactions is a List<Transaction> , you can use, for example:

List<Transaction> validTrxs = transactions.stream()
                               .filter( (Transaction tr) -> tr.isValid() )
                               .collect( Collectors.toList() );

(Instead of tr.isValid() you could put practically any condition you can make into a boolean expression)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM