📄 t2olist.java
字号:
package com.swing.game.crystal.utils;import java.util.*;/** * * <p>Title: Two key to one value mapping list</p> * <p>Description: If you want to map one value to two key, you should use this list, * you can not insert triple of which one of its key has already been * in this list. * </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author vampire * @version 1.0 */public class T2OList { private Map data = new HashMap(); private Map keyMapping = new HashMap(); public T2OList() { } public boolean put(Object key1, Object key2, Object o) { if (this.keyMapping.containsKey(key1)) { String value = (String)this.keyMapping.get(key1); // there has been the two key mapping to a value in list, just replace the value with the new value if (value.equals(key2)) { this.data.put(key1, o); return true; } else return false; } else if (this.keyMapping.containsKey(key2)) { String value = (String)this.keyMapping.get(key2); // there has been the two key mapping to a value in list, just replace the value with the new value if (value.equals(key1)) { this.data.put(key1, o); return true; } return false; } else { this.data.put(key1, o); this.keyMapping.put(key1, key2); return true; } } public Object get(Object key) { Object o = null; if (this.data.containsKey(key)) o = this.data.get(key); else { Object k = null; if (this.keyMapping.containsKey(key)) { k = this.keyMapping.get(key); if (this.data.containsKey(k)) o = this.data.get(k); } else if (this.keyMapping.containsValue(key)) { for (Iterator iter = this.keyMapping.keySet().iterator(); iter.hasNext(); ) { k = iter.next(); if (this.keyMapping.get(k).equals(key)) break; } o = this.data.get(k); } } return o; } public Object remove(Object key) { Object o = null; if (this.data.containsKey(key)) o = this.data.remove(key); else { Object k = null; if (this.keyMapping.containsKey(key)) { k = this.keyMapping.get(key); if (this.data.containsKey(k)) o = this.data.remove(k); } else if (this.keyMapping.containsValue(key)) { for (Iterator iter = this.keyMapping.keySet().iterator(); iter.hasNext(); ) { k = iter.next(); if (this.keyMapping.get(k).equals(key)) break; } o = this.data.remove(k); } } return o; } public boolean containsKey(Object key) { if (this.keyMapping.containsKey(key)) return true; else if (this.keyMapping.containsValue(key)) return true; else return false; } public boolean containsValue(Object value) { if (this.data.containsValue(value)) return true; else return false; } public int size() { return this.data.size(); } public Iterator values() { return this.data.values().iterator(); } public Iterator keys() { List l = new ArrayList(); Object key = null; for (Iterator iter = this.keyMapping.keySet().iterator(); iter.hasNext(); ) { key = iter.next(); l.add(key); l.add(this.keyMapping.get(key)); } return l.iterator(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -