菜单
本页目录
  1. List 【Long】转 【String】
Set<String> planSet = planList.stream().map(WPromotionPlan :: getPlanId).map(Object::toString).collect(Collectors.toSet());
  1. Set 转 String
String.join(",", planSet)
  1. sort排序
1. 倒序
List<Integer> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

User u1 = new User("陈一",32);
List<User> collect = users.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());


2. 正序
List<Integer> collect = list.stream().sorted().collect(Collectors.toList());

User u1 = new User("陈一",32);
List<User> collect  = users.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());


3. 升级
//先以属性一升序,升序结果进行属性一降序,再进行属性二升序
list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二));
list.stream().sorted(Comparator.comparing(类::属性一).reversed().thenComparing(类::属性二,Comparator.reverseOrder()));

  1. 统计(求和、最大、最小、平均)
1. BigDecimal示例
 1.1求和
    1.1.1 Bean求和
        BigDecimal sum = list.stream().map(User::getHeight).reduce(BigDecimal.ZERO, BigDecimal::add);
    1.1.2 BigDecimal求和
        BigDecimal sum = list.stream().reduce(BigDecimal.ZERO, (b1, b2) -> b1.add(b2));
        BigDecimal sum = Arrays.stream(bdArray).reduce(BigDecimal.ZERO, (p, q) -> p.add(q)); 
    1.1.3 自定义的方法求和
        public class Utility {
	        public static BigDecimal addWeight(BigDecimal w1, BigDecimal w2) {
	    	    return w1.add(w2);
	        }
        } 
        BigDecimal sum = map.values().stream().reduce(BigDecimal.ZERO, Utility::addWeight);  

    1.2 最大(BigDecimal)
        Optional<User> max = list.stream().max((u1, u2) -> u1.getHeight().compareTo(u2.getHeight()));
    1.3 最小(BigDecimal)
        Optional<User> min = list.stream().min((u1, u2) -> u1.getHeight().compareTo(u2.getHeight()));

2. Double示例:
//求和
double sum = list.stream().mapToDouble(User::getHeight).sum();
//最大
double max = list.stream().mapToDouble(User::getHeight).max().getAsDouble();
//最小
double min = list.stream().mapToDouble(User::getHeight).min().getAsDouble();
//平均值
double average = list.stream().mapToDouble(User::getHeight).average().getAsDouble();


  1. 【List】 Bean提取一个列成立一个新的List
List<Integer> userNameList = userList.stream().map(User::getAge).collect(Collectors.toList());
  1. 提取部分字段生成一个新的List
List<MemberInfo> newList = list.stream().map(it -> new MemberInfo(it.getRoleName(), it.getRole(), it.getRoleUrl())).collect(Collectors.toList());
  1. List userNameList = userList.stream().map(User::getAge).collect(Collectors.toList());