1、Lamda简单介绍
Lambda表达式是JAVA8中提供的一种新的特性,它支持Java也能进行简单的“函数式编程”。
它是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。
要理解函数式编程的产生目的,就要先理解匿名内部类。
先来简单看下匿名内部类调用方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| interface Animal { void run(); } public class Main { public static void see(Animal animal){ animal.run(); } public static void main(String[] args) { see(new Animal() { @Override public void run() { System.out.println("动物在跑..."); } }); } }
|
Lamda表达式写法:
1 2 3 4 5 6 7 8 9 10 11 12 13
| interface Animal { void run(); } public class Main { public static void see(Animal animal){ animal.run(); } public static void main(String[] args) { see(()->System.out.println("动物在跑...")); } }
|
是不是挺简洁,这就是Lamda表达式语言,为了解决匿名内部类繁杂的操作而出现的。
Lamda语法有三种形式:
(参数) -> 单行语句;
(参数) -> { 多行语句 };
(参数) -> 表达式;
括号()可以大致理解为就是方法,里面是参数变量。
2、常见用法
2.1、线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class ThreadMain { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { System.out.println("Hello Lamda!"); } }).start(); new Thread(()->{ System.out.println("Hello Lamda!"); }).start(); } }
|
2.2、过滤及遍历
创建测试类
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
| public class UserVO { private Integer id; private String name; private Integer balance; public UserVO() { } public UserVO(Integer id, String name, Integer balance) { this.id = id; this.name = name; this.balance = balance; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getBalance() { return balance; } public void setBalance(Integer balance) { this.balance = balance; } @Override public String toString() { return "UserVO{" + "id=" + id + ", name='" + name + '\'' + ", balance=" + balance + '}'; } }
|
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
| public class FilterForEachTest{ public static void main(String[] args) { List<UserVO> userVO = new ArrayList<>(); UserVO user1 = new UserVO(1, "Jason", 200); UserVO user2 = new UserVO(2, "Lucy", 200); UserVO user3 = new UserVO(3, "Lily", 300); UserVO user4 = new UserVO(4, "Lily", 400); userVO.add(user1); userVO.add(user2); userVO.add(user3); userVO.add(user4); boolean b = userVO.stream().anyMatch((u) -> u.getBalance() > 200); System.out.println(b); boolean c = userVO.stream().allMatch((u) -> u.getBalance() > 200); System.out.println(c); userVO.stream().filter(u -> u.getId() > 1) .forEach(u -> System.out.println(u.getId() + "->" + u.getName())); } }
|
2.3、提取为Map对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class MapTest { public static void main(String[] args) { List<UserVO> userVO = new ArrayList<>(); UserVO user1 = new UserVO(1, "Jason", 200); UserVO user2 = new UserVO(2, "Lucy", 200); UserVO user3 = new UserVO(3, "Lily", 300); UserVO user4 = new UserVO(4, "Lily", 400); userVO.add(user1); userVO.add(user2); userVO.add(user3); userVO.add(user4); Map<Integer, Integer> map = userVO.stream(). collect(Collectors.toMap(UserVO::getId, UserVO::getBalance, (o, n) -> o)); for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + "->" + entry.getValue()); } } }
|
2.4、对对象里面某个属性提取List对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class ListTest { public static void main(String[] args) { List<UserVO> userList = new ArrayList<>(); UserVO user1 = new UserVO(1, "Jason", 200); UserVO user2 = new UserVO(2, "Lucy", 200); UserVO user3 = new UserVO(3, "Lily", 300); UserVO user4 = new UserVO(4, "Lily", 400); userList .add(user1); userList .add(user2); userList .add(user3); userList .add(user4); List<String> list = userList .stream().map(UserVO::getName).collect(Collectors.toList()); list.stream().forEach(s -> System.out.println(s)); } }
|
2.5、对对象里某个字段求和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class SumTest { public static void main(String[] args) { List<UserVO> userVO = new ArrayList<>(); UserVO user1 = new UserVO(1, "Jason", 200); UserVO user2 = new UserVO(2, "Lucy", 200); UserVO user3 = new UserVO(3, "Lily", 300); UserVO user4 = new UserVO(4, "Lily", 400); userVO.add(user1); userVO.add(user2); userVO.add(user3); userVO.add(user4); int sum = userVO.stream().mapToInt(obj -> obj.getBalance()).sum(); System.out.println(sum); } }
|
2.6、count计数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class CountTest { public static void main(String[] args) { List<UserVO> userList = new ArrayList<>(); UserVO user1 = new UserVO(1, "Jason", 200); UserVO user2 = new UserVO(2, "Lucy", 200); UserVO user3 = new UserVO(3, "Lily", 300); UserVO user4 = new UserVO(4, "Lily", 400); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); Map<String, Long> counted = userList.stream() .collect(Collectors.groupingBy(u->u.getName(), Collectors.counting())); System.out.println(counted); } }
|
2.7、分组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class GroupingTest { public static void main(String[] args) { List<UserVO> userList = new ArrayList<>(); UserVO user1 = new UserVO(1, "Jason", 200); UserVO user2 = new UserVO(2, "Lucy", 200); UserVO user3 = new UserVO(3, "Lily", 300); UserVO user4 = new UserVO(4, "Lily", 400); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); Map<String, List<UserVO>> map = userList.stream() .collect(Collectors.groupingBy(UserVO::getName)); for (Map.Entry<String, List<UserVO>> entry : map.entrySet()) { System.out.println(entry.getKey() + "->" + entry.getValue()); } } }
|
以上列出了Lamda表达式的常见用法,Lamda表达式中还有很多其他用法,大家有兴趣可以深入了解下。