lambda表达式公司用的还是比较多的,今天特意写一篇博客记录下来,公司经常使用的该lambda表达式的常用表达方式,现在想想,其实这种方式还是比较好的,最起码挺高大上的。
package com.zyq;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 练习使用lamda表达式
*/
public class LamdaTest {
public static void main(String[] args) {
User user1 = new User(1, "a1");
User user2 = new User(2, "a2");
User user3 = new User(3, "a3");
User user4 = new User(4, "a4");
User user5 = new User(5, "a5");
List<User> usersList = new ArrayList<>();
usersList.add(user1);
usersList.add(user2);
usersList.add(user3);
usersList.add(user4);
usersList.add(user5);
//stream:转换成流 filter:过滤 map:要显示的内容
List<String> collect = usersList.stream().filter(user -> user.getId() >= 2).map(user -> user.getName()).collect(Collectors.toList());
//这种写法也是可以的 将要返回的值使用大括号进行包裹,并用return进行返回,这在实际项目中使用的是比较多的。
// List<String> collect = usersList.stream().filter(user -> user.getId() >= 2).map(user -> {return user.getName();}).collect(Collectors.toList());
System.out.println(collect);
}
}
运行结果:
[a2, a3, a4, a5]
package com.zyq; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; /** * 练习使用lamda表达式 */ public class LamdaTest { public static void main(String[] args) { User user1 = new User(1, "a1"); User user2 = new User(2, "a2"); User user3 = new User(3, "a3"); User user4 = new User(4, "a4"); User user5 = new User(5, "a5"); List<User> usersList = new ArrayList<>(); usersList.add(user1); usersList.add(user2); usersList.add(user3); usersList.add(user4); usersList.add(user5); //将list集合转换成map集合,其中,userId为map的key,username为map的value,如果两个键相同,值取第一个 Map<Integer, User> map = usersList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (a, b) -> a)); for (Map.Entry<Integer, User> stringEntry : map.entrySet()) { System.out.println(stringEntry.getKey()+"--"+stringEntry.getValue()); } } }
运行结果:
1--User{id=1, name='a1'}
2--User{id=2, name='a2'}
3--User{id=3, name='a3'}
4--User{id=4, name='a4'}
5--User{id=5, name='a5'}
package com.zyq; import com.treemappractice.Student; import java.text.Normalizer; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; /** * 练习使用lamda表达式 */ public class LamdaTest { public static void main(String[] args) { User user1 = new User(1, "a1"); User user2 = new User(2, "a2"); User user3 = new User(3, "a3"); User user4 = new User(4, "a1"); User user5 = new User(5, "a2"); User user6 = new User(6, "a3"); List<User> usersList = new ArrayList<>(); usersList.add(user1); usersList.add(user2); usersList.add(user3); usersList.add(user4); usersList.add(user5); usersList.add(user6); //将list转化成map集合,并按照姓名进行分组,查找姓名重复的 Map<String, List<User>> userMap = usersList.stream().collect(Collectors.groupingBy(user -> user.getName())); for (String key : userMap.keySet()) { System.out.println("key:"+"--"+key+",value:"+userMap.get(key)); } } }
运行结果:
key:--a1,value:[User{id=1, name='a1'}, User{id=4, name='a1'}]
key:--a2,value:[User{id=2, name='a2'}, User{id=5, name='a2'}]
key:--a3,value:[User{id=3, name='a3'}, User{id=6, name='a3'}]