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 * http://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 */ 017 package org.apache.commons.collections.map; 018 019 import java.io.IOException; 020 import java.io.ObjectInputStream; 021 import java.io.ObjectOutputStream; 022 import java.io.Serializable; 023 import java.util.Map; 024 025 /** 026 * A <code>Map</code> implementation that is a general purpose alternative 027 * to <code>HashMap</code>. 028 * <p> 029 * This implementation improves on the JDK1.4 HashMap by adding the 030 * {@link org.apache.commons.collections.MapIterator MapIterator} 031 * functionality and many methods for subclassing. 032 * <p> 033 * <strong>Note that HashedMap is not synchronized and is not thread-safe.</strong> 034 * If you wish to use this map from multiple threads concurrently, you must use 035 * appropriate synchronization. The simplest approach is to wrap this map 036 * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw 037 * exceptions when accessed by concurrent threads without synchronization. 038 * 039 * @since Commons Collections 3.0 040 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 041 * 042 * @author Stephen Colebourne 043 */ 044 public class HashedMap 045 extends AbstractHashedMap implements Serializable, Cloneable { 046 047 /** Serialisation version */ 048 private static final long serialVersionUID = -1788199231038721040L; 049 050 /** 051 * Constructs a new empty map with default size and load factor. 052 */ 053 public HashedMap() { 054 super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD); 055 } 056 057 /** 058 * Constructs a new, empty map with the specified initial capacity. 059 * 060 * @param initialCapacity the initial capacity 061 * @throws IllegalArgumentException if the initial capacity is less than one 062 */ 063 public HashedMap(int initialCapacity) { 064 super(initialCapacity); 065 } 066 067 /** 068 * Constructs a new, empty map with the specified initial capacity and 069 * load factor. 070 * 071 * @param initialCapacity the initial capacity 072 * @param loadFactor the load factor 073 * @throws IllegalArgumentException if the initial capacity is less than one 074 * @throws IllegalArgumentException if the load factor is less than zero 075 */ 076 public HashedMap(int initialCapacity, float loadFactor) { 077 super(initialCapacity, loadFactor); 078 } 079 080 /** 081 * Constructor copying elements from another map. 082 * 083 * @param map the map to copy 084 * @throws NullPointerException if the map is null 085 */ 086 public HashedMap(Map map) { 087 super(map); 088 } 089 090 //----------------------------------------------------------------------- 091 /** 092 * Clones the map without cloning the keys or values. 093 * 094 * @return a shallow clone 095 */ 096 public Object clone() { 097 return super.clone(); 098 } 099 100 /** 101 * Write the map out using a custom routine. 102 */ 103 private void writeObject(ObjectOutputStream out) throws IOException { 104 out.defaultWriteObject(); 105 doWriteObject(out); 106 } 107 108 /** 109 * Read the map in using a custom routine. 110 */ 111 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 112 in.defaultReadObject(); 113 doReadObject(in); 114 } 115 116 }