java8新特性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * 【接口中 默认方法 有方法体】 * JDK8之前,接口中可以定义 变量、方法: * 变量 必须 为 public static final修饰 * 方法 必须 为 public abstract 修饰 * * JDK8及以后,接口中可以定义 普通方法(有方法体) * default * static * */ interface MyJDK8{ void test1(); default void test2(){ System.out.println( "test2..." ); } static void test3(){ System.out.println( "test3..." ); } } class MyJDK8Impl implements MyJDK8{ @Override public void test1() { System.out.println( "test1..." ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /** * 【lambda表达式】 * why? * 期望 使用 接口的方法: * a,写一个class 实现接口; * b,使用匿名内部类; * 好处: * 简化 匿名内部类的调用; * * How? * 使用 lambda表达式 依赖于 函数式接口; * * 【函数式接口】 * what? * 接口 只有 一个 抽象方法,可以 认为该接口是 函数式接口,可以用 @FunctionalInterface 标识 * 规范: * 函数式接口中 允许 重新 Object方法; * 函数式接口中 允许 default、static方法; * * 语法: * () :参数列表 * -> :分隔符 * {} :方法体 * * 使用: * a, 无参 * b, 有参 * */ interface MyLambda{ void test(); } void testInterfaceMethod(){ // 使用匿名内部类 new MyLambda(){ @Override public void test() { System.out.println( "test..." ); } }.test(); // 使用lambda ((MyLambda) () -> System.out.println( "test..." )).test(); } @FunctionalInterface interface MyFuncInterface{ // 唯一 抽象方法 void test(); default void test1(){ System.out.println( "test1..." ); } static void test2(){ System.out.println( "test2..." ); } // Object 方法 String toString(); } void testMyFuncInterface(){ MyFuncInterface m = () -> System.out.println( "aa" ); m.test(); // 简化 ((MyFuncInterface) () -> System.out.println( "aa" )).test(); } @FunctionalInterface interface MyFuncInterface1{ String test(String a, String b); } void testMyFuncInterface1(){ MyFuncInterface1 m = (a, b) -> { return a + ":" + b; }; m.test( "a" , "b" ); // 简化 MyFuncInterface1 m1 = (a, b) -> a + ":" + b; ((MyFuncInterface1) (a, b) -> a + ":" + b).test( "aaaa" , "bbbbb" ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | /** * 【Stream】 * 基于 pipeline 设计: * 数据源 => pipeline(过滤 | 排序...执行器) => 结果数据 * (每个 执行器 都会生成一个新的pipeline) * (生成新的pipeline后,老的将不会存在) * * 特性: * a, stream不存储数据,变量都在局部变量; * b, 数据源不会变化; * c, stream不可以重复使用; * * stream的3种操作: * a,生成初始stream * b,中间操作 * 返回一个stream * 可以多个 * c,终值操作 * 仅有一个 * * stream的执行顺序: * 每条数据 按pipeline的流程 依次执行; * eg:pipeline: a-b * 第一条数据 -a-b- * 第二条数据 -a-b- * * JDK自带的函数式接口: * * 方法引用: * 结合 lambda表达式, 让代码 更加精简; * * 种类: * static方法引用: 类名::static方法 * 对象方法引用: 类名::实例方法 * 实例方法引用: new 对象::实例方法 * 构造函数引用: 类名::new * */ /** * 【stream的3种操作】 */ /** * 初始stream */ void initStream(){ Stream<String> stringStream = Stream.of( "a" , "b" ); IntStream intStream = Arrays.stream( new int []{ 1 , 3 }); } /** * 中间操作 */ void middleStream(){ Arrays.asList( "a" , "b" ).stream() .filter(a->{ System.out.println( "aa" ); return a.equals( "aa" ); } ); } /** * 终值操作 */ void finalStream(){ Arrays.asList( "a" , "b" ).stream().filter(a->{ System.out.println( "aa" ); return a.equals( "aa" ); }).count(); } class Person{ Person(){ } Person(String name, Integer age){ this .name = name; this .age = age; } String name; Integer age; public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\ '' + ", age=" + age + '}' ; } } void testStream(){ Person person = new Person( "1" , 1 ); Person person1 = new Person( "2" , 2 ); // 【转Set】 Set<Person> set = Arrays.asList(person, person1) .stream() .collect(Collectors.toSet()); // 【转map】 Map<String, Integer> map = Arrays.asList(person, person1) .stream() .collect(Collectors.toMap( new Function<Person, String>() { @Override public String apply(Person person) { return person.getName(); } }, new Function<Person, Integer>() { @Override public Integer apply(Person person) { return person.getAge(); } } )); // 简化 Map<String, Integer> map1 = Arrays.asList(person, person1) .stream() .collect(Collectors.toMap(p -> p.getName(), p -> p.getAge())); System.out.println(map1); // 【求和】 Person countPerson = Arrays.asList(person, person1) .stream() .reduce( new BinaryOperator<Person>() { @Override public Person apply(Person person, Person person2) { Person p = new Person(); p.setAge(person.getAge() + person2.getAge()); return p; } }) .get(); // 简化 Person countPerson1 = Arrays.asList(person, person1) .stream() .reduce((p1, p2) -> { Person p = new Person(); p.setAge(p1.getAge() + p2.getAge()); return p; }) .get(); System.out.println(countPerson1.getAge()); // 【max || min】 Person maxPerson = Arrays.asList(person, person1) .stream() .max( new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o1.getAge() - o2.getAge(); } }) .get(); // 简化 Person maxPerson1 = Arrays.asList(person, person1) .stream() .max((p1, p2) -> p1.getAge() - p2.getAge()) .get(); System.out.println(maxPerson1.getAge()); // 【match】 boolean match = Arrays.asList(person, person1) .stream() .anyMatch( new Predicate<Person>() { @Override public boolean test(Person person) { return person.getName().equals( "jack" ); } }); // 简化 boolean match1 = Arrays.asList(person, person1) .stream() .anyMatch(p -> p.getName().equals( "jack" )); System.out.println(match1); // 【Filter】 Set<Person> filter1 = Arrays.asList(person, person1) .stream() .filter( new Predicate<Person>() { @Override public boolean test(Person person) { return person.getName().equals( "jack" ); } }) .collect(Collectors.toSet()); // 简化 Set<Person> filter2 = Arrays.asList(person, person1) .stream() .filter(p -> p.getName().equals( "jack" )) .collect(Collectors.toSet()); System.out.println(filter2); // 【分页】 Set<Person> limit1 = Arrays.asList(person, person1) .stream() .skip( 0 ) .limit( 3 ) .collect(Collectors.toSet()); System.out.println(limit1); // 【排序】 Set<Person> sort = Arrays.asList(person, person1) .stream() .sorted( new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o2.getAge() - o1.getAge(); } }) .collect(Collectors.toSet()); // 简化 Set<Person> sort1 = Arrays.asList(person, person1) .stream() .sorted((p1, p2) -> p2.getAge() - p1.getAge()) .collect(Collectors.toSet()); System.out.println(sort1); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-11-22 Redis---拓展