1.转换list集合中元素的类型并去重
List<Long> ids= list.stream().map(o -> o.getId()).distinct().collect(Collectors.toList());
2.将list集合转换为map
Map<Long,Book> map=list.stream().collect(Collectors.toMap(Book::getId, a -> a, (k1, k2) -> k1));
Map<Long, String> map= list.stream().collect(Collectors.toMap(Book::getId, Book::getName, (k1, k2) -> k1)));
3.将list集合根据元素对象的属性分组
//根据元素属性A分组
Map<String,List<Book>> map = list.stream().collect(Collectors.groupingBy(Book::getName));
4.将list集合根据元素属性A分组,并且组中BigDecimal元素属性B累加,转map(CollectorsUtil工具类)
//根据属性A分组,并且组中BigDecimal属性B累加,转map
Map<Long, BigDecimal> map = list.stream().filter(o -> o.getPrice().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.groupingBy(Book::getId,CollectorsUtil.summingBigDecimal(Book::getPrice)));
//工具类
package ...
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
/**
* @description: Collectors扩展处理BigDecimal数据类
* @author: jinll
**/
public class CollectorsUtil {
static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet();
private CollectorsUtil() {
}
/**
* @param <I>
* @param <R>
* @return
*/
@SuppressWarnings("unchecked")
private static <I, R> Function<I, R> castingIdentity() {
return i -> (R) i;
}
/**
* Simple implementation class for {@code Collector}.
* @param <T> the type of elements to be collected
* @param <R> the type of the result
*/
static class CollectorImpl<T, A, R> implements Collector<T, A, R> {
private final Supplier<A> supplier;
private final BiConsumer<A, T> accumulator;
private final BinaryOperator<A> combiner;
private final Function<A, R> finisher;
private final Set<Characteristics> characteristics;
CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Function<A, R> finisher, Set<Characteristics> characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.characteristics = characteristics;
}
CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Set<Characteristics> characteristics) {
this(supplier, accumulator, combiner, castingIdentity(), characteristics);
}
@Override
public BiConsumer<A, T> accumulator() {
return accumulator;
}
@Override
public Supplier<A> supplier() {
return supplier;
}
@Override
public BinaryOperator<A> combiner() {
return combiner;
}
@Override
public Function<A, R> finisher() {
return finisher;
}
@Override
public Set<Characteristics> characteristics() {
return characteristics;
}
}
/**
* 求和
* @param mapper
* @param <T>
* @return
*/
public static <T> Collector<T, ?, BigDecimal> summingBigDecimal(ToBigDecimalFunction<? super T> mapper) {
return new CollectorImpl<>(
() -> new BigDecimal[]{new BigDecimal(0)},
(a, t) -> {
a[0] = a[0].add(mapper.applyAsBigDecimal(t), MathContext.DECIMAL32);
},
(a, b) -> {
a[0] = a[0].add(b[0], MathContext.DECIMAL32);
return a;
},
a -> a[0], CH_NOID);
}
/**
* 求最大,这里的最小MIN值,作为初始条件判断值,如果某些数据范围超过百亿以后,可以根据需求换成Long.MIN_VALUE或者Double.MIN_VALUE
* @param mapper
* @param <T>
* @return
*/
public static <T> Collector<T, ?, BigDecimal> maxBy(ToBigDecimalFunction<? super T> mapper) {
return new CollectorImpl<>(
() -> new BigDecimal[]{new BigDecimal(Integer.MIN_VALUE)},
(a, t) -> {
a[0] = a[0].max(mapper.applyAsBigDecimal(t));
},
(a, b) -> {
a[0] = a[0].max(b[0]);
return a;
},
a -> a[0], CH_NOID);
}
/**
* 求最小,这里的最大MAX值,作为初始条件判断值,如果某些数据范围超过百亿以后,可以根据需求换成Long.MAX_VALUE或者Double.MAX_VALUE
* @param mapper
* @param <T>
* @return
*/
public static <T> Collector<T, ?, BigDecimal> minBy(ToBigDecimalFunction<? super T> mapper) {
return new CollectorImpl<>(
() -> new BigDecimal[]{new BigDecimal(Integer.MAX_VALUE)},
(a, t) -> {
a[0] = a[0].min(mapper.applyAsBigDecimal(t));
},
(a, b) -> {
a[0] = a[0].min(b[0]);
return a;
},
a -> a[0], CH_NOID);
}
/**
* 求平均,并且保留小数
* @param newScale 保留小数位数
* @param roundingMode 小数处理方式
* #ROUND_UP 进1
* #ROUND_DOWN 退1
* #ROUND_CEILING 进1截取:正数则ROUND_UP,负数则ROUND_DOWN
* #ROUND_FLOOR 退1截取:正数则ROUND_DOWN,负数则ROUND_UP
* #ROUND_HALF_UP >=0.5进1
* #ROUND_HALF_DOWN >0.5进1
* #ROUND_HALF_EVEN
* #ROUND_UNNECESSARY
*/
public static <T> Collector<T, ?, BigDecimal> averagingBigDecimal(ToBigDecimalFunction<? super T> mapper, int newScale, int roundingMode) {
return new CollectorImpl<>(
() -> new BigDecimal[]{new BigDecimal(0), new BigDecimal(0)},
(a, t) -> {
a[0] = a[0].add(mapper.applyAsBigDecimal(t));
a[1] = a[1].add(BigDecimal.ONE);
},
(a, b) -> {
a[0] = a[0].add(b[0]);
return a;
},
a -> a[0].divide(a[1], MathContext.DECIMAL32).setScale(newScale, roundingMode), CH_NOID);
}
}
5.将list集合根据元素属性A去重,并且重复的元素属性B(BigDecimal)累加
List<Book> books = list.stream().collect(Collectors.toMap(Book::getId, a -> a, (o1, o2) -> {
o1.setPrice(o1.getPrice().add(o2.getPrice()));
return o1;
})).values().stream().collect(Collectors.toList());
6.将list集合根据元素属性A合并=>元素属性B累加(其他同理)
List<Book> books= list.stream().collect(Collectors.toMap(Book::getId, a -> a, (o1,o2)-> {
o1.setNums(o1.getNums() + o2.getNums());
o1.setSums(o1.getSums() + o2.getSums());
return o1;
})).values().stream().collect(Collectors.toList());
7.将list集合根据元素属性去重
//根据元素属性A和元素属性B去重
List<Book> books= list.stream().filter(temp -> ids.contains(temp.getId())).collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId() + ";" + o.getName()))), ArrayList::new));
//根据元素属性A去重
List<Book> books= list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Book::getId))), ArrayList::new));
8.将list集合中元素的BigDecimal或Double类型属性累加
BigDecimal sum= list.stream().filter(o -> ids.contains(o.id()) && (!StringUtils.isEmpty(o.getPrice()))).map(x -> x.getPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
double sum = list.stream().mapToDouble(Book::getPrice).sum();
三级联查(品名=>品牌=>单位)【去重】
//品名集合
List<Book> classList = list.stream().map(temp1-> {
Book book = new Book();
//设置品名id、品名
book.setId(temp1.getClassId());
book.setName(temp1.getClassName());
//品名下的品牌集合
List<Book> brandList = list.stream().filter(s -> s.getClassId().equals(temp1.getClassId())).map(temp2 -> {
Book brandChildren= new Book();
//设置品牌id、品牌名
brandChildren.setId(temp2.getBrandId());
brandChildren.setName(temp2.getBrandName());
//品牌下的单位集合
List<Book> unitList = list.stream().filter(s -> s.getClassId().equals(temp1.getClassId()) && s.getBrandId().equals(temp2.getBrandId())).map(temp3 -> {
Book unitChildren = new Book();
//设置单位id、单位、物料id
unitChildren.setId(temp3.getUnitId());
unitChildren.setName(temp3.getUnitName());
unitChildren.setPrice(temp3.getPrice());
return unitChildren;
//根据单位id去重
}).filter(distinctByKey(o -> o.getId())).collect(Collectors.toList());
brandChildren.setChildren(unitList);
return brandChildren;
//根据品牌id去重
}).filter(distinctByKey(o->o.getId())).collect(Collectors.toList());
book.setChildren(brandList);
return book ;
//根据品名id去重
}).filter(distinctByKey(o->o.getId())).collect(Collectors.toList());
//根据对象属性去重
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}