Java8从发布到现在已经很久了,慢慢的也开始被广泛使用,不得不说java8的一大亮点是lambda表达式以及Stream流,它将函数式编程的思想很好的融入到了java当中,使得我们的java更加的强大。今天我们就来看看stream流与lambda表达式对我们集合的各种操作带来了多大的便捷。
自定义排序以及过滤所需数据
List<String> list = new ArrayList<>();
list.add("two2");
list.add("one");
list.add("three");
//默认
System.out.println(list);
System.out.println("----------------------------------");
/**
* 基本使用stream以及lambda
*/
//留下长度小于3的值
list.stream().filter(s -> s.length() <= 3).forEach(s -> System.out.println(s));
System.out.println("----------------------------------");
//默认排序
list.stream().sorted().forEach(s -> System.out.println(s));
System.out.println("----------------------------------");
//自定义排序
list.stream().sorted((s1,s2) -> s1.length() - s2.length()).forEach(s -> System.out.println(s));
System.out.println("----------------------------------");
//将所有英文字母大写
list.stream().map(s -> s.toUpperCase()).forEach(s -> System.out.println(s));
System.out.println("----------------------------------");
将一个集合中存放的多个集合内容进行平铺
//将一个集合中存放的多个集合内容进行平铺
List<List> allList = new ArrayList<>();
List<String> oneList = new ArrayList<>();
oneList.add("one");
oneList.add("two");
List<String> twoList = new ArrayList<>();
twoList.add("three");
twoList.add("four");
allList.add(oneList);
allList.add(twoList);
allList.stream().flatMap(s ->s.stream()).forEach(s -> System.out.println(s));
System.out.println("----------------------------------");
取出一组集合中长度最长的字符串
/**
* 使用reduce
*/
//取出一组集合中长度最长的字符串
List<String> reduceList = new ArrayList<>();
reduceList.add("one");
reduceList.add("two");
reduceList.add("three");
reduceList.add("four");
//方法一
Optional<String> wayOne = reduceList.stream().max((s1, s2) -> s1.length() - s2.length());
System.out.println(wayOne.get());
//方法二
Optional<String> wayTwo = reduceList.stream().reduce((s1, s2) -> s1.length() >= s2.length() ? s1 : s2);
System.out.println(wayTwo.get());
System.out.println("----------------------------------");
取出一组集合中所有字符串长度的总和
//取出一组集合中所有字符串长度的总和
List<String> lengthCountList = new ArrayList<>();
lengthCountList.add("one");
lengthCountList.add("two");
lengthCountList.add("three");
Integer size = lengthCountList.stream().reduce(0, (max, str) -> max + str.length(), (a, b) -> a + b);
System.out.println(size);
System.out.println("----------------------------------");
将list转换成map key为字符串 value为字符串长度
/**
* 使用collect
*/
//将list转换成map key为字符串 value为字符串长度
List<String> collectList = new ArrayList<>();
collectList.add("a");
collectList.add("ab");
collectList.add("abc");
// Function.identity()返回一个输出跟输入一样的Lambda表达式对象,等价于形如t -> t形式的Lambda表达式。
Map<String, Integer> collectMap = collectList.stream().collect(Collectors.toMap(Function.identity(), str -> str.length()));
System.out.println(collectMap);
System.out.println("----------------------------------");
以60为界限将集合进行划分
//以60为接线将集合进行划分
List<Integer> partitionList = new ArrayList<>();
partitionList.add(58);
partitionList.add(59);
partitionList.add(62);
Map<Boolean, List<Integer>> partitionMap = partitionList.stream().collect(Collectors.partitioningBy(s -> s >= 60));
System.out.println(partitionMap);
System.out.println("----------------------------------");
根据年龄进行划分
//根据年龄进行划分
//准备数据
List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setName("beyondLi1");
user1.setAge(18);
User user2 = new User();
user2.setName("beyondLi2");
user2.setAge(18);
User user3 = new User();
user3.setName("beyondLi3");
user3.setAge(20);
User user4 = new User();
user4.setName("beyondLi4");
user4.setAge(22);
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
//分类
Map<Integer, List<User>> groupUser1 = userList.stream().collect(Collectors.groupingBy(User::getAge));
//或
Map<Integer, List<User>> groupUser2 = userList.stream().collect(Collectors.groupingBy(s -> s.getAge()));
System.out.println(groupUser1);
System.out.println(groupUser2);
System.out.println("----------------------------------");
根据年龄进行划分,求出各年龄的人数
//根据年龄进行划分,求出各年龄的人数
//数据使用上面的user集合
Map<Integer, Long> groupMap = userList.stream().collect(Collectors.groupingBy((User::getAge), Collectors.counting()));
System.out.println(groupMap);
System.out.println("----------------------------------");
根据年龄进行划分,查看个分组的人名
//根据年龄进行划分,查看个分组的人名
Map<Integer, List<String>> groupList = userList.stream().collect(Collectors.groupingBy(User::getAge, Collectors.mapping(User::getName, Collectors.toList())));
System.out.println(groupList);
System.out.println("----------------------------------");
对字符串集合进行拼接
//对字符串集合进行拼接
List<String> joiningList = new ArrayList<>();
joiningList.add("My");
joiningList.add("name");
joiningList.add("is");
joiningList.add("beyondLi");
//以逗号分割
String joiningStringOne = joiningList.stream().collect(Collectors.joining(","));
System.out.println(joiningStringOne);
//以逗号分割使用花括号扩起
String joiningStringTwo = joiningList.stream().collect(Collectors.joining(",", "{", "}"));
System.out.println(joiningStringTwo);