1 package com.zx; 2 3 import java.util.*; 4 import java.util.function.*; 5 import java.util.stream.Collectors; 6 import java.util.stream.Stream; 7 8 /** 9 * lambdaTest类 10 * 11 * @author ning 12 * @create 2018-06-11 17:19 13 **/ 14 public class LambdaTest { 15 16 public static void main(String[] args){ 17 18 new Thread(() -> {System.out.println("lambda创建一个新线程");}).start(); 19 20 List<String> listStr = Arrays.asList("Java","C","C++","C#","Python","PHP"); 21 StringBuilder sb = new StringBuilder(); 22 listStr.forEach(n -> System.out.println(sb.append(",").append(n))); 23 24 /** 25 * 用特殊符号拼接 26 * */ 27 String collect = listStr.stream().collect(Collectors.joining(",")); 28 System.out.println(collect); 29 String collect1 = listStr.stream().collect(Collectors.joining("#")); 30 System.out.println(collect1); 31 32 listStr.sort((a,b) -> a.compareTo(b)); 33 System.out.println(listStr); 34 35 String abc = "hello"; 36 Consumer<String> consumer = (a) -> {}; 37 38 Function<String,String> function = x -> x.toUpperCase(); 39 String zsdf = function.apply("zsdf"); 40 System.out.println(zsdf); 41 42 /** 43 * 静态方法引用 44 * 语法格式- - 类目::staticMethod 45 */ 46 //只有一个输出 47 Supplier<String> supplier = LambdaTest::put; 48 System.out.println(supplier.get()); 49 //只有一个输入 50 Consumer<String> c1 = LambdaTest::consume; 51 c1.accept("张三"); 52 //一个输入,一个输出 53 Function<String, String> f1 = LambdaTest::convertUp; 54 System.out.println(f1.apply("a")); 55 //两个输入,一个输出 56 BiFunction<String,String,Integer> biFunction = LambdaTest::getLength; 57 System.out.println(biFunction.apply("abc","123")); 58 ////////////////////// 方法的引用 - start //////////////////////// 59 /** 60 * 实例方法引用 61 * 语法格式- - 实例::实例Method 62 */ 63 Supplier<String> s1 = new LambdaTest()::put1; 64 System.out.println(s1.get()); 65 LambdaTest lt = new LambdaTest(); 66 Consumer<String> c2 = lt::consume1; 67 c2.accept("王五"); 68 69 /** 70 * 对象方法引用 71 * 定义:抽象方法的第一个参数类型【最好是自定义的类型】刚好是实例方法的类型【言外之意,必需有参数】,抽象方法剩余的参数恰好可以当作实例方法的参数。 72 * 如果函数式接口的实现能用上面说的实例方法调用来实现的话,那么就可以使用对象方法引用 73 * 语法格式- - 类名::实例Method 74 */ 75 Consumer<Too> c3 = (Too too) -> new Too().too(); 76 c3.accept(new Too()); 77 Consumer<Too> c4 = Too::too; 78 c4.accept(new Too()); 79 BiConsumer<Too,String> c5 = (too, ac) -> new Too().too1(ac); 80 c5.accept(new Too(),"宁大人"); 81 //两个输入,一个输出 82 BiFunction<Too, String, Integer> biFunction1 = (p,s) -> new Too().too2(s); 83 BiFunction<Too, String, Integer> biFunction2 = Too::too2; 84 System.out.println(biFunction1.apply(new Too(),"123")); 85 System.out.println(biFunction2.apply(new Too(),"123")); 86 /** 87 * 构造方法的引用 88 * 定义:如果函数式接口的实现恰好可以通过调用一个类的构造方法来实现,那么就可以使用构造方法引用 89 * 语法格式- - 类名::new 90 */ 91 Supplier<Person> s2 = () -> new Person(); 92 s2.get(); 93 Supplier<Person> s3 = Person::new; 94 s3.get(); 95 //含有无参构造函数都可以使用Supplier,如下: 96 Supplier<List> s4 = ArrayList::new; 97 Supplier<Thread> s5 = Thread::new; 98 Supplier<Set> s6 = HashSet::new; 99 Supplier<String> s7 = String::new; 100 101 //含有有参构造函数的 102 Consumer<Integer> c6 = Account::new; 103 c6.accept(123); 104 105 Function<String, Integer> f2 = Integer::new; 106 System.out.println(f2.apply("456")); 107 108 ////////////////////// 方法的引用 - end //////////////////////// 109 110 ////////////////////// Stream API - start //////////////////////// 111 112 /*** 113 * stream的创建 114 * */ 115 //1、数组 116 String[] arr = {"1","b","c"}; 117 Integer[] arr1 = {1,2,3,4}; 118 Stream<String> stream1 = Stream.of(arr); 119 Stream<Integer> stream2 = Stream.of(arr1); 120 //2、集合 121 List<String> list1 = Arrays.asList("1","2","3"); 122 Stream<String> stream = list1.stream(); 123 124 /** 125 * 中间操作 126 * */ 127 Arrays.asList(1, 2, 3, 4, 5, 6).stream().filter(x -> x % 2 == 0).forEach(System.out::println); 128 int max = Arrays.asList(1, 2, 3, 4, 5, 6).stream().max((a1,b1) -> a1-b1).get(); 129 System.out.println(max); 130 Integer min = Arrays.asList(1, 2, 3, 4, 5, 6).stream().min((a1, b1) -> a1 - b1).get(); 131 System.out.println(min); 132 //求集合元素数量 133 long count = Arrays.asList(1, 2, 3, 4, 5).stream().count(); 134 System.out.println(count); 135 //截取 136 Arrays.asList(1, 2, 3, 4, 5, 6).stream().limit(3).forEach(System.out::println); 137 //求平均值 138 double asDouble = Arrays.asList(1, 2, 3, 4, 5, 6).stream().mapToInt(x -> x).average().getAsDouble(); 139 System.out.println(asDouble); 140 //查找任意匹配的元素 141 Optional<Integer> first = Arrays.asList(1, 2, 3, 4, 5, 6).stream().filter(x -> x % 2 == 0).findAny(); 142 System.out.println(first.get()); 143 //查找第一个匹配的元素 144 Optional<Integer> first1 = Arrays.asList(1, 2, 3, 4, 5, 6).stream().filter(x -> x % 2 == 0).sorted((a,b) -> b -a).findFirst(); 145 System.out.println(first1.get()); 146 //从1-50里面的所有偶数找出来,放到一个list中 147 List<Integer> list2 = Stream.iterate(1, x -> x + 1).limit(50).filter(n -> n % 2 == 0).collect(Collectors.toList()); 148 list2.forEach(System.out::println); 149 //集合元素去重 150 Arrays.asList(1,2,3,4,5,4,3,5,6,7,3,8).stream().distinct().forEach(System.out::println); 151 //将流转成set集合 152 Set<Integer> collect2 = Arrays.asList(1, 2, 3, 4, 5, 4, 3, 5, 6, 7, 3, 8).stream().collect(Collectors.toSet()); 153 System.out.println(collect2); 154 //从1-50里面的所有偶数找出来,忽略前10个,放到一个list中 155 List<Integer> list3 = Stream.iterate(1, x -> x + 1).limit(50).filter(n -> n % 2 == 0).skip(10).collect(Collectors.toList()); 156 System.out.println(list3); 157 //类似分页效果, skip(10).limit(10) :跳过前10条,相当于查询第二页,每页10条 158 List<Integer> list4 = Stream.iterate(1, x -> x + 1).limit(50).sorted((a,b) -> b -a).skip(10).limit(10).collect(Collectors.toList()); 159 System.out.println(list4); 160 ////////////////////// Stream API - end //////////////////////// 161 } 162 163 String put1(){ 164 return "world"; 165 } 166 167 static String put(){ 168 return "hello"; 169 } 170 171 void consume1(String string){ 172 System.out.println(string + ", 哈哈哈"); 173 } 174 static void consume(String string){ 175 System.out.println(string + ", 哈哈哈"); 176 } 177 static String convertUp(String a){ 178 return a.toUpperCase(); 179 } 180 static Integer getLength(String a, String b){ 181 return a.length() + b.length(); 182 } 183 } 184 185 class Account{ 186 public Account(int age){ 187 System.out.println(age); 188 } 189 } 190 191 class Person{ 192 public Person(){ 193 System.out.println("person 构造方法调用了"); 194 } 195 } 196 197 class Too{ 198 public void too(){ 199 System.out.println("invoke............."); 200 } 201 202 public void too1(String str){ 203 System.out.println(str + ",invoke............."); 204 } 205 206 public Integer too2(String p) { 207 return 1; 208 } 209 }