📄 insertionsortedmap.java
字号:
package org.jahia.utils;import java.util.*;/** * <p>Title: Map implementation that respects the insertion order.</p> * <p>Description: This map implementation actually uses an ArrayList to * store the entry pairs.</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: Jahia Ltd</p> * @author Serge Huber * @version 1.0 * */public class InsertionSortedMap extends AbstractMap { private ArrayList internalList = new ArrayList(); private class Entry implements Map.Entry { private Object key; private Object value; public Entry(Object key, Object value) { this.key = key; this.value = value; } public boolean equals (Object o) { if (! (o instanceof Entry)) { return false; } Entry e2 = (Entry) o; if ( (this.getKey() == null ? e2.getKey() == null : this.getKey().equals(e2.getKey())) && (this.getValue() == null ? e2.getValue() == null : this.getValue().equals(e2.getValue()))) { return true; } else { return false; } } public int hashCode() { return ((this.getKey()==null ? 0 : this.getKey().hashCode()) ^ (this.getValue()==null ? 0 : this.getValue().hashCode())); } public Object getKey() { return key; } public Object getValue() { return value; } public Object setValue(Object value) { Object oldValue = this.value; this.value = value; return oldValue; } } public InsertionSortedMap() { } public InsertionSortedMap(Map t) { // we must now build the key order based on the map we were passed. Iterator sourceEntryIter = t.entrySet().iterator(); while (sourceEntryIter.hasNext()) { Map.Entry curEntry = (Map.Entry) sourceEntryIter.next(); internalList.add(curEntry.getKey()); } } public Set entrySet() { InsertionSortedSet insertionSortedSet = new InsertionSortedSet(); insertionSortedSet.setInternalList(internalList); return insertionSortedSet; } public Object put(Object key, Object value) throws UnsupportedOperationException , ClassCastException , IllegalArgumentException , NullPointerException { int pos = findKey(key); if (pos == -1) { Entry newEntry = new Entry(key, value); internalList.add(newEntry); return null; } else { Entry existingEntry = (Entry) internalList.get(pos); Object oldValue = existingEntry.getValue(); existingEntry.setValue(value); return oldValue; } } private int findKey(Object key) { int pos = -1; Iterator listIter = internalList.iterator(); while (listIter.hasNext()) { Map.Entry curEntry = (Map.Entry) listIter.next(); pos++; if (curEntry.getKey().equals(key)) { return pos; } } return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -