Feladatok:
interface MathOperation {
int operation(int a, int b);
}
interface GreetingService {
void sayMessage(String message);
}
int operate(int a, int b, MathOperation mathOperation) {
return mathOperation.operation(a, b);
}
MathOperation addition = (int a, int b) -> a + b;
MathOperation subtraction = (a, b) -> a - b;
MathOperation multiplication = (int a, int b) -> {
return a * b;
};
MathOperation division = (int a, int b) -> a / b;
System.out.println("10 + 5 = " + operate(10, 5, addition));
System.out.println("10 - 5 = " + operate(10, 5, subtraction));
System.out.println("10 x 5 = " + operate(10, 5, multiplication));
System.out.println("10 / 5 = " + operate(10, 5, division));
GreetingService greetService1 = message -> System.out.println("Hello " + message);
GreetingService greetService2 = (message) -> System.out.println("Hello " + message);
greetService1.sayMessage("Wolrd!");
greetService2.sayMessage("Peter");
import java.math.BigDecimal;
public class Developer {
public String name;
public BigDecimal salary;
public int age;
public Developer(String name, BigDecimal salary, int age) {
this.name = name;
this.salary = salary;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Developer [name=" + name + ", salary" + salary + ", age=" + age + "]";
}
}
import java.math.BigDecimal;
List<Developer> getDevelopers() {
List<Developer> result = new ArrayList<Developer>();
result.add(new Developer("mike", new BigDecimal("70000"), 33));
result.add(new Developer("alvin", new BigDecimal("80000"), 20));
result.add(new Developer("jason", new BigDecimal("100000"), 10));
result.add(new Developer("iris", new BigDecimal("170000"), 55));
return result;
}
List<Developer> listDevs = getDevelopers();
System.out.println("Before Sort");
for (Developer developer : listDevs) {
System.out.println(developer);
}
System.out.println("After Sort");
listDevs.sort((Developer o1, Developer o2) -> o1.getAge() - o2.getAge());
listDevs.forEach((developer) -> System.out.println(developer));
System.out.println("After Sort by name");
listDevs.sort((Developer o1, Developer o2) -> o1.getName().compareTo(o2.getName()));
listDevs.forEach(System.out::println);
Feladatok:
import java.util.stream.*;
public class Product {
private int id;
private String name;
public Product(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
List<String> words = Arrays.asList(new String[] { "hello", "hola", "hallo", "ciao" });
Stream<String> stream = words.stream();
stream.forEach(System.out::println);
Stream<String> s = Stream.of("m", "k", "c", "t").sorted().limit(3);
s.forEach(System.out::println);
List<Integer> list = Arrays.asList(57, 38, 37, 54, 2);
list.stream().sorted().forEach(System.out::println);
int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
IntStream is = IntStream.of(digits);
System.out.println(is.count());
System.out.println(IntStream.of(digits).findFirst());
words = Arrays.asList("hello", null, "");
words.stream().filter(t -> t != null) // ["hello", ""]
.filter(t -> !t.isEmpty()) // ["hello"]
.forEach(System.out::println);
IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7);
System.out.println(stream.anyMatch(i -> i % 3 == 0));
List<String> strings = Arrays.asList("Stream", "Operations", "on", "Collections");
strings.stream().min(Comparator.comparing((String x) -> x.length())).ifPresent(System.out::println);
int reducedParams = Stream.of(1, 2, 3).reduce(0, (a, b) -> a + b);
System.out.println(reducedParams);
List<Product> productList = Arrays.asList(
new Product(23, "potatoes"),
new Product(14, "orange"),
new Product(13, "lemon"),
new Product(23, "bread"),
new Product(13, "sugar"));
productList.stream().map(Product::getName).forEach(System.out::println);
System.out.println();
List<String> collectorCollection = productList.stream().map(Product::getName).collect(Collectors.toList());
for(String str : collectorCollection) {
System.out.println(str);
}
import java.util.function.Predicate;
void filter(List<String> names, Predicate<String> condition) {
for (String name : names) {
if (condition.test(name)) {
System.out.println(name + " ");
}
}
}
List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
System.out.println("Languages which starts with J :");
filter(languages, (str) -> str.startsWith("J"));
System.out.println("Languages which ends with a ");
filter(languages, (str) -> str.endsWith("a"));
System.out.println("Print all languages :");
filter(languages, (str) -> true);
System.out.println("Print no language : ");
filter(languages, (str) -> false);
System.out.println("Print language whose length greater than 4:");
filter(languages, (str) -> str.length() > 4);
Predicate<String> startsWithJ = (n) -> n.startsWith("J");
Predicate<String> fourLetterLong = (n) -> n.length() == 4;
languages.stream().filter(startsWithJ.and(fourLetterLong))
.forEach((n) -> System.out.println("\nLanguage, which starts with 'J' and four letter long is : " + n));
// Map reduce
List<Developer> devs = new ArrayList<Developer>();
devs.add(new Developer("jason", new BigDecimal("100000"), 10));
devs.add(new Developer("alvin", new BigDecimal("80000"), 20));
devs.add(new Developer("iris", new BigDecimal("170000"), 55));
devs.add(new Developer("mike", new BigDecimal("70000"), 33));
devs.stream().map((dev) -> dev.getSalary().multiply(new BigDecimal(1.12)).intValue()).sorted()
.forEach(System.out::println);
double total = devs.stream().map((dev) -> dev.getSalary().multiply(new BigDecimal(1.12)).intValue())
.reduce((sum, salary) -> sum + salary).get();
System.out.println("Total : " + total);