简单组合java.util.Map<K,V>实现Map<K,P,V>

本文介绍了一种基于Java的双键单值Map实现方法,通过扩展java.util.Map<K,V>来创建Map<K,P,V>,适用于需要使用复合键值对的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单组合java.util.Map<K,V>实现Map<K,P,V>

 

java.util.Map<K,V>为单键对单值,有时需要双键对单值,因此基于Map<K,V>可以简单实现一个Map<K,P,V>。

 

接口定义:

package cc.lixiaohui.demo.javassist.proxy.util;

import java.util.Collection;
import java.util.Set;

/**
 * 两个键的复合map
 * <pre>
 * key------+
 *          |-->value
 * param----+
 * <pre>
 * 
 * @author lixiaohui
 * @date 2016年10月1日 上午10:58:40
 * 
 */
public interface CompoundKeyMap<K, P, V> {
	
	V get(K key, P param);
	V get(K key, P param, V defValue);
	
	V put(K key, P param, V value);
	V putIfAbsent(K key, P param, V value);
	
	Set<java.util.Map.Entry<CompoundKey<K, P>, V>> entrySet();
	Set<CompoundKey<K, P>> keys();
	Collection<V> values();
	
	int size();
	boolean isEmpty();
	
	public interface CompoundKey<K, P> {
		K getKey();
		P getParam();
	}
	
}

 

基于HashMap的简单实现,关键在于CompoundKey的hashcode和equals方法的重写:

 

package cc.lixiaohui.demo.javassist.proxy.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

/**
 * 基于{@link java.util.HashMap}的CompoundKeyMap的实现.
 * 
 * @author lixiaohui
 * @date 2016年10月1日 下午12:37:08
 * 
 */
public class CompoundKeyHashMap<K, P, V> implements CompoundKeyMap<K, P, V> {

	private Map<CompoundKey<K, P>, V> map = new HashMap<CompoundKey<K, P>, V>();
	
	
	public V get(K key, P param) {
		key = Objects.requireNonNull(key, "key cannot be null");
		param = Objects.requireNonNull(param, "param cannot be null");
		
		return map.get(newKey(key, param));
	}

	private CompoundKeyMap.CompoundKey<K, P> newKey(K key, P param) {
		return new CompoundKeyImpl<K, P>(key, param);
	}

	public V get(K key, P param, V defValue) {
		key = Objects.requireNonNull(key, "key cannot be null");
		param = Objects.requireNonNull(param, "param cannot be null");
		
		V value = get(key, param);
		return value == null ? defValue : value;
	}

	public V put(K key, P param, V value) {
		return map.put(newKey(key, param), value);
	}

	public V putIfAbsent(K key, P param, V value) {
		return map.putIfAbsent(newKey(key, param), value);
	}

	public Set<Entry<CompoundKeyMap.CompoundKey<K, P>, V>> entrySet() {
		return map.entrySet();
	}

	public Set<CompoundKeyMap.CompoundKey<K, P>> keys() {
		return map.keySet();
	}

	public Collection<V> values() {
		return map.values();
	}

	public int size() {
		return map.size();
	}

	public boolean isEmpty() {
		return map.isEmpty();
	}

	static class CompoundKeyImpl<K, P> implements CompoundKey<K, P> {

		private K key;
		
		private P param;
		
		CompoundKeyImpl(K key, P param) {
			super();
			this.key = key;
			this.param = param;
		}

		public K getKey() {
			return key;
		}

		public P getParam() {
			return param;
		}
		
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((key == null) ? 0 : key.hashCode());
			result = prime * result + ((param == null) ? 0 : param.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			CompoundKeyImpl<?, ?> other = (CompoundKeyImpl<?, ?>) obj;
			if (key == null) {
				if (other.key != null)
					return false;
			} else if (!key.equals(other.key))
				return false;
			if (param == null) {
				if (other.param != null)
					return false;
			} else if (!param.equals(other.param))
				return false;
			return true;
		}
	}
	
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值