Lambda
函数接口:
有且只有一个抽象方法的接口,用作Lambda表达式的类型
特点:
匿名内部类需要显示声明对象的调用,增加代码复杂度,阅读性差,后期维护成本高
lambda 简化声明对象的过程,只需要开发人员传入想要的行为,简化了对象的传入,提升代码的可读性,后期维护成本低
@Test
public void testComparator(){
/**
* 匿名内部类实现
* */
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length()-o2.length();
}
};
int compare = comparator.compare("abc", "abcde");
System.out.println(compare);
/**
* Lambda
* */
comparator = ((o1, o2) -> o2.length()-o1.length());
compare = comparator.compare("abc", "abcde");
System.out.println(compare);
}
Lambda表达式的多种形态:
-
Runnable noArguments = ()-> System.out.println(“Hello world”);
不需要参数,并且只有一个run方法,返回值类型为void; -
Actionlistener oneArgument = event -> System.out.println(“button clicked”);
需要参数,且只有一个actionPerformed方法,返回值类型为void -
Runnable multiStatement = () - > {
System.out.print(“Hello”);
System.out.print(“World”);
};
如果需要编写的代码超过一行,可使用{},如果只有一行可以省略{},该代码块和普通方法遵循的规则别无 致,可以用返
回或抛出异常来退出 -
BinaryOperator add = (x,y)-> x + y;
创建一个函数,用来计算参数x和y的结果,使用apply方法可执行该函数 -
BinaryOperator addExplicit = (Long x,Long y)-> x + y;
创建一个函数,用来计算参数x和y的结果,使用apply方法可执行该函数,参数类型可显示
声明,可以通过返回值类型进行类型推断得出
流:
惰性求值:
stream中,未使用带有终止操作的流,在流中的任何操作都不会生成结果,也不会有任何输出
String listString = "China,Landon,Landon,Usa";
ArrayList<String> list = Lists.newArrayList(listString);
list.stream().filter(lists->{
System.out.println(lists);
return lists.contains("Landon");
});
及早求值:
在stream中使用带有终止操作的流,则计算出对应的结果,并输出
String listString = "China,Landon,Landon,Usa";
list.stream().filter(lists->{
System.out.println(lists);
return lists.contains("Landon");
}).count();
常用流操作:
Collector(toList()):
通过stream流生成list集合
List<String> collect = Stream.of("a", "b", "c").collect(Collectors.toList());
System.out.println(collect);
map:
将一种数据类型,转换成,另一种类型
List<String> collect = Stream.of("a", "b", "c").
map(str -> str.toUpperCase()).
collect(Collectors.toList());
System.out.println(collect);
filter:
根据条件判断筛选出需要的数据
List<String> collect = Stream.of("a", "b", "c").
filter(str -> str.contains("a")).
collect(Collectors.toList());
System.out.println(collect);
flatMap:
将数据中获取到的元素进行展平,并存储到新的stream中,可通过多次使用flatMap,获取到最底层的数据
//获取所有的城市名称
List<String> collect = userList.stream().
flatMap(user -> user.getAddressList().stream()).
flatMap(address -> address.getCityList().stream()).
collect(Collectors.toList());
System.out.println(collect);tream()).collect(Collectors.toList());
System.out.println(together);
Min:
获取stream中长度最小的数据,参数使用Comparator类型函数
ArrayList<String> list = Lists.newArrayList("Hello", "Word", "Shopastro");
String str = list.stream().min(Comparator.comparing(strs -> strs.length())).get();
System.out.println(str);`
Max:
获取stream中长度最大的数据,参数使用Comparator类型函数
ArrayList<String> list = Lists.newArrayList("Hello", "Word", "Shopastro");
String str = list.stream().max(Comparator.comparing(strs -> strs.length())).get();
System.out.println(str);
Reduce:
对stream数据进行求和
Integer reduce = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
System.out.println(reduce);
收集器:
toCollection:
转换成其他集合
TreeSet<String> collect = Stream.of("a", "b", "c").collect(Collectors.toCollection(TreeSet::new));
System.out.println(collect);
maxBy:
获取最大的数值
minBy:
获取最小的数值
List<String> strlist = Lists.newArrayList("12","34","5","678","9","999");
Optional<String> collect = strlist.stream().collect(Collectors.maxBy((a, b) -> Integer.parseInt(a) - Integer.parseInt(b)));
System.out.println(collect.get());
partitioningBy:
数据分块:
ArrayList<Integer> lists = Lists.newArrayList(1000, 2000, 1500, 1500, 2000);
Map<Boolean, List<Integer>> collect = lists.stream().collect(Collectors.partitioningBy(list -> list > 1500));
System.out.println(collect.get(Boolean.TRUE));
System.out.println(collect.get(Boolean.FALSE));
groupingBy:
数据分组:
ArrayList<Integer> lists = Lists.newArrayList(1000, 2000, 1500, 1500, 2000);
Map<Boolean, List<Integer>> collect = lists.stream().collect(Collectors.groupingBy(list -> list > 1500));
System.out.println(collect.get(Boolean.TRUE));
System.out.println(collect.get(Boolean.FALSE));
joining:
字符串:
ArrayList<String> lists = Lists.newArrayList("1000", "2000", "1500", "1500", "2000");
String str = lists.stream().collect(Collectors.joining(",", "[", "]"));
System.out.println(str);
peek:
查看流中的每一个值:
//获取所有的城市名称
List<String> collect = userList.stream().
flatMap(user -> user.getAddressList().stream()).
flatMap(address -> address.getCityList().stream()).
peek(city-> System.out.println("City :"+city)).
collect(Collectors.toList());