CollectionUtil.java 2.63 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
package com.lyms.etl.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author: litao
* @Date: 2017/5/22 0022 10:44
* @Version: V1.0
*/
public class CollectionUtil {
private CollectionUtil(){}

public static Map<String,Object> createMap(Object ... args){
Assert.notNull(args, "参数不能为null");
Assert.isTrue(args.length % 2 == 0, "length必须为偶数");

Map<String,Object> map = new HashMap<>();
for (int i = 0; i < args.length; i++) {
String key = args[i++].toString();
Object value = args[i];
if(value != null && StringUtils.isNotBlank(value.toString())) {
map.put(key, value);
}
}
return map;
}

/**
* 合并两个集合中 key 相同的数据 为一行
* 以第一个集合为主
* @param listOne
* @param listTwo
* @param oneKey
* @param twoKey
* @return 是否修改了
*/
public static boolean putAll(List<Map<String, Object>> listOne, List<Map<String, Object>> listTwo, String oneKey, String twoKey) {
Assert.notNull(listOne);
Assert.notNull(listTwo);
Assert.notNull(oneKey);
Assert.notNull(twoKey);
return getIntersect(listOne, listTwo, oneKey, twoKey);
}

private static boolean getIntersect(List<Map<String, Object>> listOne, List<Map<String, Object>> listTwo, String oneKey, String twoKey) {
boolean modified = false;
for (Map<String, Object> mapOne : listOne) {
for (Map<String, Object> mapTwo : listTwo) {
if(mapOne.get(oneKey).toString().equals(mapTwo.get(twoKey).toString())) {
putCouponInfo(mapOne, mapTwo);
modified = true;
}
}
}
return modified;
}

private static void putCouponInfo(Map<String, Object> couponMap, Map<String, Object> couponInfoMap) {
/** key命名为 type + "_" + coupon_order */
couponMap.put(couponInfoMap.get("type") + "_" + couponInfoMap.get("coupon_order"), couponInfoMap.get("type_used_count"));
}


public static <T> List<T> asList(String ids) {
return (List<T>) asList(ids, String.class);
}

public static <T> List<T> asList(String ids, Class<T> clazz) {
Assert.notNull(ids);
Assert.notNull(clazz);

List<T> list = new ArrayList<>();
String[] id = ids.split(",");
for (String s : id) {
list.add((T) s);
}
return list;
}

}