stream流操作技巧

实体:
package com.test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
import java.math.BigDecimal;


@JacksonXmlRootElement(localName = "xxx")
@Data
public class User {

@JsonProperty("CCName")
private String CCName;

private Integer age;

private String group;

private BigDecimal ss;

public void setCCName(String hello) {
this.CCName = hello;
}
}



测试类:
package com.test;

import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import javax.xml.bind.JAXBException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class XmlTests {
private User user;

@Test
public void testStream() {
User u = new User();
u.setCCName("hello");
u.setAge(18);
u.setGroup("F");
u.setSs(new BigDecimal("1"));
User u1 = new User();
u1.setCCName("hi");
u1.setGroup("F");
u1.setAge(28);
u1.setSs(new BigDecimal("12"));
User u2 = new User();
u2.setCCName("nice");
u2.setAge(38);
u2.setGroup("T");
u2.setSs(new BigDecimal("11"));
User u3 = new User();
u3.setCCName("nice");
u3.setAge(38);
u3.setGroup("T");
u3.setSs(new BigDecimal("12"));
List<User> source = new ArrayList<>();
Collections.addAll(source, u, u1, u2,u3);
log.info("操作前{}",source);
//去重
List<User> list1 = source.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getCCName()))), ArrayList::new));
log.info("根据某个属性去重后的{}",list1);

//分组
Map<String, List<User>> byDept = source.stream().collect(Collectors.groupingBy(User::getGroup));
log.info("分组后的{}",byDept);
Map<String, List<User>> byDept1 = source.stream().collect(Collectors.groupingBy(e -> e.getGroup() + "_" + e.getAge()));
log.info("多个Key分组后的{}",byDept1);

//拼接
List<String> jj = source.stream().map(User::getCCName).collect(Collectors.toList());
String[] strings = jj.toArray(new String[jj.size()]);
Stream<String> stream = Stream.of(strings);
//String joined = stream.collect(Collectors.joining());// "Iloveyou"
//String joined = stream.collect(Collectors.joining(","));// "I,love,you"
String joined = stream.collect(Collectors.joining(",", "{", "}"));// "{I,love,you}"
log.info("拼接后的{}",joined);

//排序
source.sort(Comparator.comparing(User::getAge));
log.info("自然排序后的{}",source);

source.sort((o1,o2) -> {
Integer k1 = o1.getAge();
Integer k2 = o2.getAge();
return k1.compareTo(k2);
});
log.info("自然排序后的{}",source);

source.stream().sorted(Comparator.comparing(User::getAge));
log.info("自然排序后的{}",source);

List<User> users = source.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());//根据年龄逆序
log.info("根据年龄倒序后的{}",users);

int all = source.stream().map(User::getAge)
.reduce(0,Integer::sum);
log.info("int年龄综合为{}",all);
int max = source.stream().map(User::getAge)
.reduce(0,Integer::max);
log.info("int年龄最大为{}",max);
BigDecimal all1 = source.stream().map(User::getSs)
.reduce(BigDecimal.ZERO,BigDecimal::add);
log.info("BigDecimal年龄综合为{}",all1);
BigDecimal maxBigDecimal = source.stream().map(User::getSs)
.reduce(BigDecimal.ZERO,BigDecimal::max);
log.info("BigDecimal年龄最大为{}",maxBigDecimal);

double mm = source.stream().mapToInt(User::getAge).average().getAsDouble();
log.info("int,long年龄平均值为{}",mm);

//BigDecimal类型的值求平均数的时候必须转成double,否则不能用average求平均数方法
double qw = source.stream().mapToDouble(e -> {
return e.getSs().doubleValue();
}).average().getAsDouble();
log.info("年龄平均值为{}",qw);

//filter方法使用
List<User> re = source.stream().filter(e -> e.getAge() > 18).collect(Collectors.toList());
log.info("过滤年龄大于18岁的集合为{}",re);

source.stream().forEach(e -> e.setAge(e.getAge() * 3));
log.info("年龄都乘以3{}",re);

String text = "ss";
int qwqq = Optional.ofNullable(text).map(String::length).orElse(-1);
log.info("Optional的用法{}",qwqq);

//list转map
Map<Integer, String> result1 = list1.stream().collect(
Collectors.toMap(User::getAge, User::getCCName));
log.info("list转成map后的结果为{}",result1);
for (Map.Entry<Integer, String> m : result1.entrySet()){
log.info("遍历map {}",m.getKey() + ";;;" + m.getValue());
}

//取差集
public static List<ClmPendDtlVO> diffList(List<ClmPendDtlVO> pendDtlList,List<ClmPendDtlVO> lastPendDtlList) {
List<ClmPendDtlVO> resultList = pendDtlList.stream()
.filter(item -> !lastPendDtlList.stream().map(e -> e.getCItem())
.collect(Collectors.toList()).contains(item.getCItem()))
.collect(Collectors.toList());
return resultList;
}

//取交集,并且去重
public static List<ClmPendDtlVO> inSectionList(List<ClmPendDtlVO> pendDtlList,List<ClmPendDtlVO> lastPendDtlList) {
List<ClmPendDtlVO> resultList = pendDtlList.stream()
.filter(item -> lastPendDtlList.stream().map(e -> e.getCItem())
.collect(Collectors.toList()).contains(item.getCItem()))
.collect(Collectors.toList());
return resultList;
}

}
}
原文地址:https://www.cnblogs.com/sunny-miss/p/10951916.html