All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flyfish.oauth.utils.ContainUtils Maven / Gradle / Ivy

/**
 * ContainUtils.java com.bibenet.cedp.utils.service
 * 

* **************************************************************************** * Change Log *

* 1. wangyu create me at 2016年11月25日11:56 for class DESC. 2. * **************************************************************************** * Copyright (c) 2016, www.bibenet.com All Rights Reserved.O(∩_∩)O */ package com.flyfish.oauth.utils; import com.flyfish.oauth.common.Function; import java.util.*; /** * @author wangyu * @name 包含工具类 *

* 描述:含有键值存储和包含关系的便捷查询 * 用例: * 1.从一个Map里取出包含多个key的值或排除多个key的值 * 2.将Collection实例化为List或Set * 3.从Map里快速去除一系列值 * 4.从Map里快速取出需要值并将值写入新的Map */ public class ContainUtils { /** * 通过源Map将所有key对应的值写入目标Map内 * * @param map * @param keys * @param source * @param * @param * @return 目标Map */ public static Map putAllWithKeys(Map map, Collection keys, Map source) { map.putAll(mapWithKeys(source, keys)); return map; } /** * 通过function转换标识 * * @param list 列表 * @param function 方法 * @param 泛型 * @return 结果 */ public static List getIdentifiers(List list, Function function) { List identifiers = new ArrayList<>(); for (T data : list) { Object identifier = function.apply(data); if (null != identifier) { identifiers.add(identifier); } } return identifiers; } /** * 排除不同的keys剩下的值 * * @param map * @param keys * @param * @param * @return */ public static List listWithoutKeys(Map map, Collection keys) { Collection originKeys = map.keySet(); originKeys.removeAll(keys); return listWithKeys(map, originKeys); } /** * 排除相同的keys的集合 * * @param map * @param keys * @param * @param * @return */ public static List listWithKeys(Map map, Collection keys) { List intersected = new ArrayList<>(); for (E key : keys) { if (map.containsKey(key)) { intersected.add(map.get(key)); } } return intersected; } /** * 排除不同的keys剩下的值 * * @param map * @param keys * @param * @param * @return */ public static Map mapWithoutKeys(Map map, Collection keys) { Collection originKeys = map.keySet(); originKeys.removeAll(keys); return mapWithKeys(map, originKeys); } /** * 排除相同的keys的集合 * * @param map * @param keys * @param * @param * @return */ public static Map mapWithKeys(Map map, Collection keys) { Map intersectedMap = new HashMap<>(); for (E key : keys) { if (map.containsKey(key)) { intersectedMap.put(key, map.get(key)); } } return intersectedMap; } /** * 通过指定的key批量删除值 * * @param map * @param keys * @param * @param * @return */ public static Map removeByKeys(Map map, Collection keys) { for (E key : keys) { if (map.containsKey(key)) { map.remove(key); } } return map; } /** * 将key和value组成的list转化为map(zipObject) * 转换时不保证有序 * !!但是如果传入的是list,则必然有序!! * * @param keys * @param values * @param * @param * @return */ public static Map listsToMap(Collection keys, Collection values) { Map map = new HashMap<>(); if (null == keys || null == values) { return map; } Iterator keyIterator = keys.iterator(); Iterator valueIterator = values.iterator(); while (keyIterator.hasNext() && valueIterator.hasNext()) { map.put(keyIterator.next(), valueIterator.next()); } return map; } }