更新 java 8 特性
This commit is contained in:
@ -1,5 +1,21 @@
|
||||
package com.raorao;
|
||||
|
||||
import afu.org.checkerframework.checker.igj.qual.I;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.DelayQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
@ -14,6 +30,13 @@ public class Test {
|
||||
//System.out.println(foo());
|
||||
int r = new Test().foo();
|
||||
System.out.println(r);
|
||||
PriorityBlockingQueue<Integer> aa;
|
||||
LinkedBlockingQueue<Integer> ss;
|
||||
ss = new LinkedBlockingQueue<>();
|
||||
ss.add(1);
|
||||
ArrayBlockingQueue<Integer> s;
|
||||
LinkedBlockingQueue<Integer> a;
|
||||
|
||||
}
|
||||
|
||||
public int foo() {
|
||||
|
46
code/src/main/java/com/raorao/java/base/LambdaTest.java
Normal file
46
code/src/main/java/com/raorao/java/base/LambdaTest.java
Normal file
@ -0,0 +1,46 @@
|
||||
package com.raorao.java.base;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Lambda 表达式.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-17:32
|
||||
*/
|
||||
public class LambdaTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
foo1();
|
||||
}
|
||||
|
||||
public static void foo1() {
|
||||
String[] ss = new String[] {"a", "c", "b", "d"};
|
||||
List<String> list = Arrays.asList(ss);
|
||||
list.forEach(e -> System.out.print(e));
|
||||
System.out.println();
|
||||
list.sort((e1, e2) -> e2.compareTo(e1));
|
||||
list.forEach(System.out::print);
|
||||
System.out.println();
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
map.put(i, i * i);
|
||||
}
|
||||
map.forEach((k, v) -> System.out.println("key = " + k + ", value = " + v));
|
||||
|
||||
Thread t = new Thread(()->{
|
||||
System.out.println( "start thread");
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("end thread");
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
}
|
92
code/src/main/java/com/raorao/java/base/StreamTest.java
Normal file
92
code/src/main/java/com/raorao/java/base/StreamTest.java
Normal file
@ -0,0 +1,92 @@
|
||||
package com.raorao.java.base;
|
||||
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* java 8 Stream.
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-06-17:29
|
||||
*/
|
||||
public class StreamTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Stream<String> stream = Stream.of("a", "b", "c");
|
||||
List<String> list = Arrays.asList("hello", "world");
|
||||
List<Integer> intList = Arrays.asList(1, 3, 12, 4, 8, 9, -1);
|
||||
|
||||
// 大写
|
||||
List<String> uppercase = list.stream().map(String::toUpperCase)
|
||||
.collect(Collectors.toList()); // 类实例的方法引用,没有传入参数
|
||||
List<String> uppercase2 = list.stream().map((e) -> e + "a").collect(Collectors.toList());
|
||||
uppercase.forEach(System.out::println);
|
||||
uppercase2.forEach(System.out::println);
|
||||
|
||||
// 平方数
|
||||
List<Integer> square = intList.stream().map((e) -> e * e).collect(Collectors.toList());
|
||||
square.forEach((e) -> System.out.print(e + " "));
|
||||
System.out.println();
|
||||
|
||||
// 一对多
|
||||
Stream<List<Integer>> inputStream = Stream.of(
|
||||
Arrays.asList(1),
|
||||
Arrays.asList(2, 3),
|
||||
Arrays.asList(4, 5, 6)
|
||||
);
|
||||
Stream<Integer> outputStream = inputStream.
|
||||
flatMap((childList) -> childList.stream());
|
||||
outputStream.forEach(e -> System.out.print(e + " "));
|
||||
|
||||
// 过滤器
|
||||
// 1. 选择偶数
|
||||
List<Integer> evenNumber = intList.stream().filter(e -> e % 2 == 0)
|
||||
.collect(Collectors.toList());
|
||||
evenNumber.forEach((e) -> System.out.print(e + " "));
|
||||
System.out.println();
|
||||
|
||||
// 2. find first
|
||||
//intList = new ArrayList<>();
|
||||
Optional<Integer> first = intList.stream().findFirst();
|
||||
System.out.println(first.orElse(0));
|
||||
|
||||
/**
|
||||
* reduce, 组合元素。
|
||||
* 它提供一个起始值(种子),然后依照运算规则(BinaryOperator),
|
||||
* 和前面 Stream 的第一个、第二个、第 n 个元素组合。从这个意义上说,字符串拼接、数值的 sum、min、max、average 都是特殊的 reduce。
|
||||
*/
|
||||
// 1. 字符串拼接
|
||||
Optional<String> ss = stream.reduce((a, b) -> a + b);
|
||||
System.out.println(ss.orElse(null));
|
||||
//String s2 = stream.reduce("", (a, b) -> a + b + "-");
|
||||
//System.out.println(s2);
|
||||
|
||||
// 2. 数值相加
|
||||
Optional<Integer> sum = intList.stream().reduce((a, b) -> a + b);
|
||||
System.out.println(sum.orElse(0));
|
||||
|
||||
// 3. min
|
||||
int min = intList.stream().mapToInt(e->e).min().getAsInt();
|
||||
System.out.println("min: " + min);
|
||||
|
||||
// 自己生成流
|
||||
Stream.iterate(0, n -> n +3).limit(10).forEach((e) -> System.out.print(e + " "));
|
||||
// 生成随机数
|
||||
Random seed = new Random();
|
||||
Supplier<Integer> random = seed::nextInt;
|
||||
Stream.generate(random).limit(10).forEach(System.out::println);
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user