数据处理时写的几个小工具

最近在最数据操作时,写了几个关于HashMap的小工具。

/**
 * 对HashMap的封装;
 * Key是指定类型的值,<K> ;
 * Value是指定类型的ArrayList<V>;
 * K -> List<V>
 *
 * @param <K>
 * @param <V>
 */
public class ArrayHashMap<K, V> {
	
	private HashMap<K, ArrayList<V>> values = null ;
	
	public ArrayHashMap()
	{
		values = new HashMap<K, ArrayList<V>>() ;
	}
	
	public List<V> get(K key)
	{
		List<V> valuse = values.get(key);
		if(valuse == null)
			valuse = new ArrayList<V>();
		return valuse ;
	}
	
	public void put(K key, V value)
	{
		this.get(key).add(value);
	}
	
	public int size()
	{
		if(values == null)
			values = new HashMap<K, ArrayList<V>>() ;
		return values.size();
	}
}
 
第二给类:
/**
 * Three Dimension HashMap;
 * 根据两个K,来映射到一个值上面;
 * (K1,K2) -> V
 *
 */
public class ThreeDimHashMap<K1, K2, V> {
		
	private HashMap<String, V> values = null ;
	
	public ThreeDimHashMap()
	{
		values = new HashMap<String, V>() ;
	}
	
	public V get(K1 key1, K2 key2)
	{
		String key = getKey(key1, key2);
		return values.get(key);
	}
	
	public void put(K1 key1, K2 key2, V value)
	{
		String key = getKey(key1, key2);
		values.put(key, value);
	}

	private String getKey(K1 key1, K2 key2) {
		return new StringBuilder().append(key1).append("+").append(key2).toString();
	}

}
 
第三个类:
/**
 * (K1,K2) —> List<V>
 * @author shaohui.zs
 *
 */
public class ThreeDimArrayHashMap<K1,K2,V> {
	
	private Map<String,ArrayList<V>> values = null ;
	
	public ThreeDimArrayHashMap()
	{
		values = new HashMap<String, ArrayList<V>>();
	}
	
	public List<V> get(K1 key1, K2 key2)
	{
		String key = getKey (key1, key2);
		if(values == null)
			values = new HashMap<String, ArrayList<V>>(); 
		List<V> value = values.get(key);
		if(value == null)
			value = new ArrayList<V>();
		return value;
	}

	public void put(K1 key1, K2 key2, V value)
	{
		String key = getKey(key1, key2);
		this.get(key1, key2).add(value);
	}
	
	private String getKey(K1 key1, K2 key2) {
		return new StringBuilder().append(key1).append("+").append(key2).toString();
	}
}
 
第四个类:
/**
 * Four Dimnsion HashMap.
 * (K1,K2,K3) -> V
 *
 * @param <K1>
 * @param <K2>
 * @param <K3>
 * @param <V>
 */
public class FourDimHashMap<K1,K2,K3,V> {
	
	private HashMap<String, V> values = null ;
	
	public FourDimHashMap()
	{
		values = new HashMap<String, V>();
	}

	public V get(K1 key1, K2 key2, K3 key3)
	{
		String key = getKey(key1, key2, key3);
		return values.get(key);
	}
	
	public void put(K1 key1, K2 key2, K3 key3, V value)
	{
		values.put(getKey(key1, key2, key3), value);
	}

	private String getKey(K1 key1, K2 key2, K3 key3) {
		return new StringBuilder().append(key1).append("+").append(key2).append("+").append(key3).toString();
	}
}
 

猜你喜欢

转载自zhh1655.iteye.com/blog/1409387
今日推荐