Skip to content

Instantly share code, notes, and snippets.

@solaris33
Created April 28, 2017 16:23
Show Gist options
  • Save solaris33/91d65a5d203d1a60431d2b5379f9d0e9 to your computer and use it in GitHub Desktop.
Save solaris33/91d65a5d203d1a60431d2b5379f9d0e9 to your computer and use it in GitHub Desktop.
// Traditional Method
List<Transaction> groceryTransactions = new Arraylist<>();
for(Transaction t: transactions){
if(t.getType() == Transaction.GROCERY){
groceryTransactions.add(t);
}
}
Collections.sort(groceryTransactions, new Comparator(){
public int compare(Transaction t1, Transaction t2){
return t2.getValue().compareTo(t1.getValue());
}
});
List<Integer> transactionIds = new ArrayList<>();
for(Transaction t: groceryTransactions){
transactionsIds.add(t.getId());
}
// Using Stream API
List<Integer> transactionsIds =
transactions.stream()
.filter(t -> t.getType() == Transaction.GROCERY)
.sorted(comparing(Transaction::getValue).reversed())
.map(Transaction::getId)
.collect(toList());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment