简单组合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; } } }
相关推荐
java.util.HashMap<K,V> (implements java.lang.Cloneable, java.util.Map<K,V>, java.io.Serializable) java.util.LinkedHashMap<K,V> (implements java.util.Map<K,V>) org.springframework.core.annotation....
<p th:text="${hello}">dddd</p> </body> </html> --------------------------- 直接访问静态页面 --------------------------- http://localhost:8080/index.html 可直接访问到 src/main/resources/...
这是我在编写struts2中遇到的问题,整理出来,包括截图,希望可以帮到大家
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
可将List<Bean>导出成Excel,或读取Excel成List<Bean>、Map<String,Object>,很方便,可配置日期格式,可识别数字,经过企业级测试有效. <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</...
默认的降级服务需要自定义实现很多FallbackFactory,本模块针提供统一默认的降级服务,同时也支持自定义降级服务。
java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
ExcelUtil 将这些代码段添加到您的pom.xml ... < artifactId>excel-util</ artifactId> < version>master</ version> </ dependency> ... 示例-Excel文件 示例-ModelEntity @ExcelEntity public cla
boolean delete(java.lang.String sql, java.util.Map<java.lang.Integer,java.lang.Object> elements) 根据传入的参数删除单条记录的方法 boolean delete(java.lang.String sql, java.lang.Object[] elements)...
boolean delete(java.lang.String sql, java.util.Map<java.lang.Integer,java.lang.Object> elements) 根据传入的参数删除单条记录的方法 boolean delete(java.lang.String sql, java.lang.Object[] elements)...
java.util.ConcurrentModificationException 解决方法 在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除。 则使用会报以下异常: Java.util....
当forEach 的items属性中的表达式的值是java.util.Map时,则var中命名的变量的类型就是 java.util.Map.Entry。这时var=entry的话,用表达式${entry.key}取得键名。 用表达${entry.value}得到每个entry的值。这是...
import java.util.Map; //MapView Com import com.google.android.maps.MapView; import com.google.android.maps.MapController; import com.google.android.maps.GeoPoint; //import com.google.android.maps. ...
import java.util.Map; import java.util.Set; public class ChatRoomServer { private ServerSocket ss; private Map<String,Socket> onlineUsers; public ChatRoomServer(){ try { ss=new ...
sb.append("<p><blockquote><b>URI</b> = ").append( String.valueOf(session.getUri())).append("<br />"); sb.append("<b>Method</b> = ").append( String.valueOf(session.getMethod()))....
< java.util >Map接口 81 把map集合转成set的方法 82 使用集合的技巧 83 Collections--集合工具类 83 Arrays—数组对象工具类 84 增强for循环 85 可变参数(...) 86 枚举:关键字 enum 86 自动拆装箱 86 泛型 87 ...
import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; public class TestTreeMap { public static void main(String[] args) { TreeMap<Province, ArrayList<City>> map = new ...
运用下列类进行JAVA编程: Date Calendar Random 使用 Collection 接口及其实现类 ArrayList LinkedList 使用 HashMap 使用Vector 等方法的使用
<param name="class" value="java.util.Date"/> </create> <!-- 通过Spring反射对象 --> <create creator="spring" javascript="TestService"> <param name="beanName" value="testService" /> </create> ...
21.java.util.Date 22.java.sql.Date 23.java.sql.Time 24.java.sql.Timestamp 25.InetAddress 26.Inet4Address 27.Inet6Address 28.InetSocketAddress 1.使用xson进行对象到字节数组的序列化 User user ...