赞
踩
jdk8 提供了很多实用的新特性,针对接口推出了接口默认方法,接口静态方法以及函数式接口,同时为了简化代码编写,推出了lambda表达式,为了增强对数据的操作,还定义了Stream操作等。
多继承和默认方法:
interface A{}
interface B{}
public interface Inte1 extends A,B {
String add();
default int test(int a,int b){
return a*b;
}
}
public class Test1 {
public static void main(String[] args) throws InterruptedException {
new Thread(Test1::test).start();
new Thread(()->test()).start();
Thread.sleep(5000);
}
private static void test(){
System.out.println("哈哈哈哈");
}
}
如果创建线程只执行一个方法的话,可以直接使用new Thread(Test1::test).start();其中test为静态方法,执行多行逻辑的话,可以()->{}形式。
Function 输入输出类型函数:
Function<String, String> f = (s) -> {
System.out.println(s);
return s+": abc";
};
String s = f.apply("s");
Consumer 消费类型函数,通常只有输入没有输出
Consumer<String> c = s -> {
System.out.println(s);
};
c.accept("s");
Supplier 提供类型函数,通常只有输出,没有输入。
Supplier<String> s = ()->{
return "s";
};
String r = s.get();
BiFunction 可以指定两个输入类型
BiFunction<String, String, String> f = (s1, s2) -> {
System.out.println(s1 + " " + s2);
return s1 + ": " + s2;
};
String s = f.apply("s1", "s2");
BinaryOperator 继承自 BiFunction,三个参数类型都是一致:
BinaryOperator<String> f = (s1, s2) -> {
System.out.println(s1 + " " + s2);
return s1 + ": " + s2;
};
String s = f.apply("s1", "s2");
UnaryOperator 继承自Function,两个输入输出类型一致:
UnaryOperator<String> u = (s) -> {
System.out.println(s);
return s+": abc";
};
String s = u.apply("s");
固定类型接口IntUnaryOperator 、DoubleUnaryOperator、LongUnaryOperator
IntUnaryOperator i = (s) -> {
return s;
};
int a = i.applyAsInt(1);
DoubleUnaryOperator d = (s) -> {
return s;
};
double b = d.applyAsDouble(1d);
LongUnaryOperator l = (s) -> {
return s;
};
long c = l.applyAsLong(1L);
Predicate 断言型函数
Predicate<String> p = (s) -> {
System.out.println(s);
return Objects.equals(s, "s");
};
boolean b = p.test("s");
无参构造函数创建对象
Supplier<Dog> supplier = Dog::new;
System.out.println(supplier.get().test4());
Function<Integer,String> function3 = ((Supplier<Dog>)(Dog::new)).get()::test2;
System.out.println(function3.apply(6));
有参构造函数
Function<String,Dog> function4 = Dog::new;
Dog apply1 = function4.apply("44");
List创建流
List<String> list = new ArrayList<>();
list.stream();
list.parallelStream();
通过Arrays创建流
int[] i = new int[]{1,2};
Arrays.stream(i);
指定类型流
IntStream.of(i);
IntStream.rangeClosed(1,10);
随机流
new Random().ints().limit(10);
Stream.generate(()-> "");

map 中间操作
String str = "my name is hellword";
Stream.of(str.split(" ")).map(s -> s.length()).forEach(System.out :: println);
Stream.of(str.split(" ")).map(s -> s.length()).forEachOrdered(System.out :: println);
List<Dog> list = new ArrayList<>();
list.add(new Dog("aa", "0"));
list.add(new Dog("bb", "1"));
list.add(new Dog("cc", "0"));
list.add(new Dog("dd", "1"));
list.forEach(System.out::println);
list.stream().map(dog -> {
dog.age = "0".equals(dog.age) ? "是" : "否";
return dog;
}).forEach(System.out::println);
flatMap A里面存的所有B 的集合
Stream.of(str.split(" ")).flatMap(s -> s.chars().boxed()).forEachOrdered(s->{
System.out.println((char)s.intValue());
});
Stream.of(str.split(" ")).flatMap(s -> s.chars().boxed()).forEach(s->{
System.out.println((char)s.intValue());
});
List<List<String>> lists = new ArrayList<>();
List<String> list1 = new ArrayList<>();
list1.add("aa");
list1.add("bb");
list1.add("cc");
List<String> list2 = new ArrayList<>();
list2.add("aa");
list2.add("bb");
list2.add("cc");
lists.add(list1);
lists.add(list2);
lists.stream().flatMap(l->l.stream()).forEach(System.out::println);
filter 过滤
list.stream().map(dog -> {
dog.age = "0".equals(dog.age) ? "是" : "否";
return dog;
}).filter(dog -> "是".equals(dog.age)).forEach(System.out::println);
list.stream().map(dog -> {
dog.age = "0".equals(dog.age) ? "是" : "否";
return dog;
}).filter(dog -> {
if ("是".equals(dog.age)){
return true;
}
return false;
}).forEach(System.out::println);
peek 处理中间内容
Stream.of(str.split(" ")).peek(System.out::println).map(s -> s.length()).forEachOrdered(System.out :: println);
Random
过滤条件大于100小于1000的10个数据。
new Random().ints().filter(i-> i>100&&i<1000).limit(10).forEach(System.out::println);

并行流下的forEach 和 forEachOrdered
String str = "my is hello word";
//并行流
Stream.of(str.split(" ")).parallel().forEach(System.out::println);
Stream.of(str.split(" ")).parallel().forEachOrdered(System.out::println);
collect 收集器
收集到List
List<String> list = Stream.of(str.split(" ")).parallel().collect(Collectors.toList());
System.out.println(list);
//收集到TreeSet
TreeSet treeSet = Stream.of(str.split(" ")).parallel().collect(Collectors.toCollection(TreeSet::new));
System.out.println(treeSet.toString());
拼接
String s = Stream.of(str.split(" ")).parallel().collect(Collectors.joining("|"));
System.out.println(s);
计算平均
int i = Stream.of(str.split(" ")).map(s -> s.length()).parallel().collect(Collectors.averagingInt(Integer::shortValue)).intValue();
System.out.println(i);
double d = Stream.of(str.split(" ")).map(s -> (double)s.length()).parallel().collect(Collectors.averagingDouble(Double::doubleValue));
System.out.println(d);
double d2 = Stream.of(str.split(" ")).map(s -> (double)s.length()).parallel().collect(Collectors.averagingDouble(s->s.doubleValue()));
System.out.println(d2);
统计汇总
IntSummaryStatistics summaryStatistics = Stream.of(str.split(" ")).map(s -> s.length()).parallel().collect(Collectors.summarizingInt(Integer::intValue));
System.out.println(summaryStatistics.toString());

会得到 数量、总和、最小值、最大值、平均数。
分块
Map<Boolean,List<String>> map = Stream.of(str.split(" ")).collect(Collectors.partitioningBy(s->s.length() == 2));
System.out.println(map.toString());
分组
Map<Integer,List<String>> map = Stream.of(str.split(" ")).collect(Collectors.groupingBy(s-> s.length()));
System.out.println(map.toString());
//分组,第二个参数可对原先的List再操作
Map<Integer,String> map = Stream.of(str.split(" ")).collect(Collectors.groupingBy(s-> s.length(),Collectors.joining(",")));
System.out.println(map.toString());
Map<Integer,Long> map1 = Stream.of(str.split(" ")).collect(Collectors.groupingBy(s->s.length(),Collectors.counting()));
System.out.println(map1.toString());
分组后,提取第一个数据
Map<String, Rule> ruleMap = list.stream().
.collect(Collectors.groupingBy(User::getName,
Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));
reduce 收集(拼接)
String s = Stream.of(str.split(" ")).parallel().reduce((s1,s2)->s1+","+s2).get();
System.out.println(s);
带初始化的reduce
String str1 = Stream.of(str.split(" ")).parallel().reduce("",(s1, s2) -> s1+"|"+s2);
System.out.println(str1);
Integer integer = Stream.of(str.split(" ")).map(s -> s.length()).reduce(0,(s1, s2) -> s1+s2);
System.out.println(integer);
String str1 = Stream.of(str.split(" ")).parallel().reduce((s1, s2) -> s1+","+s2).orElse("");
System.out.println(str1);
int[] i = {1,9,6,4,3,5};
//计算平均数
double d = IntStream.of(i).average().getAsDouble();
System.out.println(d);
//计算最小值
int m = IntStream.of(i).min().getAsInt();
System.out.println(m);
//并行计算最小值
int m1 = IntStream.of(i).parallel().min().getAsInt();
System.out.println(m1);
//计算平均值
double d1 = DoubleStream.of(0.1,0.1).average().getAsDouble();
System.out.println(d1);
//含有中间操作的计算
double d2 = IntStream.of(i).parallel().map((x)-> x*2).average().getAsDouble();
System.out.println(d2);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。