Skip to content

Instantly share code, notes, and snippets.

@sohlich
Created October 4, 2015 05:10
Show Gist options
  • Save sohlich/7fab095f60991d1847eb to your computer and use it in GitHub Desktop.
Save sohlich/7fab095f60991d1847eb to your computer and use it in GitHub Desktop.
//Old School
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());
}
//Streams
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