package com.demo.test.util;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class ListHelper {
/**
* example ListHelper.firstOrDefault(students, n -> n.getAge()==12)
*/
public static <T> T firstOrDefault(List<T> list, Predicate<? super T> predicate) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
Optional<T> first = list.stream().filter(predicate).findFirst();
return first.orElse(null);
}
/**
* ListHelper.where(students, n -> n.getAge() >12)
*/
public static <T> List<T> where(List<T> list, Predicate<? super T> predicate) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
return list.stream().filter(predicate).collect(Collectors.toList());
}
/**
* example ListHelper.select(students, n -> n.getAge())
*/
public static <T, E> List<E> select(List<T> list, Function<? super T, E> express) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
Set<E> eList = new HashSet<>();
for (T t : list) {
E e = express.apply(t);
eList.add(e);
}
return Lists.newArrayList(eList);
}
/**
* example ListHelper.isRepeated(students,n->n.getName()
*/
public static <T, E> boolean isRepeated(List<T> list, Function<? super T, E> express) {
if (CollectionUtils.isEmpty(list)) {
return false;
}
for (int i = 0; i < list.size() - 1; i++) {
E e = express.apply(list.get(i));
for (int j = i + 1; j < list.size(); j++) {
E e2 = express.apply(list.get(j));
if (e.equals(e2)) {
return true;
}
}
}
return false;
}
/**
* 分组
*/
public static <T, E> HashMap<E, List<T>> group(List<T> list, Function<? super T, E> express) {
if (CollectionUtils.isEmpty(list)) {
return new HashMap<>();
}
HashMap<E, List<T>> groups = new HashMap<>();
for (T t : list) {
E e = express.apply(t);
if (groups.keySet().contains(e)) {
groups.get(e).add(t);
} else {
List<T> tempList = new ArrayList<>();
tempList.add(t);
groups.put(e, tempList);
}
}
return groups;
}
/**
* example ListHelper.orderBy(students,n->n.getAge())
*/
public static <T, U extends Comparable<? super U>> List<T> orderBy(List<T> list, Function<? super T, ? extends U> keyExtractor) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
list.sort(Comparator.comparing(keyExtractor));
return list;
}
/**
* example ListHelper.orderBy(students,n->n.getAge())
*/
public static <T, U extends Comparable<? super U>> List<T> orderByDesc(List<T> list, Function<? super T, ? extends U> keyExtractor) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
list.sort(Comparator.comparing(keyExtractor).reversed());
return list;
}
/**
* example ListHelper.max(students,n->n.getAge())
*/
public static <T, U extends Comparable<? super U>> T max(List<T> list, Function<? super T, ? extends U> keyExtractor) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
list.sort(Comparator.comparing(keyExtractor));
if (list.size() > 0) {
return list.get(list.size() - 1);
}
return null;
}
/**
* example ListHelper.min(student,n->n.getAge())
*/
public static <T, U extends Comparable<? super U>> T min(List<T> list, Function<? super T, ? extends U> keyExtractor) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
list.sort(Comparator.comparing(keyExtractor));
if (list.size() > 0) {
return list.get(0);
}
return null;
}
public static List<String> objectListToStringList(List<? extends Object> objList) {
if (CollectionUtils.isEmpty(objList)) {
return new ArrayList<>();
}
List<String> strList = new ArrayList<>();
for (Object obj : objList) {
strList.add(JsonUtil.toJSONString(obj));
}
return strList;
}
public static <T> List<T> deepCopy(List<T> src, Class<T> tClass) {
String srcStr = JsonUtil.toJSONString(src);
return JsonUtil.toJavaObjectList(srcStr, tClass);
}
/**
* 映射,A类型集合转成B类型集合
*/
public static <T, E> List<E> map(List<T> list, Function<? super T, E> express) {
if (CollectionUtils.isEmpty(list)) {
return Lists.newArrayList();
}
return list.stream().filter(Objects::nonNull)
.map(express).collect(Collectors.toList());
}
/**
* ListHelper.filter(students, n -> n.getAge() >12)
*/
public static <T> List<T> filter(List<T> list, Predicate<? super T> predicate) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList<>();
}
return list.stream().filter(Objects::nonNull)
.filter(predicate).collect(Collectors.toList());
}
/**
* 获取集合中实体属性<BR/>
* ListHelper.listEntityAttribute(students, n -> n.getAge())
*/
public static <T, E> List<E> listEntityAttribute(List<T> list, Function<? super T, E> express) {
if (CollectionUtils.isEmpty(list)) {
return Lists.newArrayList();
}
Set<E> eList = new HashSet<>(list.size());
for (T t : list) {
if (Objects.nonNull(t)) {
E e = express.apply(t);
if (Objects.nonNull(e)) {
eList.add(e);
}
}
}
return Lists.newArrayList(eList);
}
/**
* 过滤掉集合中的null
*
* @param list
* @return
*/
public static <T> List<T> filterNull(List<T> list) {
return Optional.ofNullable(list).map(x ->
x.stream().filter(Objects::nonNull).collect(Collectors.toList()))
.orElse(Lists.newArrayList());
}
/**
* 过滤集合中的空字符串并去重
*
* @param stringList
* @return
*/
public static List<String> filterEmptyString(List<String> stringList) {
return Optional.ofNullable(stringList)
.map(x -> x.stream()
.filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList()))
.orElse(Lists.newArrayList());
}
/**
* 集合去重
*/
public static <T> List<T> distinct(List<T> list) {
if (CollectionUtils.isEmpty(list)) {
return Lists.newArrayList();
}
Set<T> eList = new HashSet<>(list.size());
for (T t : list) {
if (Objects.nonNull(t)) {
eList.add(t);
}
}
return Lists.newArrayList(eList);
}
/**
* example: ListHelper.convertToMap(students,n->n.getName() <BR/><BR/>
* 集合根据实体属性转成map
*/
public static <T, E> Map<E, T> convertToMap(List<T> list, Function<? super T, E> express) {
if (CollectionUtils.isEmpty(list)) {
return new HashMap<>();
}
return list.stream().filter(Objects::nonNull).collect(
Collectors.toMap(express, x -> x, (v1, v2) -> v2)
);
}
/**
* 集合转成map
*
* @param list
* @param keyMapper
* @param valueMapper
* @param <T>
* @param <K>
* @param <U>
* @return map: key: 实体类一个属性, value: 实体类一个属性
*/
public static <T, K, U> Map<K, U> convertToMap(List<T> list,
Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper) {
if (CollectionUtils.isEmpty(list)) {
return new HashMap<>();
}
return list.stream().filter(x -> Objects.nonNull(x) && Objects.nonNull(valueMapper.apply(x))).collect(
Collectors.toMap(keyMapper, valueMapper, (v1, v2) -> v2)
);
}
/**
* example: ListHelper.groupByAttribute(students,n->n.getName() <BR/><BR/>
* 根据属性将集合分组
*/
public static <T, E> Map<E, List<T>> groupByAttribute(List<T> list, Function<? super T, E> express) {
if (CollectionUtils.isEmpty(list)) {
return new HashMap<>();
}
return list.stream()
.filter(Objects::nonNull)
.filter(x -> Objects.nonNull(express.apply(x)))
.collect(Collectors.groupingBy(express));
}
/**
* 减少NPE错误
*
* @param list
* @param <T>
* @return
*/
public static <T> List<T> checkNPE(List<T> list) {
return CollectionUtils.isEmpty(list) ? new ArrayList<>() : list;
}
}
版权声明:本文为qianhuan_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。