1.刷新map集合
public static <V> void flushMap(String key, Map<String, List<V>> map, V v) {
List<V> vs = map.containsKey(key) ? map.get(key) : new ArrayList<V>();
vs.add(v);
map.put(key, vs);
}
2.集合与集合的转换
public static <O, T> List<T> list2list(List<O> os, Function<O, ? extends T> tartget) {
List<T> ts = new ArrayList<T>();
if (null == os || os.size() == 0) {
return ts;
}
os.forEach(o -> {
T apply = tartget.apply(o);
if (null != apply) {
ts.add(apply);
}
});
return ts;
}
3.map与map的转换
public static <A, B, C, D> Map<A, B> map2map(Map<C, D> sorceMap, Function<C, A> keyFunction, Function<D, B> valueFunction) {
Map<A, B> targetMap = new HashMap<>();
if (null == sorceMap || sorceMap.isEmpty()) {
return targetMap;
}
sorceMap.forEach((skey, svalue) -> {
if (isNotNull(svalue)) {
targetMap.put(keyFunction.apply(skey), valueFunction.apply(svalue));
}
});
return targetMap;
}
public static <A, C, D> Map<A, D> map2mapKey(Map<C, D> sorceMap, Function<C, A> keyFunction) {
return map2map(sorceMap, keyFunction, v -> v);
}
public static <A, B, D> Map<A, B> map2mapValue(Map<A, D> sorceMap, Function<D, B> valueFunction) {
return map2map(sorceMap, k -> k, v -> valueFunction.apply(v));
}
4.数组转集合
public static <O, T> List<T> array2list(O[] os, Function<O, ? extends T> tartget) {
List<T> ts = new ArrayList<T>();
if (null == os || os.length == 0) {
return ts;
}
for (O o : os) {
T apply = tartget.apply(o);
if (null != apply) {
ts.add(apply);
}
}
return ts;
}
public static <O> List<O> array2list(O[] os) {
return array2list(os, o -> o);
}
5.集合转数组
public static <T> T[] list2Arr(List<T> ts) {
if (null == ts || ts.size() == 0) {
return null;
}
Class<? extends Object> class1 = ts.get(0).getClass();
T[] tarr = (T[]) Array.newInstance(class1, ts.size());
for (int i = 0; i < ts.size(); i++) {
tarr[i] = ts.get(i);
}
return tarr;
}
6.list分组
public static <T, K> Map<K, List<T>> groupList(List<T> ts, Function<? super T, ? extends K> classifier) {
return ts.stream().filter(t -> DrinStringUtils.isNotNull(classifier.apply(t))).collect(Collectors.groupingBy(classifier));
}
7.list转k-model
public static <T> Map<String, T> groupModel(List<T> ts, Function<? super T, String> keyFun) {
Map<String, T> map = new HashMap<String, T>();
if (null == ts || ts.size() == 0) {
return map;
}
ts.forEach(t -> {
String key = keyFun.apply(t);
if (isNotNull(key) && !map.containsKey(key)) {
map.put(key, t);
}
});
return map;
}
8.收集list的某2个string字段,k-xxx
public static <T, K> Map<String, String> groupField(List<T> ts, Function<? super T, String> keyfun, Function<? super T, String> fieldFun) {
Map<String, String> map = new HashMap<String, String>();
ts.forEach(t -> {
map.put(keyfun.apply(t), fieldFun.apply(t));
});
return map;
}
public static <T, K> Map<String, Long> groupFieldLong(List<T> ts, Function<? super T, String> keyfun, Function<? super T, Long> fieldFun) {
Map<String, Long> map = new HashMap<String, Long>();
ts.forEach(t -> {
map.put(keyfun.apply(t), fieldFun.apply(t));
});
return map;
}
public static <T, K> Map<String, Integer> groupFieldInt(List<T> ts, Function<? super T, String> keyfun, Function<? super T, Integer> fieldFun) {
Map<String, Integer> map = new HashMap<String, Integer>();
ts.forEach(t -> {
map.put(keyfun.apply(t), fieldFun.apply(t));
});
return map;
}
public static <T, K> Map<String, List<String>> groupFields(List<T> ts, Function<? super T, String> keyfun, Function<? super T, String> fieldFun) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (T t : ts) {
String key = keyfun.apply(t);
List<String> list = map.containsKey(key) ? map.get(key) : new ArrayList<>();
list.add(fieldFun.apply(t));
map.put(key, list);
}
return map;
}
9.收集list的某一个字段,如ids
public static <T> List<String> collectField(List<T> ts, Function<? super T, String> keyfun) {
if (null == ts || ts.size() == 0) {
return New.newArrayList();
}
List<String> strs = new ArrayList<>();
for (T t : ts) {
String apply = keyfun.apply(t);
if (isNotNull(apply)) {
strs.add(apply);
}
}
return strs;
}
10.等分list
public static <T> List<List<T>> subList2SameCount(List<T> list, int num) {
int size = list.size();
int fortimes = size / num;
int more = size % num;
fortimes = more > 0 ? ++fortimes : fortimes;
int count = 0;
List<List<T>> lists = new ArrayList<List<T>>();
Iterator<T> iterator = list.iterator();
List<T> ts = null;
for (int i = 0; i < fortimes; i++) {
ts = new ArrayList<T>();
while (iterator.hasNext()) {
if (count > size || ts.size() == num) {
break;
}
ts.add(iterator.next());
iterator.remove();
count++;
}
lists.add(ts);
}
return lists;
}
11.字符串转集合
public static List<String> str2List(String str, String reg) {
if (isNull(str)) {
return new ArrayList<>();
}
String[] split = str.split(reg);
List<String> list = new ArrayList<String>();
for (String string : split) {
if (isNotNull(string) && !list.contains(string)) {
list.add(string);
}
}
return list;
}
12.排序
public static <T> void sortLong(List<T> ts, Function<T, Long> getSort, boolean isDesc) {
ts.sort((t1, t2) -> {
Long s1 = getSort.apply(t1);
Long s2 = getSort.apply(t2);
if (isDesc) {
s1 = null == s1 ? Long.MIN_VALUE : s1;
s2 = null == s2 ? Long.MIN_VALUE : s2;
return s2.compareTo(s1);
} else {
s1 = null == s1 ? Long.MAX_VALUE : s1;
s2 = null == s2 ? Long.MAX_VALUE : s2;
return s1.compareTo(s2);
}
});
}
public static <T> void sortInteger(List<T> ts, Function<T, Integer> getSort, boolean isDesc) {
ts.sort((t1, t2) -> {
Integer s1 = getSort.apply(t1);
Integer s2 = getSort.apply(t2);
if (isDesc) {
s1 = null == s1 ? Integer.MIN_VALUE : s1;
s2 = null == s2 ? Integer.MIN_VALUE : s2;
return s2.compareTo(s1);
} else {
s1 = null == s1 ? Integer.MAX_VALUE : s1;
s2 = null == s2 ? Integer.MAX_VALUE : s2;
return s1.compareTo(s2);
}
});
}
13.防止dao in ids过多的静态方法
public static <T> List<T> getByIds(List<String> ids, Function<List<String>, List<T>> findTs) {
List<T> ts = new ArrayList<>();
if (null == ids || ids.size() == 0) {
return ts;
}
// 去个重
List<String> distinctIds = new ArrayList<>();
for (String id : ids) {
if (DrinStringUtils.isNotNull(id) && !distinctIds.contains(id)) {
distinctIds.add(id);
}
}
// 按50个id一组进行查询
if (distinctIds.size() <= 50) {
return findTs.apply(distinctIds);
}
List<List<String>> idLists = DrinListUtils.subList2SameCount(distinctIds, 50);
for (List<String> idList : idLists) {
if (null != idList && idList.size() > 0) {
List<T> subTs = findTs.apply(idList);
if (null != subTs && subTs.size() > 0) {
ts.addAll(subTs);
}
}
}
return ts;
}
统计对象的某个字段
//单个的情况
public static <T> Map<String, Integer> countField(List<T> ts,Function<T,String> fieldValueFun){
Map<String, Integer> countMap=new HashMap<>();
if(null==ts||ts.size()==0){
return countMap;
}
ts.forEach(t->{
if(null!=t){
String fieldValue = fieldValueFun.apply(t);
if(DrinStringUtils.isNotNull(fieldValue)) {
Integer count = countMap.get(fieldValue);
countMap.put(fieldValue, null==count?1:count+1);
}
}
});
return countMap;
}
//集合的情况
public static <T> Map<String, Integer> countFields(List<T> ts, Function<T, List<String>> fieldValueFun) {
Map<String, Integer> countMap=new HashMap<>();
if(null==ts||ts.size()==0){
return countMap;
}
ts.forEach(t -> {
toCount(fieldValueFun, countMap, t);
});
return countMap;
}
private static <T> void toCount(Function<T, List<String>> fieldValueFun, Map<String, Integer> countMap, T t) {
if(null==t){
return;
}
List<String> fieldValues = fieldValueFun.apply(t);
if (null != fieldValues && fieldValues.size() > 0) {
fieldValues.forEach(fieldValue -> {
if (DrinStringUtils.isNotNull(fieldValue)) {
Integer count = countMap.get(fieldValue);
countMap.put(fieldValue, null == count ? 1 : count + 1);
}
});
}
}
//示例旧代码:
Map<String,Integer> countMap=new HashMap<>();
for (GroupCountStatement statement : statements) {
List<String> firstReflecterTypes = statement.getFirstReflecterTypes();
if(null!=firstReflecterTypes&&firstReflecterTypes.size()>0) {
for (String type : firstReflecterTypes) {
if(DrinStringUtils.isNotNull(type)) {
Integer integer = countMap.get(type);
countMap.put(type, null!=integer?integer+1:1);
}
}
}
}
//提取方法后的代码:
Map<String, Integer> countType = ListUtils.countFields(this.statements, s->s.getFirstReflecterTypes());