提交 c2bc4073 编写于 作者: 武汉红喜's avatar 武汉红喜

lambda

上级 d289e0d9
package org.hongxi.whatsmars.base.lambda.collection;
import java.util.function.Supplier;
/**
* Created by shenhongxi on 2018/1/11.
*/
public interface CollectionFactory {
static MyCollection create(Supplier<MyCollection> supplier) {
return supplier.get();
}
}
package org.hongxi.whatsmars.base.lambda.collection;
/**
* Created by shenhongxi on 2018/1/11.
*/
public interface MyCollection {
int size();
default String notRequired() {
return "Default implementation";
}
}
package org.hongxi.whatsmars.base.lambda.collection;
/**
* Created by shenhongxi on 2018/1/11.
*/
public class MyList implements MyCollection {
@Override
public int size() {
return 0;
}
}
package org.hongxi.whatsmars.base.lambda.collection;
/**
* Created by shenhongxi on 2018/1/11.
*/
public class NewList implements MyCollection {
@Override
public int size() {
return 0;
}
@Override
public String notRequired() {
return "Overridden implementation";
}
}
package org.hongxi.whatsmars.base.lambda.collection;
/**
* Created by shenhongxi on 2018/1/11.
*/
public class Test {
public static void main(String[] args) {
MyCollection collection = CollectionFactory.create(MyList::new);
System.out.println(collection.notRequired());
collection = CollectionFactory.create(NewList::new);
System.out.println(collection.notRequired());
}
}
package org.hongxi.whatsmars.base.lambda.stream;
/**
* Created by shenhongxi on 2018/1/11.
*/
public enum Status {
OPEN, CLOSED
}
package org.hongxi.whatsmars.base.lambda.stream;
/**
* Created by shenhongxi on 2018/1/11.
*/
public class Task {
private Status status;
private int point;
public Task() {
}
public Task(Status status, int point) {
this.status = status;
this.point = point;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
@Override
public String toString() {
return String.format("(%s, %d)", status, point);
}
}
package org.hongxi.whatsmars.base.lambda.stream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Created by shenhongxi on 2018/1/11.
*/
public class Test {
public static void main(String[] args) {
Collection<Task> tasks = Arrays.asList(
new Task(Status.OPEN, 5),
new Task(Status.OPEN, 13),
new Task(Status.CLOSED, 8)
);
int openTaskPoints = tasks
.stream()
.filter(task -> task.getStatus() == Status.OPEN)
.mapToInt(Task::getPoint)
.sum();
System.out.println(openTaskPoints);
final int totalPoints = tasks
.stream()
.parallel()
.map(Task::getPoint)
.reduce(0, Integer::sum);
System.out.println(totalPoints);
Map<Status, List<Task>> map = tasks.stream().collect(Collectors.groupingBy(Task::getStatus));
System.out.println(map);
Collection<String> result = tasks
.stream()
.mapToInt(Task::getPoint)
.mapToDouble(point -> point * 100 / totalPoints)
.mapToObj(percentage -> (int) percentage + "%")
.collect(Collectors.toList());
System.out.println(result);
}
}
......@@ -2,13 +2,109 @@ package org.hongxi.whatsmars.base.lambda;
import org.junit.Test;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.nio.charset.StandardCharsets;
import java.time.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
/**
* Created by javahongxi on 2017/6/24.
* Created by javahongxi on 2018/1/2.
*/
public class LambdaTest {
@Test
public void t1() {
new Thread(() -> System.out.println("hi,lambda!")).start();
public void t() {
new Thread(() -> System.out.println("Hi, I am lambda!")).start();
System.out.println("main thread ...");
}
@Test
public void t2() {
Arrays.asList("a", "b", "c").forEach(e -> System.out.println(e));
}
@Test
public void t3() {
Arrays.asList("a", "b", "c").forEach(e -> {
System.out.println(System.currentTimeMillis());
System.out.println(e);
});
}
@Test
public void t4() {
List<String> names = new ArrayList<>();
names.add("d");
names.add("a");
names.add("ax");
names.add("bbb");
names.sort(String::compareTo);
System.out.println(names);
}
@Test
public void t5() {
Optional<String> fullName = Optional.ofNullable(null);
System.out.println("Full Name is set? " + fullName.isPresent());
System.out.println("Full Name: " + fullName.orElseGet(() -> "[none]"));
System.out.println(fullName.map(s -> "Hey " + s + "!").orElse("Hey Stranger!"));
}
@Test
public void t6() {
long[] arr = new long [20000];
Arrays.parallelSetAll(arr,
index -> ThreadLocalRandom.current().nextInt(1000000));
Arrays.stream(arr).limit(10).forEach(
i -> System.out.print(i + " "));
System.out.println();
Arrays.parallelSort(arr);
Arrays.stream(arr).limit(10).forEach(
i -> System.out.print(i + " "));
System.out.println();
}
@Test
public void t7() {
String text = "Base64 finally in Java 8!";
String encoded = Base64
.getEncoder()
.encodeToString(text.getBytes(StandardCharsets.UTF_8));
System.out.println(encoded);
String decoded = new String(Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
System.out.println(decoded);
}
@Test
public void t8() throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
System.out.println(engine.getClass().getName());
System.out.println("Result:" + engine.eval("function f() { return 1; }; f() + 1;"));
}
@Test
public void t9() {
Clock clock = Clock.systemUTC();
System.out.println(clock.instant());
System.out.println(clock.millis());
LocalDate localDate = LocalDate.now(); // LocalDate.now(clock)
System.out.println(localDate);
LocalTime localTime = LocalTime.now(); // LocalTime.now(clock)
System.out.println(localTime);
LocalDateTime localDateTime = LocalDateTime.now(); // LocalDateTime.now(clock)
System.out.println(localDateTime);
LocalDateTime from = LocalDateTime.of( 2014, Month.APRIL, 16, 0, 0, 0 );
LocalDateTime to = LocalDateTime.of( 2015, Month.APRIL, 16, 23, 59, 59 );
Duration duration = Duration.between(from, to);
System.out.println("Duration in days: " + duration.toDays());
System.out.println("Duration in hours: " + duration.toHours());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册