Skip to content

Commit 20dc058

Browse files
committed
Adding Exercise 5.5 Java8 In Action
1 parent 16d589e commit 20dc058

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
*
3+
*/
4+
package com.java8in.action.ch5;
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
/**
12+
* @author Sanket Gupta
13+
*
14+
*/
15+
public class TestClass {
16+
17+
/**
18+
* @param args
19+
*/
20+
public static void main(String[] args) {
21+
Trader raoul = new Trader("Raoul", "Cambridge");
22+
Trader mario = new Trader("Mario", "Milan");
23+
Trader alan = new Trader("Alan", "Cambridge");
24+
Trader brain = new Trader("Brain", "Cambridge");
25+
26+
List<Transaction> transactions = Arrays.asList(
27+
new Transaction(brain, 2011, 300),
28+
new Transaction(raoul, 2012, 1000),
29+
new Transaction(raoul, 2011, 400),
30+
new Transaction(mario, 2012, 710),
31+
new Transaction(mario, 2012, 700),
32+
new Transaction(alan, 2012, 950));
33+
34+
35+
// 1. Find all transactions in the year 2011 and sort them by value (small to high).
36+
System.out.println("Answer 1:");
37+
transactions.stream().filter( t->t.getYear()==2011).sorted((t1,t2)->Integer.compare(t1.getValue(), t2.getValue())).forEach(t3-> System.out.println(t3));
38+
transactions.stream().filter( t->t.getYear()==2011).sorted(Comparator.comparing(Transaction::getValue)).forEach(t3-> System.out.println(t3));//.collect(Collectors.toList());
39+
40+
// 2. What are all the unique cities where the traders work?
41+
System.out.println("Answer 2:");
42+
transactions.stream().map(t->t.getTrader().getCity()).distinct().forEach(c->System.out.println(c));
43+
44+
// 3. Find all traders from Cambridge and sort them by name.
45+
System.out.println("Answer 3:");
46+
transactions.stream().filter(t->t.getTrader().getCity()=="Cambridge").map(t->t.getTrader()).sorted(Comparator.comparing(Trader::getName)).distinct().forEach(trader->System.out.println(trader));//.collect(Collectors.toList());
47+
48+
// 4. Return a string of all traders's names sorted alphabetically.
49+
System.out.println("Answer 4:");
50+
// String names=transactions.stream().map(tx->tx.getTrader().getName()).distinct().sorted().reduce("",(n1,n2)->n1+n2);
51+
String names=transactions.stream().map(tx->tx.getTrader().getName()).distinct().sorted().collect(Collectors.joining());
52+
System.out.println(names);
53+
// transactions.stream().map(tx->tx.getTrader().getName()).sorted().collect(joining());
54+
55+
// 5. Are any traders based in Milan?
56+
System.out.println("Answer 5:");
57+
Boolean isMialBased=transactions.stream().anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
58+
System.out.println(isMialBased);
59+
60+
// 6. Print all transactions' values from the traders living in Cambridge.
61+
System.out.println("Answer 6:");
62+
transactions.stream().filter(t->t.getTrader().getCity()=="Cambridge").map(Transaction::getValue).distinct().forEach(tradeValue->System.out.println(tradeValue));
63+
64+
// 7. What's the highest value of all the transactions?
65+
System.out.println("Answer 7:");
66+
System.out.println(transactions.stream().map(tx->tx.getValue()).max(Comparator.comparing(Integer::intValue)));
67+
// 8. Find the transaction with the smallest value.
68+
System.out.println("Answer 8:");
69+
System.out.println(transactions.stream().min(Comparator.comparing(Transaction::getValue)));
70+
}
71+
72+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
*
3+
*/
4+
package com.java8in.action.ch5;
5+
6+
/**
7+
* @author Sanket Gupta
8+
*
9+
*/
10+
public class Trader {
11+
12+
private final String name;
13+
private final String city;
14+
/**
15+
* @param name
16+
* @param city
17+
*/
18+
public Trader(String name, String city) {
19+
this.name = name;
20+
this.city = city;
21+
}
22+
/**
23+
* @return the name
24+
*/
25+
public String getName() {
26+
return name;
27+
}
28+
/**
29+
* @return the city
30+
*/
31+
public String getCity() {
32+
return city;
33+
}
34+
/* (non-Javadoc)
35+
* @see java.lang.Object#toString()
36+
*/
37+
@Override
38+
public String toString() {
39+
return "Trader [name=" + name + ", city=" + city + "]";
40+
}
41+
42+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
*
3+
*/
4+
package com.java8in.action.ch5;
5+
6+
/**
7+
* @author Sanket Gupta
8+
*
9+
*/
10+
public class Transaction {
11+
12+
private final Trader trader;
13+
private final int year;
14+
private final int value;
15+
/**
16+
* @param trader
17+
* @param year
18+
* @param value
19+
*/
20+
public Transaction(Trader trader, int year, int value) {
21+
this.trader = trader;
22+
this.year = year;
23+
this.value = value;
24+
}
25+
/**
26+
* @return the trader
27+
*/
28+
public Trader getTrader() {
29+
return trader;
30+
}
31+
/**
32+
* @return the year
33+
*/
34+
public int getYear() {
35+
return year;
36+
}
37+
/**
38+
* @return the value
39+
*/
40+
public int getValue() {
41+
return value;
42+
}
43+
44+
/* (non-Javadoc)
45+
* @see java.lang.Object#toString()
46+
*/
47+
@Override
48+
public String toString() {
49+
return "Transaction [trader=" + trader + ", year=" + year + ", value=" + value + "]";
50+
}
51+
52+
}

0 commit comments

Comments
 (0)