View Javadoc
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    *      https://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  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  import java.util.Map;
24  
25  import org.apache.commons.collections4.MapIterator;
26  
27  /**
28   * A {@code Map} implementation that is a general purpose alternative
29   * to {@code HashMap}.
30   * <p>
31   * This implementation improves on the JDK1.4 HashMap by adding the
32   * {@link MapIterator MapIterator}
33   * functionality and many methods for subclassing.
34   * </p>
35   * <p>
36   * <strong>Note that HashedMap is not synchronized and is not thread-safe.</strong>
37   * If you wish to use this map from multiple threads concurrently, you must use
38   * appropriate synchronization. The simplest approach is to wrap this map
39   * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
40   * exceptions when accessed by concurrent threads without synchronization.
41   * </p>
42   *
43   * @param <K> The type of the keys in this map
44   * @param <V> The type of the values in this map
45   * @since 3.0
46   */
47  public class HashedMap<K, V>
48          extends AbstractHashedMap<K, V> implements Serializable, Cloneable {
49  
50      /** Serialization version */
51      private static final long serialVersionUID = -1788199231038721040L;
52  
53      /**
54       * Constructs a new empty map with default size and load factor.
55       */
56      public HashedMap() {
57          super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
58      }
59  
60      /**
61       * Constructs a new, empty map with the specified initial capacity.
62       *
63       * @param initialCapacity  The initial capacity
64       * @throws IllegalArgumentException if the initial capacity is negative
65       */
66      public HashedMap(final int initialCapacity) {
67          super(initialCapacity);
68      }
69  
70      /**
71       * Constructs a new, empty map with the specified initial capacity and
72       * load factor.
73       *
74       * @param initialCapacity  The initial capacity
75       * @param loadFactor  The load factor
76       * @throws IllegalArgumentException if the initial capacity is negative
77       * @throws IllegalArgumentException if the load factor is less than zero
78       */
79      public HashedMap(final int initialCapacity, final float loadFactor) {
80          super(initialCapacity, loadFactor);
81      }
82  
83      /**
84       * Constructor copying elements from another map.
85       *
86       * @param map  The map to copy
87       * @throws NullPointerException if the map is null
88       */
89      public HashedMap(final Map<? extends K, ? extends V> map) {
90          super(map);
91      }
92  
93      /**
94       * Clones the map without cloning the keys or values.
95       *
96       * @return A shallow clone
97       */
98      @Override
99      public HashedMap<K, V> clone() {
100         return (HashedMap<K, V>) super.clone();
101     }
102 
103     /**
104      * Deserializes the map in using a custom routine.
105      *
106      * @param in The input stream
107      * @throws IOException Thrown if an error occurs while reading from the stream
108      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
109      */
110     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
111         in.defaultReadObject();
112         doReadObject(in);
113     }
114 
115     /**
116      * Serializes this object to an ObjectOutputStream.
117      *
118      * @param out The target ObjectOutputStream.
119      * @throws IOException thrown when an I/O errors occur writing to the target stream.
120      */
121     private void writeObject(final ObjectOutputStream out) throws IOException {
122         out.defaultWriteObject();
123         doWriteObject(out);
124     }
125 
126 }