forked from nibnait/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataUtils.java
More file actions
161 lines (143 loc) · 4.95 KB
/
DataUtils.java
File metadata and controls
161 lines (143 loc) · 4.95 KB
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
package common.util;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Created by nibnait on 2021/08/18
*/
@Slf4j
public class DataUtils {
/**
* 分批调rpc接口 查询数据
* @param list 入参 eg: skuIdList
* @param size 一批的数量 eg: 100
* @param function 一个lambda表达式 eg: skuIdList -> itemsSkuService.batchQuery(skuIdList)
* @return 将每一次的返回值,用 flatMap 打平 返回一个 list
*/
public static <T, R> List<R> getDataPartition(List<T> list, Integer size, Function<List<T>, List<R>> function) {
return Lists.partition(list, size).stream()
.map(function)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
/**
* 返回 list1 - (list1 ∩ list2)
*/
public static <T> List<T> difference(Collection<T> list1, Collection<T> list2) {
return Lists.newArrayList(Sets.difference(Sets.newHashSet(list1), Sets.newHashSet(list2)));
}
/**
* 返回交集 list1 ∩ list2
*/
public static <T> List<T> intersection(Collection<T> list1, Collection<T> list2) {
return Lists.newArrayList(Sets.intersection(Sets.newHashSet(list1), Sets.newHashSet(list2)));
}
/**
* @param format abc{}e
* @param args d
* @return abcde
*/
public static String format(String format, Object... args) {
return MessageFormatter.arrayFormat(format, args).getMessage();
}
/**
* @param clazz 类型不可为抽象类/接口
*/
public static <T> T parseObject(String s, Class<T> clazz) {
if (StringUtils.isBlank(s) || "null".equalsIgnoreCase(s)) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
log.error(e.getMessage(), e);
return null;
}
}
try {
return JSON.parseObject(s, clazz);
} catch (Throwable e) {
log.error(e.getMessage(), e);
return null;
}
}
public static <T> List<T> parseArray(String s, Class<T> clazz) {
if (StringUtils.isBlank(s) || "null".equalsIgnoreCase(s)) {
return Lists.newArrayList();
}
return JSON.parseArray(s, clazz);
}
public static String toJsonStringObject(Object o) {
if (o == null) {
return JSON.toJSONString(Maps.newHashMap());
}
return JSON.toJSONString(o);
}
public static String toJsonStringArray(Object o) {
if (o == null) {
return JSON.toJSONString(Lists.newArrayList());
}
return JSON.toJSONString(o);
}
/**
* 将 element 加到 list 中
*/
public static <T> List<T> addToList(T element, List<T> list) {
list = CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
if (element != null) {
list.add(element);
return list.stream().distinct().collect(Collectors.toList());
}
return CollectionUtils.isEmpty(list) ? null : list;
}
/**
* 将 element 加到 list 中
*/
public static <T> List<T> addToList(List<T> element, List<T> list) {
list = CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
if (CollectionUtils.isNotEmpty(element)) {
list.addAll(element);
return list.stream().distinct().collect(Collectors.toList());
}
return CollectionUtils.isEmpty(list) ? null : list;
}
/**
* 将 element 从 list 中删除
*/
public static <T> List<T> removeElement(T element, List<T> list) {
if (CollectionUtils.isEmpty(list)) {
return list;
}
CopyOnWriteArrayList<T> cowList = new CopyOnWriteArrayList<>(list);
cowList.removeIf(t -> t.equals(element));
return cowList;
}
/**
* 返回 list中 重复的元素
*/
public static <T> List<T> getDuplicateElements(List<T> list) {
return list.stream()
.collect(Collectors.toMap(e -> e, e -> 1, Integer::sum))
.entrySet().stream()
.filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
/**
* 判断 list 中 是否包含 t
*/
public static <T> boolean contains(List<T> list, T t) {
return CollectionUtils.isNotEmpty(list) && list.contains(t);
}
}