Exercises 1
Given a list of numbers, how can I return a list of the squares of each number?
, given [1, 2, 3, 4, 5], it should return [1, 4, 9, 16, 25]
public class Problem1 { public static void main(String[] args) { // TODO Auto-generated method stub List<Integer> list1 = Arrays.asList(1,2,3,4,5); List<Integer> list2 = list1.stream().map((x)->x*x).collect(Collectors.toList()); System.out.println(list2); } }
Exercise two
Trader attribute (name, city)
Transaction attribute of transaction type (Trader, year, value)
public class TestTransaction { List<Transaction> transaction=null; @Before public void before(){ Trader raoul=new Trader("Raoul","Cambridge"); Trader mario=new Trader("Mario","Milan"); Trader alan=new Trader("Alan","Cambridge"); Trader brian=new Trader("Brian","Cambridge"); transaction=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) ); }
1. Find out all transactions occurred in 2011, and sort by transaction amount (from low to high)
2. What different cities have traders worked in?
3. Find all traders from Cambridge and sort by name
4. Return the name strings of all traders in alphabetical order
5. Are there any traders working in Milan?
6. Print all trading volume of traders living in Cambridge
7. What is the highest trading volume of all transactions?
8. Find the transaction with the smallest transaction amount
import java.util.Arrays; import java.util.List; import java.util.Optional; import org.junit.Before; import org.junit.Test; public class TestTransaction { List<Transaction> transaction = null; @Before public void before() { Trader raoul = new Trader("Raoul", "Cambridge"); Trader mario = new Trader("Mario", "Milan"); Trader alan = new Trader("Alan", "Cambridge"); Trader brian = new Trader("Brian", "Cambridge"); transaction = 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)); } @Test public void test1() { transaction.stream().filter(x -> x.getYear() == 2011).sorted((x1, x2) -> { return x1.getValue() - x2.getValue(); }).forEach(System.out::println); } @Test public void test2() { transaction.stream() .map(x -> (x.getTrader().getName() + " " + x.getTrader().getCity())) .distinct() .forEach(System.out::println); } @Test public void test3() { transaction.stream().filter((x) -> x.getTrader().getCity() == "Cambridge") .map((x)->x.getTrader()) .sorted((x1, x2) -> x1.getName().compareTo(x2.getName())) .distinct() .forEach(System.out::println); } @Test public void test4(){ transaction.stream().map(x->x.getTrader().getName()) .sorted((x1,x2)->x1.compareTo(x2)) .distinct() .forEach(System.out::println); } @Test public void test5(){ boolean b = transaction.stream().map((x)->x.getTrader().getCity()) .distinct() .anyMatch((x)->x.equals("Milan")); System.out.println(b); } @Test public void test6(){ Optional<Integer> op = transaction.stream().filter((x)->x.getTrader().getCity().equals("Cambridge")) .map((x)->x.getValue()) .reduce(Integer::sum); System.out.println(op.get()); } @Test public void test7(){ Optional<Integer> mmax = transaction.stream().map((x)->x.getValue()) .sorted((x1,x2)->-x1.compareTo(x2)) .findFirst(); System.out.println(mmax.get()); } @Test public void test8(){ Optional<Integer> mmin = transaction.stream().map((x)->x.getValue()) .sorted() .findFirst(); //System.out.println(mmin.get()); Optional<Transaction> ot = transaction.stream().filter((x)->x.getValue()==mmin.get()) .findFirst(); System.out.println(ot.get()); } }