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