从一个对象列表中获取某个属性的值的列表,
从一个对象列表中根据某个字段的最小/最大值获取其中的一个对象,
从一个对象列表中根据某个数字类型的字段进行累加,
将一个对象列表根据某个字段进行分组,变为Map,
……
相信以上很多很场景在你的编程生涯中都会遇到,那你是否还自己写for循环进行判断呢?来看看我是如何让代码更简洁!
1、整型求和
Bash
/**
* 整型求和
*
* @param list
* @return
*/
public Integer sumInt(List<Integer> list) {
Integer sum = list.stream().reduce(Integer::sum).orElse(0);
return sum;
}
2、对象列表某个字段求和
Bash
/**
* 对象列表里的某个字段求和,这里还是有mapToLong等等
* {@link java.util.stream.Stream#mapToLong}
*
* @param list
* @return
*/
public Integer sum(List<Order> list) {
return list.stream().mapToInt(Order::getSellNum).sum();
}
3、BigDecimal求和,不支持为null的情况
Bash
/**
* BigDecimal求和,不支持为null的情况
*
* @param list
* @return
*/
public BigDecimal sumBigDecimal(List<Order> list) {
BigDecimal totalQuantity = list.stream().map(Order::getSellPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
return totalQuantity;
}
4、求和,支持NULL的情况
Bash
/**
* 求和,支持NULL的情况
*
* @param list
* @return
*/
public BigDecimal sumBigDecimalOfNull(List<Order> list) {
BigDecimal totalQuantity = list.stream().map(Order::getSellPrice).reduce(BigDecimal.ZERO, BigDecimalUtil::sum);
return totalQuantity;
}
5、去重,根据某个字段去重
Bash
/**
* 去重,根据某个字段去重
*
* @param list
* @return
*/
public List<Order> duplicateRemoval(List<Order> list) {
List<Order> newList = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getOrderNo()))),
ArrayList::new));
return newList;
}
6、去重,获取map的形式,这种注意Key值的唯一性,否则会忽略数据
Bash
/**
* 去重,获取map的形式,这种注意Key值的唯一性,否则会忽略数据
*
* @param list
* @return
*/
public Map<String, Order> listToMap(List<Order> list) {
return list.stream().collect(Collectors.toMap(item -> item.getOrderNo(), item -> item));
}
/**
* 对比上述方法
* @param list
* @return
*/
public Map<String, Order> listToMap2(List<Order> list) {
return list.stream().collect(Collectors.toMap(Order::getOrderNo, Function.identity()));
}
7、list转map,当list里面的key存在重复时,将以后面的值进行覆盖
Bash
/**
* list转map,当list里面的key存在重复时,将以后面的值进行覆盖
*
* @param list
* @return
*/
public Map<String, Order> listToMap3(List<Order> list) {
return list.stream().collect(Collectors.toMap(item -> item.getOrderNo(), item -> item, (oldV, newV) -> newV));
}
8、根据订单类型进行分组
Bash
/**
* 根据订单类型进行分组
*
* @param list
* @return
*/
public Map<Integer, List<Order>> groupBy(List<Order> list) {
//分组
Map<Integer, List<Order>> groupByOrderType = list.stream().collect(Collectors.groupingBy(Order::getOrderType));
// 遍历分组
for (Map.Entry<Integer, List<Order>> entry : groupByOrderType.entrySet()) {
Integer key = entry.getKey();
List<Order> value = entry.getValue();
}
return groupByOrderType;
}
9、范围过滤——根据某个条件进行过滤,找出订单销售金额大于100的订单
Bash
/**
* 范围过滤——根据某个条件进行过滤,找出订单销售金额大于100的订单
*
* @param list
* @return
*/
public List<Order> filterBySellPrice(List<Order> list) {
return list.stream().filter(item -> item.getSellPrice().compareTo(new BigDecimal(100)) > 0)
.collect(Collectors.toList());
}
10、去除过滤——指定的某个订单数据
Bash
/**
* 去除过滤——指定的某个订单数据
* filter()可以实现一个Predicate
*
* @param list
* @return
*/
public List<Order> filterByOrderNo(List<Order> list) {
List<Order> newList =
list.stream().filter(a -> !a.getOrderNo().equals("OCS20191021122320008")).collect(Collectors.toList());
return newList;
}
11、查询指定时间区间的订单
Bash
/**
* 查询指定时间区间的订单
*
* @param list
* @param beginTime
* @param endTime
* @return
*/
public List<Order> filterByTime(List<Order> list, LocalDateTime beginTime, LocalDateTime endTime) {
List<Order> newList = list.stream().filter(new Predicate<Order>() {
@Override
public boolean test(Order order) {
if (order.getCreateTime().compareTo(beginTime) >= 0 && order.getCreateTime().compareTo(endTime) <= 0) {
return true;
}
return false;
}
}).collect(Collectors.toList());
return newList;
}
12、获取最大销售金额的订单
Bash
/**
* 获取最大销售金额的订单
*
* @param list
* @return
*/
public Order maxSellPrice(List<Order> list) {
return list.stream().max(new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
return o1.getSellPrice().compareTo(o2.getSellPrice());
}
}).get();
}
13、获取最小销售金额的订单
Bash
/**
* 获取最小销售金额的订单
*
* 注意最大最小值三种写法,三种写法都可以
* @param list
* @return
*/
public Order minSellPrice(List<Order> list) {
//list.stream().min((o1, o2) -> o1.getSellPrice().compareTo(o2.getSellPrice())).get()
return list.stream().min(Comparator.comparing(Order::getSellPrice)).get();
}
14、去重,该方法需要重写equals
Bash
/**
* 该方法需要重写equals
* 对比{@link TestLambda#duplicateRemoval}
*
* @param list
* @return
*/
public List<Order> distinctList(List<Order> list) {
return list.stream().distinct().collect(Collectors.toList());
}
15、先排序,再截取,用于排行
Bash
/**
* 先排序,再截取,用于排行
*
* @param list
* @return
*/
public List<Order> limit(List<Order> list) {
List<Order> newList = list.stream().sorted(new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
return o1.getSellPrice().compareTo(o2.getSellPrice());
}
}).limit(5).collect(Collectors.toList());
return newList;
}
16、获取list里面某个属性的列表
Bash
/**
* 获取list里面某个属性的列表
*
* @param list
* @return
*/
public List<String> getFieldList(List<Order> list) {
List<String> orderNoList = list.stream().map(Order::getOrderNo).collect(Collectors.toList());
return orderNoList;
}
17、获取list里面某个属性的列表,不包含空
Bash
/**
* 获取list里面某个属性的列表,不包含空
*
* @param list
* @return
*/
public List<String> getFieldListOfNotNull(List<Order> list) {
List<String> orderNoList =
list.stream().filter(a -> StringUtils.isNotEmpty(a.getOrderNo())).map(Order::getOrderNo)
.collect(Collectors.toList());
return orderNoList;
}
上面的并不是很全,有些方法还没用到,后期再做补充,如果您觉得这篇对您有帮助,请帮忙点个赞,谢谢!