liulin
2024-07-29 b65029f9d6e351a714682c110343a8c23eb4e793
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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 <T> 实体类型
     */
    public static <T> List<T> 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<Long> 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<Integer> 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<BigDecimal> 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<Double> 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<String> list) {
        if(ListUtil.isNullOrEmpty(list)) {
            return new String[0];
        }
        String[] array = new String[list.size()];
        return list.toArray(array);
    }
 
    /**
     * 判断集合【为】null或empty
     * @param list 集合
     * @param <T> 集合实体类型
     */
    public static <T> boolean isNullOrEmpty(Collection<T> list){
        return list==null || list.isEmpty();
    }
 
    /**
     * 判断集合【不为】null或empty
     * @param list 集合
     * @param <T> 集合实体类型
     */
    public static <T> boolean isNotNullOrEmpty(Collection<T> list){
        return !ListUtil.isNullOrEmpty(list);
    }
 
    /**
     * String类型集合转Integer类型集合
     * @param list 集合
     */
    public static List<Integer> toIntList(List<String> list) {
        if(ListUtil.isNullOrEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(Integer::parseInt).collect(Collectors.toList());
    }
 
    /**
     * String类型集合转Long类型集合
     * @param list 集合
     */
    public static List<Long> toLongList(List<String> list) {
        if(ListUtil.isNullOrEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(Long::parseLong).collect(Collectors.toList());
    }
 
    /**
     * Integer类型集合转String类型集合
     * @param list 集合
     */
    public static List<String> toStringList(List<Integer> 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<BigDecimal> list) {
        BigDecimal result = BigDecimal.ZERO;
        if(ListUtil.isNotNullOrEmpty(list)) {
            result = list.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return result;
    }
 
    /**
     * 判断集合非空后,执行委托
     * @param list 集合
     * @param predicate 循环集合的委托代码
     * @param <T> 集合实体类型
     * @param <R> 函数(集合委托)返回值,委托里最后一句写死“return null;”
     */
    public static <T, R> void notNullForeach(Collection<T> list, Function<T, R> predicate) {
        if(ListUtil.isNotNullOrEmpty(list)) {
            for (T item : list) {
                predicate.apply(item);
            }
        }
    }
 
    /**
     * 集合去重
     * @param list 集合
     * @param <T> 集合实体类型
     */
    public static <T> void repeat(Collection<T> list) {
        HashSet<T> hash = new HashSet<T>(list);
        list.clear();
        list.addAll(hash);
    }
 
    /**
     * 判断集合是否存在重复元素
     * @param list 集合
     * @param <T> 集合实体类型
     */
    public static <T> Boolean hasRepeat(Collection<T> list) {
        int oldSize = list.size();
        repeat(list);
        return list.size()!=oldSize;
    }
 
    /**
     * 集合非空,则执行委托
     * @param list 集合
     * @param predicate 循环集合的委托代码
     * @param <T> 集合实体类型
     */
    public static <T> void notNullForeach(Collection<T> list, Consumer<T> predicate) {
        if(ListUtil.isNotNullOrEmpty(list)) {
            for (T item : list) {
                predicate.accept(item);
            }
        }
    }
}