Lambda表达式
表达式的特点
- 可选类型声明:不需要声明参数类型,编译器可以统一识别参数值。
- 可选的参数圆括号:一个参数无需定义圆括号,但多个参数需要定义圆括号。
- 可选的大括号:如果主体包含了一个语句,就不需要使用大括号。
- 可选的返回关键字:如果主体只有一个表达式返回值则编译器会自动返回值,大括号需要指定表达式返回了一个数值
Lambda在集合当中的使用
1.List和forEach、sort
forEach()方法遍历集合,如果要打印元素,它需要的实现 Consumer接口,同时要实现重写accept()方法,它会把数组里的每一个元素都交给,accept()方法。
/**
* @author Summer
* @date 22/08/25
*/
@Slf4j
public class CollectionTest1 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add("Summer");
list.forEach(new Consumer<String>(){
/**
* Performs this operation on the given argument.
*
* @param s the input argument
*/
@Override
public void accept(String s) {
log.info(s);
}
});
System.out.println("==========================");
list.forEach(a-> System.out.println(a));
}
}
输出
sort方法
list.sort(new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
System.out.println(list);
System.out.println("===================");
//再添加一个元素tomorrow
list.add("tomorrow");
//默认是升序
list.sort(((o1, o2) -> o1.compareTo(o2)));
System.out.println(list);
Hashmap的forEach
/**
* @author Summer
* @date 22/08/25
*/
public class HashmapTest {
public static void main(String[] args) {
HashMap<Integer,String> hashMap = new HashMap();
hashMap.put(1,"Spring");
hashMap.put(2,"Summer");
hashMap.put(3,"autumn");
hashMap.put(4,"winter");
//此时并未使用lambda表达式,较为繁琐
hashMap.forEach(new BiConsumer<Integer, String>() {
@Override
public void accept(Integer k, String v) {
System.out.println(k+"-->"+v);
}
});
System.out.println("=========================");
//只需要一句即可
hashMap.forEach((k,v)-> System.out.println(k+"-->"+v));
}
}
list.stream().foreach()和list.foreach()的对比
-
执行顺序
Collection.forEach()使用集合的迭代器(如果指定了一个),集合里元素的处理顺序是明确的。相反,Collection.stream().forEach()的处理顺序是不明确的。 -
自定义的迭代器
list.stream().foreach()会忽略自定义的迭代器,只是在列表上逐个获取元素。list.foreach()会使用自定义的迭代器。 -
修改集合
list.foreach()是快速失败的,遍历的时候操作一旦有问题就会抛异常。而 list.stream().foreach() 则稍后抛异常 -
执行顺序
Collection.forEach()使用集合的迭代器,集合元素的处理顺序是明确的。
但是Collection.stream.forEach()的处理顺序是不明确的。
Collectors.toList()
获取某一列,例如User类要Id
List<Long> idList = users.stream().map(User::getId).collect(Collectors.toList());
将某一列大写输出
List<String> list1 = Arrays.asList("a","b","c");
List<String> result = list1.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("大写=" + result);
Collectors.toMap()
一般用于将List转换为Map
参数 | 作用 |
---|---|
keyMapper | 用于指定Key的Function |
valueMapper | 用于指定value的Function |
mergeFuncation | 用于指定多个相同的key如何进行合并的 Funcation |
一些常用的方法
- 将用户的Id和Name作为key和value,存储为Map
Map<Long, String> map = userList.stream().collect(Collectors.toMap(User::getId, User::getName));
- 当出现相同的key时,取后面的那个
Map<Integer, String> map = userList.stream().collect(Collectors.toMap(User::getAge, User::getName, (a, b) -> b));
Collectors.groupingBy()
分组查询
- 该返回值是Integer,List 通过年龄进行分组
Map<Integer, List<User>> map = userList.stream().collect(Collectors.groupingBy(User::getAge));
stream().filter
//筛选只要字符串是b的
List<String> list2 = list1.stream().filter(a -> a.equals("b")).collect(Collectors.toList());
System.out.println("list2" + list2);
结果