Lambda常用方法
记录学习Lambda表达式常用方法,后续接着更新!!!
Lambda常用方法
- forEach(循环)
- filter(过滤)
- map(转换)
- peek(*)
- reduce(加减乘除)
- sorted(排序)
- limit(保留前n)
- skip(抛弃前n)
- distinct(去重)
- collect(导出结果)
- count(统计数量)
- max(最大值)
- min(最小值)
forEach(循环)
@Test
public void forEach() {
/**
* forEach
* 对stream流中的元素逐个遍历
*/
List<String> userNameList = Arrays.asList("海绵宝宝", "派大星", "章鱼哥");
userNameList.forEach(System.out::println);
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("姓名", "蟹老板");
hashMap.put("职位", "蟹堡王老板");
hashMap.put("爱好", "money");
hashMap.forEach((key, value) -> System.out.println(key + ":" + value));
}
filter(过滤)
@Test
public void filter() {
/**
* filter
* filter会按照指定的条件匹配出符合要求的元素,并返回一个新的stream流
*/
List<String> filterList = Arrays.asList("炎龙铠甲", "地虎铠甲", "雪獒铠甲", "黑犀铠甲", "风鹰铠甲", "封魔斩", "裂地劈", "震雷削", "狂瀑扎", "穿风刺");
List<String> listFilter = filterList.stream().filter(name -> name.contains("铠甲")).collect(Collectors.toList());
listFilter.forEach(System.out::println);
}
map(转换)
@Test
public void map() {
/**
* map
* map方法可以实现元素转换,比如一个元素所有字母转化为大写,又或者 把获取一个元素对象的某个属性
*/
// 转化为大写
List<String> userNameMapList = Arrays.asList("di jia", "sai luo");
List<String> upperCaselist = userNameMapList.stream().map(String::toUpperCase).collect(Collectors.toList());
upperCaselist.forEach(System.out::println);
// 获取一个元素对象的某个属性
List<User> userList = new ArrayList<>();
userList.add(new User().setId(1).setAge("19").setName("张三"));
userList.add(new User().setId(2).setAge("18").setName("李四"));
userList.add(new User().setId(3).setAge("20").setName("王五"));
List<String> listInUserToString = userList.stream().map(User::getName).collect(Collectors.toList());
listInUserToString.forEach(System.out::println);
}
peek(*)
@Test
public void peek() { // 一般peek用于打印,map用于转换
/**
* peek
* peek会对流中的元素逐个遍历处理,它与map的区别在于:map一般用于对象的转换,peek用于对象的消费,即不改变元素本身的类型。
*/
List<String> userNameMapList = Arrays.asList("di jia", "sai luo");
System.out.println("原始集合:" + userNameMapList);
List<String> upperCaselist = userNameMapList.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("原始集合经历map后:" + userNameMapList);
upperCaselist.forEach(a -> System.out.println("map后:" + a));
List<String> userNameMapList2 = Arrays.asList("di jia", "sai luo");
System.out.println("原始集合2:" + userNameMapList2);
List<String> upperCaselist2 = userNameMapList2.stream().peek(String::toUpperCase).collect(Collectors.toList());
System.out.println("原始集合经历peek后:" + userNameMapList2);
upperCaselist2.forEach(a -> System.out.println("peek后:" + a));
List<String> userNameMapList3 = Arrays.asList("di jia", "sai luo");
List<String> upperCaselist3 = userNameMapList3.stream()
.peek(name -> System.out.println("peek打印:" + name))
.map(name -> name.toUpperCase())
.collect(Collectors.toList());
System.out.println("原始集合3:" + userNameMapList3);
upperCaselist3.forEach(a -> System.out.println("peek + map 后:" + a));
}
reduce(加减乘除)
@Test
public void reduce() {
/**
* reduce
* reduce()方法可以执行一些二元操作,例如求和、求积、求最大值等等
*/
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
int sum = numbers.stream().reduce(0, Integer::sum);
int sum2 = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println(sum);
}
sorted(排序)
@Test
public void sorted() {
/**
* sorted
* sorted()方法可以对Stream中的元素进行排序,如果要倒序可以加上 reversed()
*/
List<Integer> numbers = Arrays.asList(1, 3, 4, 5, 2, 0);
numbers = numbers.stream().sorted(Comparator.comparing((Integer number) -> number).reversed()).collect(Collectors.toList());
System.out.println(numbers);
}
limit(保留前n)
@Test
public void limit() {
/**
* limit
* 就相当于sql中的limit,可以指定保留前N的元素(可以使用 limit + skip 做分页处理)
*/
List<String> dbList = Arrays.asList("Oracle", "MySQL", "PostgreSQL", "SQL Server", "Redis", "MongoDB", "Elasticsearch");
List<String> limitList = dbList.stream()
.limit(3)
.collect(Collectors.toList());
limitList.forEach(System.out::println);
}
skip(抛弃前n)
@Test
public void skip() {
/**
* skip
* 作用与limit相反,会抛弃前N的元素(可以使用 limit + skip 做分页处理)
*/
List<String> dbList = Arrays.asList("Oracle", "MySQL", "PostgreSQL", "SQL Server", "Redis", "MongoDB", "Elasticsearch");
List<String> limitList = dbList.stream()
.skip(3)
.collect(Collectors.toList());
limitList.forEach(System.out::println);
}
distinct(去重)
@Test
public void distinct() {
/**
* distinct
* 用于对流中的元素去重
*/
List<String> list = Arrays.asList("金钟罩", "铁布衫", "寒冰掌", "天马流星拳", "天马流星拳");
List<String> limitList = list.stream()
.distinct()
.collect(Collectors.toList());
limitList.forEach(System.out::println);
}
collect(导出结果)
@Test
public void collect() {
/**
* collect
* 将流转换为指定的类型,比如List转换为List、Set、Map
*/
List<User> userList = new ArrayList<>();
userList.add(new User().setId(1).setAge("19").setName("上官婉儿"));
userList.add(new User().setId(2).setAge("18").setName("老夫子"));
userList.add(new User().setId(3).setAge("20").setName("虞姬"));
userList.forEach(user -> System.out.println(user.toString()));
// List<User> 转 List<String>
List<String> listInUserToString = userList.stream().map(User::getName).collect(Collectors.toList());
listInUserToString.forEach(name -> System.out.println(name));
// List<User> 转 name,name,name 字符串
String listUserToString = userList.stream().map(User::getName).collect(Collectors.joining(","));
System.out.println("listUserToString ====> " + listUserToString);
// List<User> 转 List<UserInfo>
List<UserInfo> listInUserToUserInfo = userList.stream().map(u -> new UserInfo().setId(u.getId()).setName(u.getName()).setAge(u.getAge())).collect(Collectors.toList());
listInUserToUserInfo.forEach(userInfo -> System.out.println(userInfo.toString()));
// List<User> 转 Map<String, User> ( 数据本身:Function.identity(),要生成的key和value都是自定义的 )
Map<String, User> listToMapSU = userList.stream().collect(Collectors.toMap(User::getName, Function.identity()));
System.out.println(listToMapSU);
}
count(统计数量)
@Test
public void count() {
/**
* count
* count方法可以统计元素的个数
*/
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
long numbersLength = numbers.stream().count();
System.out.println("numbers集合的长度:" + numbersLength);
}
max(最大值)
@Test
public void max() {
/**
* max
* 匹配元素最大值并返回
*/
List<String> numbers = Arrays.asList("1", "2", "3", "4", "5", "6");
String max = numbers.stream().max(Comparator.comparing(Integer::parseInt)).get();
System.out.println("max:" + max);
}
min(最小值)
@Test
public void min() {
/**
* min
* 匹配元素最小值并返回
*/
List<String> numbers = Arrays.asList("1", "2", "3", "4", "5", "6");
String min = numbers.stream().min(Comparator.comparing(Integer::parseInt)).get();
System.out.println("min:" + min);
}