HashedMap.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.map;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.util.Map;

  23. /**
  24.  * A {@code Map} implementation that is a general purpose alternative
  25.  * to {@code HashMap}.
  26.  * <p>
  27.  * This implementation improves on the JDK1.4 HashMap by adding the
  28.  * {@link org.apache.commons.collections4.MapIterator MapIterator}
  29.  * functionality and many methods for subclassing.
  30.  * </p>
  31.  * <p>
  32.  * <strong>Note that HashedMap is not synchronized and is not thread-safe.</strong>
  33.  * If you wish to use this map from multiple threads concurrently, you must use
  34.  * appropriate synchronization. The simplest approach is to wrap this map
  35.  * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
  36.  * exceptions when accessed by concurrent threads without synchronization.
  37.  * </p>
  38.  *
  39.  * @param <K> the type of the keys in this map
  40.  * @param <V> the type of the values in this map
  41.  * @since 3.0
  42.  */
  43. public class HashedMap<K, V>
  44.         extends AbstractHashedMap<K, V> implements Serializable, Cloneable {

  45.     /** Serialization version */
  46.     private static final long serialVersionUID = -1788199231038721040L;

  47.     /**
  48.      * Constructs a new empty map with default size and load factor.
  49.      */
  50.     public HashedMap() {
  51.         super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
  52.     }

  53.     /**
  54.      * Constructs a new, empty map with the specified initial capacity.
  55.      *
  56.      * @param initialCapacity  the initial capacity
  57.      * @throws IllegalArgumentException if the initial capacity is negative
  58.      */
  59.     public HashedMap(final int initialCapacity) {
  60.         super(initialCapacity);
  61.     }

  62.     /**
  63.      * Constructs a new, empty map with the specified initial capacity and
  64.      * load factor.
  65.      *
  66.      * @param initialCapacity  the initial capacity
  67.      * @param loadFactor  the load factor
  68.      * @throws IllegalArgumentException if the initial capacity is negative
  69.      * @throws IllegalArgumentException if the load factor is less than zero
  70.      */
  71.     public HashedMap(final int initialCapacity, final float loadFactor) {
  72.         super(initialCapacity, loadFactor);
  73.     }

  74.     /**
  75.      * Constructor copying elements from another map.
  76.      *
  77.      * @param map  the map to copy
  78.      * @throws NullPointerException if the map is null
  79.      */
  80.     public HashedMap(final Map<? extends K, ? extends V> map) {
  81.         super(map);
  82.     }

  83.     /**
  84.      * Clones the map without cloning the keys or values.
  85.      *
  86.      * @return a shallow clone
  87.      */
  88.     @Override
  89.     public HashedMap<K, V> clone() {
  90.         return (HashedMap<K, V>) super.clone();
  91.     }

  92.     /**
  93.      * Deserializes the map in using a custom routine.
  94.      *
  95.      * @param in the input stream
  96.      * @throws IOException if an error occurs while reading from the stream
  97.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  98.      */
  99.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  100.         in.defaultReadObject();
  101.         doReadObject(in);
  102.     }

  103.     /**
  104.      * Serializes this object to an ObjectOutputStream.
  105.      *
  106.      * @param out the target ObjectOutputStream.
  107.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  108.      */
  109.     private void writeObject(final ObjectOutputStream out) throws IOException {
  110.         out.defaultWriteObject();
  111.         doWriteObject(out);
  112.     }

  113. }