MapUtil.java 4.64 KB
package com.yoho.unions.utils;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.function.BiConsumer;

/**
 * map的工具类
 * @author MALI
 */
public class MapUtil {
	/**
	 * Iterator 转换成map
	 * @param values
	 * @param keyFunction
	 * @return
	 */
	public static final  <K, V> Map<K, V> transformMap(Iterable<V> values, Function<? super V, K> keyFunction) {
		if (Iterables.isEmpty(values) || null == keyFunction) {
			return Maps.newHashMap();
		}
		
		Map<K, V> builder = new HashMap<K, V>();
		Iterator<V> iterator = values.iterator();
	    while (iterator.hasNext()) {
	      V value = iterator.next();
	      builder.put(keyFunction.apply(value), value);
	    }
		return builder;
	}
	
	
	
	/**
	 *  Iterator 转换成<key,List>
	 * @param values 
	 * @param keyFunction
	 * @return
	 */
	public static final  <K, V> Map<K, List<V>> transformListMap(Iterable<V> values, Function<? super V, K> keyFunction) {
		if (Iterables.isEmpty(values) || null == keyFunction) {
			return Maps.newHashMap();
		}
		Map<K, List<V>> builder = new HashMap<K, List<V>>();
		Iterator<V> iterator = values.iterator();
	    while (iterator.hasNext()) 
	    {
	      V value = iterator.next();
	      if(builder.containsKey(keyFunction.apply(value)))
	      {
	    	  builder.get(keyFunction.apply(value)).add(value);
	      }else
	      {	
	    	  List<V> newArrayList=new ArrayList<V>();
	    	  newArrayList.add(value);
	    	  builder.put(keyFunction.apply(value), newArrayList);
	      }
	    }
		return builder;
	}
	
	/**
	 *  Iterator 转换成<key,List>
	 * @param values 
	 * @param keyFunction
	 * @return
	 */
	public static final  <V, T> Map<T, List<T>> transformListMap(Iterable<V> values, FunctionEx<V, T> keyFunction) {
		if (Iterables.isEmpty(values) || null == keyFunction) {
			return Maps.newHashMap();
		}
		Map<T, List<T>> builder = new HashMap<T, List<T>>();
		Iterator<V> iterator = values.iterator();
		while (iterator.hasNext()) 
		{
			V value = iterator.next();
			if(builder.containsKey(keyFunction.applyKey(value)))
			{
				builder.get(keyFunction.applyKey(value)).add(keyFunction.applyValue(value));
			}else
			{	
				List<T> newArrayList=new ArrayList<T>();
				newArrayList.add(keyFunction.applyValue(value));
				builder.put(keyFunction.applyKey(value), newArrayList);
			}
		}
		return builder;
	}
	
	/**
	 *  Iterator 转换成<key,List>
	 * @param values 
	 * @param keyFunction
	 * @return
	 */
	public static final  <V, T, K> Map<T, K> transformListMap(Iterable<V> values, FunctionExPlus<V, T, K> keyFunction) {
		if (Iterables.isEmpty(values) || null == keyFunction) {
			return Maps.newHashMap();
		}
		Map<T, K> builder = new HashMap<T, K>();
		Iterator<V> iterator = values.iterator();
		while (iterator.hasNext()) 
		{
			V value = iterator.next();
			builder.put(keyFunction.applyKey(value), keyFunction.applyValue(value));
		}
		return builder;
	}
	
	public static <T> List<T> getList(Map<Integer, List<T>> map)
	{
		if(MapUtils.isEmpty(map))
		{
			return Lists.newArrayList();
		}
		List<T> newArrayList = Lists.newArrayList();
		for (List<T> item : map.values()) {
			newArrayList.addAll(item);
		}
		return newArrayList;
	}
	
	/**
	 * 合并两个map,一个map的键值对应另一个map的值
	 * @param map1
	 * @param map2
	 * @return
	 */
	public static <K, T, V> Map<K, V> mergerTwoMap(Map<K, T> map1, Map<T, V> map2) {
		if (MapUtils.isEmpty(map1) || MapUtils.isEmpty(map2)) {
			return new HashMap<K, V>(0);
		}
		Map<K, V> result = new HashMap<K, V>(map1.size());
		for (Map.Entry<K, T> entry : map1.entrySet()) {
			result.put(entry.getKey(), map2.get(entry.getValue()));
		}
		return result;
	}
	
	public static interface Function<F, T> {
		T apply(F input);
	}
	
	public static interface FunctionEx<F, T> {
		T applyKey(F input);
		T applyValue(F input);
	}
	
	public static interface FunctionExPlus<F, T, K> {
		T applyKey(F input);
		K applyValue(F input);
	}

	/**
	 * 删除不满足条件的
	 */
	public static <K, V> void filterMapByValue(Map<K, V> map, Predicate predicateValue) {
		Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry entry=it.next();
			if (!predicateValue.evaluate(entry.getValue())) {
				it.remove();
			}
		}
	}

	public static <K, V> void filterStringBlankMap(Map<K, V> map) {
		filterMapByValue(map, o -> {
			if (o == null) {
				return false;
			}
			if (o instanceof String && StringUtils.isBlank((String)o)) {
				return false;
			}
			return true;
		});
	}

}