📄 fasthashmap.java
字号:
} else { synchronized (map) { if (mo.size() != map.size()) { return (false); } Iterator i = map.entrySet().iterator(); while (i.hasNext()) { Map.Entry e = (Map.Entry) i.next(); Object key = e.getKey(); Object value = e.getValue(); if (value == null) { if (!(mo.get(key) == null && mo.containsKey(key))) { return (false); } } else { if (!value.equals(mo.get(key))) { return (false); } } } return (true); } } } /** * Return the hash code value for this map. This implementation uses * exactly the code that is used to define the list hash function in the * documentation for the <code>Map.hashCode</code> method. * * @return suitable integer hash code */ public int hashCode() { if (fast) { int h = 0; Iterator i = map.entrySet().iterator(); while (i.hasNext()) { h += i.next().hashCode(); } return (h); } else { synchronized (map) { int h = 0; Iterator i = map.entrySet().iterator(); while (i.hasNext()) { h += i.next().hashCode(); } return (h); } } } /** * Return a shallow copy of this <code>FastHashMap</code> instance. * The keys and values themselves are not copied. * * @return a clone of this map */ public Object clone() { FastHashMap results = null; if (fast) { results = new FastHashMap(map); } else { synchronized (map) { results = new FastHashMap(map); } } results.setFast(getFast()); return (results); } // Map views // ---------------------------------------------------------------------- /** * Return a collection view of the mappings contained in this map. Each * element in the returned collection is a <code>Map.Entry</code>. */ public Set entrySet() { return new EntrySet(); } /** * Return a set view of the keys contained in this map. */ public Set keySet() { return new KeySet(); } /** * Return a collection view of the values contained in this map. */ public Collection values() { return new Values(); } // Map view inner classes // ---------------------------------------------------------------------- /** * Abstract collection implementation shared by keySet(), values() and entrySet(). */ private abstract class CollectionView implements Collection { public CollectionView() { } protected abstract Collection get(Map map); protected abstract Object iteratorNext(Map.Entry entry); public void clear() { if (fast) { synchronized (FastHashMap.this) { map = new HashMap(); } } else { synchronized (map) { get(map).clear(); } } } public boolean remove(Object o) { if (fast) { synchronized (FastHashMap.this) { HashMap temp = (HashMap) map.clone(); boolean r = get(temp).remove(o); map = temp; return r; } } else { synchronized (map) { return get(map).remove(o); } } } public boolean removeAll(Collection o) { if (fast) { synchronized (FastHashMap.this) { HashMap temp = (HashMap) map.clone(); boolean r = get(temp).removeAll(o); map = temp; return r; } } else { synchronized (map) { return get(map).removeAll(o); } } } public boolean retainAll(Collection o) { if (fast) { synchronized (FastHashMap.this) { HashMap temp = (HashMap) map.clone(); boolean r = get(temp).retainAll(o); map = temp; return r; } } else { synchronized (map) { return get(map).retainAll(o); } } } public int size() { if (fast) { return get(map).size(); } else { synchronized (map) { return get(map).size(); } } } public boolean isEmpty() { if (fast) { return get(map).isEmpty(); } else { synchronized (map) { return get(map).isEmpty(); } } } public boolean contains(Object o) { if (fast) { return get(map).contains(o); } else { synchronized (map) { return get(map).contains(o); } } } public boolean containsAll(Collection o) { if (fast) { return get(map).containsAll(o); } else { synchronized (map) { return get(map).containsAll(o); } } } public Object[] toArray(Object[] o) { if (fast) { return get(map).toArray(o); } else { synchronized (map) { return get(map).toArray(o); } } } public Object[] toArray() { if (fast) { return get(map).toArray(); } else { synchronized (map) { return get(map).toArray(); } } } public boolean equals(Object o) { if (o == this) return true; if (fast) { return get(map).equals(o); } else { synchronized (map) { return get(map).equals(o); } } } public int hashCode() { if (fast) { return get(map).hashCode(); } else { synchronized (map) { return get(map).hashCode(); } } } public boolean add(Object o) { throw new UnsupportedOperationException(); } public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } public Iterator iterator() { return new CollectionViewIterator(); } private class CollectionViewIterator implements Iterator { private Map expected; private Map.Entry lastReturned = null; private Iterator iterator; public CollectionViewIterator() { this.expected = map; this.iterator = expected.entrySet().iterator(); } public boolean hasNext() { if (expected != map) { throw new ConcurrentModificationException(); } return iterator.hasNext(); } public Object next() { if (expected != map) { throw new ConcurrentModificationException(); } lastReturned = (Map.Entry)iterator.next(); return iteratorNext(lastReturned); } public void remove() { if (lastReturned == null) { throw new IllegalStateException(); } if (fast) { synchronized (FastHashMap.this) { if (expected != map) { throw new ConcurrentModificationException(); } FastHashMap.this.remove(lastReturned.getKey()); lastReturned = null; expected = map; } } else { iterator.remove(); lastReturned = null; } } } } /** * Set implementation over the keys of the FastHashMap */ private class KeySet extends CollectionView implements Set { protected Collection get(Map map) { return map.keySet(); } protected Object iteratorNext(Map.Entry entry) { return entry.getKey(); } } /** * Collection implementation over the values of the FastHashMap */ private class Values extends CollectionView { protected Collection get(Map map) { return map.values(); } protected Object iteratorNext(Map.Entry entry) { return entry.getValue(); } } /** * Set implementation over the entries of the FastHashMap */ private class EntrySet extends CollectionView implements Set { protected Collection get(Map map) { return map.entrySet(); } protected Object iteratorNext(Map.Entry entry) { return entry; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -