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