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

java

上级 44792216
package org.hongxi.java.util;
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.Base64;
public class Java8Test {
@Test
public void join() {
String[] countries = {"China", "America", "Japan"};
System.out.println(String.join(",", countries));
}
@Test
public void base64() {
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 script() 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 clock() {
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());
}
}
package org.hongxi.java.util;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
/**
* Created by javahongxi on 2018/1/2.
*/
public class LambdaTests {
@Test
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();
}
}
package org.hongxi.java.util.function;
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.java.util.function;
/**
* Created by shenhongxi on 2018/1/11.
*/
public interface MyCollection {
int size();
default String notRequired() {
return "Default implementation";
}
}
package org.hongxi.java.util.function;
/**
* Created by shenhongxi on 2018/1/11.
*/
public class MyList implements MyCollection {
@Override
public int size() {
return 0;
}
}
package org.hongxi.java.util.function;
/**
* 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.java.util.function;
/**
* 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.java.util.stream;
/**
* Created by shenhongxi on 2018/1/11.
*/
public enum Status {
OPEN, CLOSED
}
package org.hongxi.java.util.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.IntStream;
/**
* Created by shenhongxi on 2019/4/10.
*/
public class StreamTest {
public static void main(String[] args) {
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "10");
CopyOnWriteArrayList<Integer> numbers = new CopyOnWriteArrayList<>();
IntStream.range(0, 10000).parallel().forEach(numbers::add);
System.out.println(numbers.size() == 10000);
// 下面的操作会报错 ArrayIndexOutOfBoundsException
List<Integer> list2 = new ArrayList<>();
numbers.parallelStream().forEach(list2::add);
System.out.println(list2.size() == numbers.size());
}
}
package org.hongxi.java.util.stream;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
/**
* Created by shenhongxi on 2019/4/14.
*/
public class StreamTests {
@Test
public void t() {
List<String> words = new ArrayList<>();
words.add("a");
words.add("b");
words.add("c");
words = words.stream().filter(e -> e.equals("d")).collect(Collectors.toList());
assert words.isEmpty();
}
@Test
public void t2() {
List<String> words = new ArrayList<>();
words.add("a");
words.add("b");
words.add("c");
CopyOnWriteArrayList<String> result = new CopyOnWriteArrayList<>();
words.parallelStream().forEach(e -> result.add(e.toUpperCase()));
assert result.size() == words.size();
}
@Test
public void t3() {
List<String> words = new ArrayList<>();
words.add("a");words.add("b");words.add("c");words.add("d");words.add("e");words.add("f");
words.add("g");words.add("h");words.add("i");words.add("j");words.add("k");words.add("l");
words.add("m");words.add("n");words.add("o");words.add("p");words.add("q");words.add("r");
words.add("s");words.add("t");words.add("u");words.add("v");words.add("w");words.add("x");
words.add("y");words.add("z");
Map<String, String> result = words.parallelStream()
.collect(HashMap::new, (m, word) -> m.put(word, word.toUpperCase()), HashMap::putAll);
System.out.println(result);
assert result.size() == words.size();
}
}
package org.hongxi.java.util.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.java.util.stream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static 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(groupingBy(Task::getStatus));
System.out.println(map);
Map<Status, Long> map2 = tasks.stream().collect(groupingBy(Task::getStatus, counting()));
System.out.println(map2);
Map<Boolean, List<Task>> map3 = tasks.stream().collect(partitioningBy(e -> e.getPoint() > 7));
System.out.println(map3);
// Map<String, Product> productMap = products.stream().collect(toMap(Product::getSku, identity(), (key1, key2) -> key2));
Collection<String> result = tasks
.stream()
.mapToInt(Task::getPoint)
.mapToDouble(point -> point * 100 / totalPoints)
.mapToObj(percentage -> (int) percentage + "%")
.collect(toList());
System.out.println(result);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册