利用流和Lambda表达式对list进行分组、排序、求和、去重、过滤、最值

  • Post author:
  • Post category:其他


	流式数据处理是java8的新特效之一,流处理在加上lambda表达式极大的提高编程效率和程序可读性。
创建一个实体类
public class User {
	
	private Integer id ;
	private String name ;
	private String sex;
	private Integer age;
	private BigDecimal weight;
	public void setId(Integer id){
		this.id = id;
	}
	public Integer getId(){
		return this.id;
	}
	public void setSex(String sex){
		this.sex= sex;
	}

	public String getSex(){
		return this.sex;
	}

	public void setName(String name){
		this.name = name;
	}

	public String getName(){
		return this.name;
	}
	
	public void setAge(Integer age){
		this.age = age
	}
	public Integer getAge(){
		return this.age;
	}
	
	public void setWeight(String weight){
		this.weight = weight;
	}

	public String getWeight(){
		return this.weight;
	}


对list进行操作


	// stream().forEach()遍历集合
	list.stream().forEach(user->{System.out.println(user.getId());});

	//分组
	// Collectors.groupingBy(User::getSex)其实是Collectors.groupingBy(User::getSex,Collectors.toList())的缩写,还可以计算count求和等
	Map<String, List<User>> groupByCity = userList.stream().collect(Collectors.groupingBy(User::getSex));

    Map<String, List<User>> groupByCity = userList.stream().collect(Collectors.groupingBy(x->{
            if(x.getAge()>18){
                return "a";
            }else {
                return "b";
            }
        }));

	
	//遍历分组
	for (Map.Entry<String, List<User>> entryUser : groupBySex.entrySet()) {
    	String key = entryUser.getKey();
    	List<User> entryUserList = entryUser.getValue();
	}

	// 多分组字段
	// 字段写入
	Function<User, List<Object>> compositeKey = f -> Arrays.<Object>asList(f.getAge(), f.getSex(), f.getName());
	// 分组
	Map<Object, List<User>> map = userList.stream().collect(Collectors.groupingBy(compositeKey, Collectors.toList()));
	//遍历分组
	for (Map.Entry<Object, List<User>> entryUser : map.entrySet()) {
 	    List<Object> key = (List<Object>) entryUser.getKey();
  	    Integer age= (Integer) key.get(0);
  	    String sex=  key.get(1).toString() ;
  	    String name=  key.get(2).toString();
   	    List<User> entryUserList = entryUser.getValue();
 	}
        
	//排序
	//根据字段排序
	userList = userList.sort(Comparator.comparing(User::getId));
	//多字段排序,根年龄、性别排序
	userList = userList.sort(Comparator.comparing(User::getAge).thenComparing(User::getSex));
	
	//某字段去重
	userList = userList.stream().map(x->x.getName()).distinct().collect(Collectors.toList());
	
	//过滤
	userList = userList.stream().filter(x->StringUtils.isNotEmpty(x.getName()))..collect(Collectors.toList());

	//求和
	int sumAge = userList.stream().mapToInt(User::getAge).sum();
	BigDecimal totalQuantity = userList.stream().map(User::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
	
	//最值
	//最小
	Integer minAge = userList.stream().map(User::getAge).min(Integer::compareTo).get();
	//最大
	Integer maxAge = userList.stream().map(User::getAge).max(Integer::compareTo).get();
	
	// 赋相同值
	userList.stream().forEach(a -> a.setAge(17));

	// foreach
	userList.forEach(s -> System.out.println(s.getId()));
	// lambda分页
		
	List<User> result = userList.stream().skip(pageSize * (pageIndex - 1)).limit(pageSize).collect(Collectors.toList());

/ 拼接成 [x, y, z] 形式
String result1 = stream1.collect(Collectors.joining(", ", "[", "]"));
// 拼接成 x | y | z 形式
String result2 = stream2.collect(Collectors.joining(" | ", "", ""));
// 拼接成 x -> y -> z] 形式
String result3 = stream3.collect(Collectors.joining(" -> ", "", ""));

boolean aa = strs.stream().anyMatch(str -> str.equals("a"));
anyMatch表示,判断的条件里,任意一个元素成功,返回true
allMatch表示,判断条件里的元素,所有的都是,返回true
noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true



版权声明:本文为yinhaoh原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。