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 */ 017package org.apache.commons.collections4.multiset; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.io.Serializable; 023import java.util.Collection; 024import java.util.HashMap; 025 026/** 027 * Implements {@code MultiSet}, using a {@link HashMap} to provide the 028 * data storage. This is the standard implementation of a multiset. 029 * <p> 030 * A {@code MultiSet} stores each object in the collection together with a 031 * count of occurrences. Extra methods on the interface allow multiple copies 032 * of an object to be added or removed at once. 033 * 034 * @param <E> the type held in the multiset 035 * @since 4.1 036 */ 037public class HashMultiSet<E> extends AbstractMapMultiSet<E> implements Serializable { 038 039 /** Serial version lock */ 040 private static final long serialVersionUID = 20150610L; 041 042 /** 043 * Constructs an empty {@link HashMultiSet}. 044 */ 045 public HashMultiSet() { 046 super(new HashMap<E, MutableInteger>()); 047 } 048 049 /** 050 * Constructs a multiset containing all the members of the given collection. 051 * 052 * @param coll a collection to copy into this multiset 053 */ 054 public HashMultiSet(final Collection<? extends E> coll) { 055 this(); 056 addAll(coll); 057 } 058 059 //----------------------------------------------------------------------- 060 /** 061 * Write the multiset out using a custom routine. 062 * 063 * @param out the output stream 064 * @throws IOException if an error occurs while writing to the stream 065 */ 066 private void writeObject(final ObjectOutputStream out) throws IOException { 067 out.defaultWriteObject(); 068 super.doWriteObject(out); 069 } 070 071 /** 072 * Read the multiset in using a custom routine. 073 * 074 * @param in the input stream 075 * @throws IOException if an error occurs while reading from the stream 076 * @throws ClassNotFoundException if an object read from the stream can not be loaded 077 */ 078 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 079 in.defaultReadObject(); 080 setMap(new HashMap<E, MutableInteger>()); 081 super.doReadObject(in); 082 } 083 084}