stream是java8的新特性。基于lambda表达式,是对集合对象功能的增强,它专注于对集合对象进行各种高效、便利的聚合操作或者大批量的数据操作,提高了编程效率和代码可读性。
Stream的原理:将要处理的元素看做一种流,流在管道中传输,并且可以在管道的节点上处理,包括过滤筛选、去重、排序、聚合等。元素流在管道中经过中间操作的处理,最后由最终操作得到前面处理的结果。
User实体类
@Data
@AllArgsConstructor
public class User {
private Integer id;
private String userName;
}
例1:list转map,list<Object>根据对象里的一个属性作为key,对应的对象作为value转成map【此方法,key不可以重复,如果重复会报错】
List<User> userList = new ArrayList<>();
userList.add(new User(1,"aaa"));
userList.add(new User(2,"bbb"));
userList.add(new User(3,"ccc"));
userList.add(new User(4,"ddd"));
Map<Integer, User> scTempMap = userList.stream().collect(Collectors.toMap(User::getId, var -> var));
System.out.println(scTempMap);
结果:
{1=User(id=1, userName=aaa), 2=User(id=2, userName=bbb), 3=User(id=3, userName=ccc), 4=User(id=4, userName=ddd)}
错误案例:
例2:list转map,list<Object>根据对象里的一个属性作为key,对应的对象作为value转成map【key重复的情况下,(v1, v2) -> v2 后面的会覆盖前面的value,(v1, v2) -> v1 取得是前面的value】
List<User> userList = new ArrayList<>();
userList.add(new User(1,"aaa"));
userList.add(new User(2,"bbb"));
userList.add(new User(3,"ccc"));
userList.add(new User(1,"ddd"));
Map<Integer, User> scTempMap = userList.stream().collect(
Collectors.toMap(User::getId, Function.identity(), (v1, v2) -> v2));
System.out.println(scTempMap);
结果:{1=User(id=1, userName=ddd), 2=User(id=2, userName=bbb), 3=User(id=3, userName=ccc)}
例3:list分组,list<User>根据某个属性进行分组,得出Map<String,List<User>>
List<User> userList = new ArrayList<>();
userList.add(new User(1,"aaa"));
userList.add(new User(2,"bbb"));
userList.add(new User(3,"ccc"));
userList.add(new User(1,"ddd"));
Map<Integer, List<User>> userMap = userList.parallelStream().collect(
Collectors.groupingBy(User::getId));
System.out.println(userMap);
结果:
{1=[User(id=1, userName=aaa), User(id=1, userName=ddd)], 2=[User(id=2, userName=bbb)], 3=[User(id=3, userName=ccc)]}
例4:list去重,List<User>根据某个属性去重,得到新的List<User>
List<User> userList = new ArrayList<>();
userList.add(new User(1,"aaa"));
userList.add(new User(2,"bbb"));
userList.add(new User(3,"ccc"));
userList.add(new User(1,"ddd"));
List<User> userList1 = userList.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(User::getId))),ArrayList::new));
System.out.println(userList1);
结果:
[User(id=1, userName=aaa), User(id=2, userName=bbb), User(id=3, userName=ccc)]
例5:list去重,根据多个属性进行去重
List<User> userList1 = userList.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(o ->
o.getId()+o.getUserName()))), ArrayList::new));
例6:list排序,List<User>根据某个属性进行排序
List<User> userList = new ArrayList<>();
userList.add(new User(1,"aaa"));
userList.add(new User(2,"bbb"));
userList.add(new User(3,"ccc"));
userList.add(new User(1,"ddd"));
//升序
List<User> userList1 = userList.stream().sorted(Comparator.comparing(User::getId))
.collect(Collectors.toList());
结果:
[User(id=1, userName=aaa), User(id=1, userName=ddd), User(id=2, userName=bbb), User(id=3, userName=ccc)]
//降序
List<User> userList1 = userList.stream().sorted(Comparator.comparing(User::getId)
.reversed()).collect(Collectors.toList());
结果:
[User(id=3, userName=ccc), User(id=2, userName=bbb), User(id=1, userName=aaa), User(id=1, userName=ddd)]
例7:list排序,根据属性A进行升序,相同的根据属性B降序
List<User> userList = new ArrayList<>();
userList.add(new User(1,"aaa"));
userList.add(new User(2,"bbb"));
userList.add(new User(3,"ccc"));
userList.add(new User(1,"ddd"));
List<User> userList1 = userList.stream().sorted(
Comparator.comparing(User::getId)
.thenComparing(
Comparator.comparing(User::getUserName).reversed()))
.collect(Collectors.toList());
System.out.println(userList1);
结果:
[User(id=1, userName=ddd), User(id=1, userName=aaa), User(id=2, userName=bbb), User(id=3, userName=ccc)]
例8:list过滤,根据某个属性进行过滤
List<User> userList = new ArrayList<>();
userList.add(new User(1,"aaa"));
userList.add(new User(2,"bbb"));
userList.add(new User(3,"ccc"));
userList.add(new User(1,"ddd"));
List<User> userList1 = userList.stream().filter(user -> "bbb".equals(user.getUserName()) || 3==user.getId()).collect(Collectors.toList());
System.out.println(userList1);
结果:
[User(id=2, userName=bbb), User(id=3, userName=ccc)]
ps:实用的方法还有好多,等碰到了会继续写进去