Created
April 28, 2017 16:23
-
-
Save solaris33/91d65a5d203d1a60431d2b5379f9d0e9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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