Last active
March 21, 2017 08:27
-
-
Save iamlow/2b877d3a742c92b26a53cf20e2d8ea41 to your computer and use it in GitHub Desktop.
Revisions
-
iamlow revised this gist
Mar 21, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -38,5 +38,5 @@ public static void main(String ...args) { // Query 8: What's the lowest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::min)); } } -
iamlow revised this gist
Mar 21, 2017 . 1 changed file with 26 additions and 26 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,39 +4,39 @@ public static void main(String ...args) { Trader mario = new Trader("Mario","Milan"); Trader alan = new Trader("Alan","Cambridge"); Trader brian = new Trader("Brian","Cambridge"); List<Transaction> transactions = Arrays.asList( new Transaction(brian, 2011, 300), new Transaction(raoul, 2012, 1000), new Transaction(raoul, 2011, 400), new Transaction(mario, 2012, 710), new Transaction(mario, 2012, 700), new Transaction(alan, 2012, 950) ); // Query 1: Find all transactions from year 2011 and sort them by value (small to high). transactions.stream().filter(t -> t.getYear() == 2011).sorted(comparing(Transaction::getValue)).forEach(System.out::println); // Query 2: What are all the unique cities where the traders work? transactions.stream().map(t -> t.getTrader().getCity()).distinct().forEach(System.out::println); // Query 3: Find all traders from Cambridge and sort them by name. transactions.stream().map(Transaction::getTrader).filter(t -> t.getCity().equals("Cambridge")).distinct().sorted(comparing(Trader::getName)).forEach(System.out::println); // Query 4: Return a string of all traders’ names sorted alphabetically. System.out.println(transactions.stream().map(t -> t.getTrader().getName()).distinct().sorted().reduce("", (n1, n2) -> n1 + n2) + "\n"); // Query 5: Are there any trader based in Milan? System.out.println(transactions.stream().anyMatch(t -> t.getTrader().getCity().equals("Milan"))); // Query 6: Update all transactions so that the traders from Milan are set to Cambridge. Optional<Transaction> trans = transactions.stream().filter(t -> t.getTrader().getCity().equals("Cambridge")).findAny(); System.out.println(trans); // Query 7: What's the highest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::max)); // Query 8: What's the lowest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::min)); } } -
iamlow revised this gist
Mar 21, 2017 . No changes.There are no files selected for viewing
-
iamlow revised this gist
Mar 21, 2017 . 1 changed file with 17 additions and 17 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,42 +1,42 @@ public class StreamAPI { public static void main(String ...args) { Trader raoul = new Trader("Raoul", "Cambridge"); Trader mario = new Trader("Mario","Milan"); Trader alan = new Trader("Alan","Cambridge"); Trader brian = new Trader("Brian","Cambridge"); List<Transaction> transactions = Arrays.asList( new Transaction(brian, 2011, 300), new Transaction(raoul, 2012, 1000), new Transaction(raoul, 2011, 400), new Transaction(mario, 2012, 710), new Transaction(mario, 2012, 700), new Transaction(alan, 2012, 950) ); // Query 1: Find all transactions from year 2011 and sort them by value (small to high). transactions.stream().filter(t -> t.getYear() == 2011).sorted(comparing(Transaction::getValue)).forEach(System.out::println); // Query 2: What are all the unique cities where the traders work? transactions.stream().map(t -> t.getTrader().getCity()).distinct().forEach(System.out::println); // Query 3: Find all traders from Cambridge and sort them by name. transactions.stream().map(Transaction::getTrader).filter(t -> t.getCity().equals("Cambridge")).distinct().sorted(comparing(Trader::getName)).forEach(System.out::println); // Query 4: Return a string of all traders’ names sorted alphabetically. System.out.println(transactions.stream().map(t -> t.getTrader().getName()).distinct().sorted().reduce("", (n1, n2) -> n1 + n2) + "\n"); // Query 5: Are there any trader based in Milan? System.out.println(transactions.stream().anyMatch(t -> t.getTrader().getCity().equals("Milan"))); // Query 6: Update all transactions so that the traders from Milan are set to Cambridge. Optional<Transaction> trans = transactions.stream().filter(t -> t.getTrader().getCity().equals("Cambridge")).findAny(); System.out.println(trans); // Query 7: What's the highest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::max)); // Query 8: What's the lowest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::min)); } } -
iamlow revised this gist
Mar 21, 2017 . No changes.There are no files selected for viewing
-
iamlow revised this gist
Mar 21, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -38,5 +38,5 @@ public static void main(String ...args){ // Query 8: What's the lowest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::min)); } } -
iamlow created this gist
Mar 21, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ public class StreamAPI { public static void main(String ...args){ Trader raoul = new Trader("Raoul", "Cambridge"); Trader mario = new Trader("Mario","Milan"); Trader alan = new Trader("Alan","Cambridge"); Trader brian = new Trader("Brian","Cambridge"); List<Transaction> transactions = Arrays.asList( new Transaction(brian, 2011, 300), new Transaction(raoul, 2012, 1000), new Transaction(raoul, 2011, 400), new Transaction(mario, 2012, 710), new Transaction(mario, 2012, 700), new Transaction(alan, 2012, 950) ); // Query 1: Find all transactions from year 2011 and sort them by value (small to high). transactions.stream().filter(t -> t.getYear() == 2011).sorted(comparing(Transaction::getValue)).forEach(System.out::println); // Query 2: What are all the unique cities where the traders work? transactions.stream().map(t -> t.getTrader().getCity()).distinct().forEach(System.out::println); // Query 3: Find all traders from Cambridge and sort them by name. transactions.stream().map(Transaction::getTrader).filter(t -> t.getCity().equals("Cambridge")).distinct().sorted(comparing(Trader::getName)).forEach(System.out::println); // Query 4: Return a string of all traders’ names sorted alphabetically. System.out.println(transactions.stream().map(t -> t.getTrader().getName()).distinct().sorted().reduce("", (n1, n2) -> n1 + n2) + "\n"); // Query 5: Are there any trader based in Milan? System.out.println(transactions.stream().anyMatch(t -> t.getTrader().getCity().equals("Milan"))); // Query 6: Update all transactions so that the traders from Milan are set to Cambridge. Optional<Transaction> trans = transactions.stream().filter(t -> t.getTrader().getCity().equals("Cambridge")).findAny(); System.out.println(trans); // Query 7: What's the highest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::max)); // Query 8: What's the lowest value in all the transactions? System.out.println(transactions.stream().map(Transaction::getValue).reduce(Integer::min)); } }