# 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

Transaction attribute of transaction type (Trader, year, value)

```public class TestTransaction {
List<Transaction> transaction=null;
@Before
public void before(){

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)
);
}

```

## 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() {

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()
.distinct()
.forEach(System.out::println);
}

@Test
public void test3() {
.sorted((x1, x2) -> x1.getName().compareTo(x2.getName()))
.distinct()
.forEach(System.out::println);
}
@Test
public void test4(){
.sorted((x1,x2)->x1.compareTo(x2))
.distinct()
.forEach(System.out::println);

}
@Test
public void test5(){
.distinct()
.anyMatch((x)->x.equals("Milan"));
System.out.println(b);
}
@Test
public void test6(){
.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());

}
}

```

Keywords: Java Attribute Junit

Added by kincet7 on Sun, 01 Dec 2019 16:29:10 +0200