java8Lambda表达式如何使用
lamda表达式是java8新特性,优点简化代码,降低代码重复率。
语法格式:
由三部分组成:1.形参列表。2.箭头(->)。代码块。
使用:
1.集合元素过滤;
//集合元素过滤
List list =new ArrayList();
list.add("apple");
list.add("arange");
list.add("peach");
List newList = list.stream().filter(o1 ->o1.equalsIgnoreCase("peach")).collect(Collectors.toList());

2.编写线程
Runnable runnable = ()->{
//TODO something
};
new Thread(runnable).start();
}
3.数组计数

4.数组去重
String name[] = {"张三", "李四", "王五", "王五"};
List newName = Arrays.stream(name).filter(o1 -> o1.equals("王五")).collect(Collectors.toList());
5.:;函数 使用
List stu=new ArrayList();
stu.add(new Student(24,"李四"));
stu.add(new Student(25,"张三"));
stu.add(new Student(23,"王五"));
stu.stream().map(Student :: getName).collect(Collectors.joining("," , "[" , "]"));
6,map和flatmap的区别
List listString = Arrays.asList("hello welcome", "world hello", "hello world", "hello world welcome");
//map会将一个元素变成一个新的Stream
List<Stream> list3 = list2.stream().map(o1 -> Arrays.stream(o1.split(""))).distinct().collect(Collectors.toList());
//flatmap会将结果打平,得到一个单个元素
List<String> listFlatMap= list2.stream().flatMap(o1 -> Arrays.stream(o1.split(" "))).distinct().collect(Collectors.toList());

每天进步一点点
网友评论