📄 fasthashmap.java
字号:
return (map.isEmpty());
}
}
}
/**
* Return a set view of the keys contained in this map.
*/
public Set keySet() {
return new KeySet();
}
/**
* Associate the specified value with the specified key in this map.
* If the map previously contained a mapping for this key, the old
* value is replaced and returned.
*
* @param key The key with which the value is to be associated
* @param value The value to be associated with this key
*/
public Object put(Object key, Object value) {
if (fast) {
synchronized (this) {
HashMap temp = (HashMap) map.clone();
Object result = temp.put(key, value);
map = temp;
return (result);
}
} else {
synchronized (map) {
return (map.put(key, value));
}
}
}
/**
* Copy all of the mappings from the specified map to this one, replacing
* any mappings with the same keys.
*
* @param in Map whose mappings are to be copied
*/
public void putAll(Map in) {
if (fast) {
synchronized (this) {
HashMap temp = (HashMap) map.clone();
temp.putAll(in);
map = temp;
}
} else {
synchronized (map) {
map.putAll(in);
}
}
}
/**
* Remove any mapping for this key, and return any previously
* mapped value.
*
* @param key Key whose mapping is to be removed
*/
public Object remove(Object key) {
if (fast) {
synchronized (this) {
HashMap temp = (HashMap) map.clone();
Object result = temp.remove(key);
map = temp;
return (result);
}
} else {
synchronized (map) {
return (map.remove(key));
}
}
}
/**
* Return the number of key-value mappings in this map.
*/
public int size() {
if (fast) {
return (map.size());
} else {
synchronized (map) {
return (map.size());
}
}
}
/**
* Return a collection view of the values contained in this map.
*/
public Collection values() {
return new Values();
}
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) {
HashMap temp = (HashMap) map.clone();
get(temp).clear();
map = temp;
}
} 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;
}
}
}
}
private class KeySet extends CollectionView implements Set {
protected Collection get(Map map) {
return map.keySet();
}
protected Object iteratorNext(Map.Entry entry) {
return entry.getKey();
}
}
private class Values extends CollectionView {
protected Collection get(Map map) {
return map.values();
}
protected Object iteratorNext(Map.Entry entry) {
return entry.getValue();
}
}
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 + -