@Override public void clear() { parent.clear(); }
@Override public boolean remove(final Object key) { final boolean result = parent.containsKey(key); parent.remove(key); return result; }
@Override public boolean contains(final Object value) { return parent.containsValue(value); }
/** * 创建性能与访问速度更优的微型Map * * 当Map的元素个数<=3时,直接使用key1,key2,key3属性,节约空间与加快速度. 当Map的元素个数>3时,自动创建真正的HashMap */ public static <K, V> Flat3Map<K, V> flat3Map() { return new Flat3Map<K, V>(); }
/** * Read the map in using a custom routine. * * @param in the input stream * @throws IOException if an error occurs while reading from the stream * @throws ClassNotFoundException if an object read from the stream can not be loaded */ @SuppressWarnings("unchecked") private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); final int count = in.readInt(); if (count > 3) { delegateMap = createDelegateMap(); } for (int i = count; i > 0; i--) { put((K) in.readObject(), (V) in.readObject()); } }
/** * Write the map out using a custom routine. * * @param out the output stream * @throws IOException if an error occurs while writing to the stream */ private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(size()); for (final MapIterator<?, ?> it = mapIterator(); it.hasNext();) { out.writeObject(it.next()); // key out.writeObject(it.getValue()); // value } }
/** * Puts all the values from the specified map into this map. * * @param map the map to add * @throws NullPointerException if the map is null */ @Override public void putAll(final Map<? extends K, ? extends V> map) { final int size = map.size(); if (size == 0) { return; } if (delegateMap != null) { delegateMap.putAll(map); return; } if (size < 4) { for (final Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { put(entry.getKey(), entry.getValue()); } } else { convertToMap(); delegateMap.putAll(map); } }
@Override public void remove() { if (canRemove == false) { throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID); } parent.remove(getKey()); nextIndex--; canRemove = false; }
/** * Constructor copying elements from another map. * * @param map the map to copy * @throws NullPointerException if the map is null */ public Flat3Map(final Map<? extends K, ? extends V> map) { super(); putAll(map); }
@Override public boolean contains(final Object key) { return parent.containsKey(key); }
/** * Converts the flat map data to a map. */ private void convertToMap() { delegateMap = createDelegateMap(); switch (size) { // drop through case 3: delegateMap.put(key3, value3); case 2: delegateMap.put(key2, value2); case 1: delegateMap.put(key1, value1); case 0: break; default: throw new IllegalStateException("Invalid map index: " + size); } size = 0; hash1 = hash2 = hash3 = 0; key1 = key2 = key3 = null; value1 = value2 = value3 = null; }
convertToMap(); delegateMap.put(key, value); return null;
/** * 创建性能与访问速度更优的微型Map * * 当Map的元素个数<=3时,直接使用key1,key2,key3属性,节约空间与加快速度. 当Map的元素个数>3时,自动创建真正的HashMap */ public static <K, V> Flat3Map<K, V> flat3Map() { return new Flat3Map<K, V>(); }
public void remove() { if (currentEntry == null) { throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID); } currentEntry.setRemoved(true); parent.remove(currentEntry.getKey()); nextIndex--; currentEntry = null; }
@Override public boolean remove(final Object obj) { if (obj instanceof Map.Entry == false) { return false; } final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj; final Object key = entry.getKey(); final boolean result = parent.containsKey(key); parent.remove(key); return result; }
@Override public void clear() { parent.clear(); }
@Override public void clear() { parent.clear(); }