Created
October 4, 2015 05:10
-
-
Save sohlich/7fab095f60991d1847eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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