001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.map; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.io.Serializable; 023import java.util.Map; 024 025import org.apache.commons.collections4.MapIterator; 026 027/** 028 * A {@code Map} implementation that is a general purpose alternative 029 * to {@code HashMap}. 030 * <p> 031 * This implementation improves on the JDK1.4 HashMap by adding the 032 * {@link MapIterator MapIterator} 033 * functionality and many methods for subclassing. 034 * </p> 035 * <p> 036 * <strong>Note that HashedMap is not synchronized and is not thread-safe.</strong> 037 * If you wish to use this map from multiple threads concurrently, you must use 038 * appropriate synchronization. The simplest approach is to wrap this map 039 * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw 040 * exceptions when accessed by concurrent threads without synchronization. 041 * </p> 042 * 043 * @param <K> The type of the keys in this map 044 * @param <V> The type of the values in this map 045 * @since 3.0 046 */ 047public class HashedMap<K, V> 048 extends AbstractHashedMap<K, V> implements Serializable, Cloneable { 049 050 /** Serialization version */ 051 private static final long serialVersionUID = -1788199231038721040L; 052 053 /** 054 * Constructs a new empty map with default size and load factor. 055 */ 056 public HashedMap() { 057 super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD); 058 } 059 060 /** 061 * Constructs a new, empty map with the specified initial capacity. 062 * 063 * @param initialCapacity The initial capacity 064 * @throws IllegalArgumentException if the initial capacity is negative 065 */ 066 public HashedMap(final int initialCapacity) { 067 super(initialCapacity); 068 } 069 070 /** 071 * Constructs a new, empty map with the specified initial capacity and 072 * load factor. 073 * 074 * @param initialCapacity The initial capacity 075 * @param loadFactor The load factor 076 * @throws IllegalArgumentException if the initial capacity is negative 077 * @throws IllegalArgumentException if the load factor is less than zero 078 */ 079 public HashedMap(final int initialCapacity, final float loadFactor) { 080 super(initialCapacity, loadFactor); 081 } 082 083 /** 084 * Constructor copying elements from another map. 085 * 086 * @param map The map to copy 087 * @throws NullPointerException if the map is null 088 */ 089 public HashedMap(final Map<? extends K, ? extends V> map) { 090 super(map); 091 } 092 093 /** 094 * Clones the map without cloning the keys or values. 095 * 096 * @return A shallow clone 097 */ 098 @Override 099 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}