package com.lunhan.xxx.common.util; import java.math.BigDecimal; import java.util.*; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; /** * List工具类 * @author linliu * @date 2018-12-28 */ public final class ListUtil { private ListUtil() { throw new IllegalStateException("Utility class"); } /** * array转list * @param array 数组 * @param 实体类型 */ public static List toList(T... array) { if(array==null || array.length==0) { return new ArrayList<>(); } return Arrays.stream(array).collect(Collectors.toList()); } /** * Long类型 list转array * @param list 集合 */ public static Long[] toLongArray(List list) { if(ListUtil.isNullOrEmpty(list)) { return new Long[0]; } Long[] array = new Long[list.size()]; return list.toArray(array); } /** * Integer类型 list转array * @param list 集合 */ public static Integer[] toIntArray(List list) { if(ListUtil.isNullOrEmpty(list)) { return new Integer[0]; } Integer[] array = new Integer[list.size()]; return list.toArray(array); } /** * BigDecimal类型 list转array * @param list 集合 */ public static BigDecimal[] toDecimalArray(List list) { if(ListUtil.isNullOrEmpty(list)) { return new BigDecimal[0]; } BigDecimal[] array = new BigDecimal[list.size()]; return list.toArray(array); } /** * Double类型 list转array * @param list 集合 */ public static Double[] toDoubleArray(List list) { if(ListUtil.isNullOrEmpty(list)) { return new Double[0]; } Double[] array = new Double[list.size()]; return list.toArray(array); } /** * String类型 list转array * @param list 集合 */ public static String[] toArray(List list) { if(ListUtil.isNullOrEmpty(list)) { return new String[0]; } String[] array = new String[list.size()]; return list.toArray(array); } /** * 判断集合【为】null或empty * @param list 集合 * @param 集合实体类型 */ public static boolean isNullOrEmpty(Collection list){ return list==null || list.isEmpty(); } /** * 判断集合【不为】null或empty * @param list 集合 * @param 集合实体类型 */ public static boolean isNotNullOrEmpty(Collection list){ return !ListUtil.isNullOrEmpty(list); } /** * String类型集合转Integer类型集合 * @param list 集合 */ public static List toIntList(List list) { if(ListUtil.isNullOrEmpty(list)) { return new ArrayList<>(); } return list.stream().map(Integer::parseInt).collect(Collectors.toList()); } /** * String类型集合转Long类型集合 * @param list 集合 */ public static List toLongList(List list) { if(ListUtil.isNullOrEmpty(list)) { return new ArrayList<>(); } return list.stream().map(Long::parseLong).collect(Collectors.toList()); } /** * Integer类型集合转String类型集合 * @param list 集合 */ public static List toStringList(List list) { if(ListUtil.isNullOrEmpty(list)) { return new ArrayList<>(); } return list.stream().map(String::valueOf).collect(Collectors.toList()); } /** * BigDecimal类型集合求和 * @param list 集合 */ public static BigDecimal sum(Collection list) { BigDecimal result = BigDecimal.ZERO; if(ListUtil.isNotNullOrEmpty(list)) { result = list.stream().reduce(BigDecimal.ZERO, BigDecimal::add); } return result; } /** * 判断集合非空后,执行委托 * @param list 集合 * @param predicate 循环集合的委托代码 * @param 集合实体类型 * @param 函数(集合委托)返回值,委托里最后一句写死“return null;” */ public static void notNullForeach(Collection list, Function predicate) { if(ListUtil.isNotNullOrEmpty(list)) { for (T item : list) { predicate.apply(item); } } } /** * 集合去重 * @param list 集合 * @param 集合实体类型 */ public static void repeat(Collection list) { HashSet hash = new HashSet(list); list.clear(); list.addAll(hash); } /** * 判断集合是否存在重复元素 * @param list 集合 * @param 集合实体类型 */ public static Boolean hasRepeat(Collection list) { int oldSize = list.size(); repeat(list); return list.size()!=oldSize; } /** * 集合非空,则执行委托 * @param list 集合 * @param predicate 循环集合的委托代码 * @param 集合实体类型 */ public static void notNullForeach(Collection list, Consumer predicate) { if(ListUtil.isNotNullOrEmpty(list)) { for (T item : list) { predicate.accept(item); } } } }