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 createMap(Object ... args){ Assert.notNull(args, "参数不能为null"); Assert.isTrue(args.length % 2 == 0, "length必须为偶数"); Map 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> listOne, List> 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> listOne, List> listTwo, String oneKey, String twoKey) { boolean modified = false; for (Map mapOne : listOne) { for (Map mapTwo : listTwo) { if(mapOne.get(oneKey).toString().equals(mapTwo.get(twoKey).toString())) { putCouponInfo(mapOne, mapTwo); modified = true; } } } return modified; } private static void putCouponInfo(Map couponMap, Map couponInfoMap) { /** key命名为 type + "_" + coupon_order */ couponMap.put(couponInfoMap.get("type") + "_" + couponInfoMap.get("coupon_order"), couponInfoMap.get("type_used_count")); } public static List asList(String ids) { return (List) asList(ids, String.class); } public static List asList(String ids, Class clazz) { Assert.notNull(ids); Assert.notNull(clazz); List list = new ArrayList<>(); String[] id = ids.split(","); for (String s : id) { list.add((T) s); } return list; } }