001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import com.google.common.annotations.GwtCompatible; 020import com.google.common.annotations.GwtIncompatible; 021import com.google.common.annotations.J2ktIncompatible; 022import org.checkerframework.checker.nullness.qual.Nullable; 023 024/** 025 * Multiset implementation that uses hashing for key and entry access. 026 * 027 * @author Kevin Bourrillion 028 * @author Jared Levy 029 * @since 2.0 030 */ 031@GwtCompatible(serializable = true, emulated = true) 032public final class HashMultiset<E extends @Nullable Object> extends AbstractMapBasedMultiset<E> { 033 034 /** Creates a new, empty {@code HashMultiset} using the default initial capacity. */ 035 public static <E extends @Nullable Object> HashMultiset<E> create() { 036 return create(ObjectCountHashMap.DEFAULT_SIZE); 037 } 038 039 /** 040 * Creates a new, empty {@code HashMultiset} with the specified expected number of distinct 041 * elements. 042 * 043 * @param distinctElements the expected number of distinct elements 044 * @throws IllegalArgumentException if {@code distinctElements} is negative 045 */ 046 public static <E extends @Nullable Object> HashMultiset<E> create(int distinctElements) { 047 return new HashMultiset<>(distinctElements); 048 } 049 050 /** 051 * Creates a new {@code HashMultiset} containing the specified elements. 052 * 053 * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. 054 * 055 * @param elements the elements that the multiset should contain 056 */ 057 public static <E extends @Nullable Object> HashMultiset<E> create( 058 Iterable<? extends E> elements) { 059 HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); 060 Iterables.addAll(multiset, elements); 061 return multiset; 062 } 063 064 HashMultiset(int distinctElements) { 065 super(distinctElements); 066 } 067 068 @Override 069 ObjectCountHashMap<E> newBackingMap(int distinctElements) { 070 return new ObjectCountHashMap<>(distinctElements); 071 } 072 073 @GwtIncompatible // Not needed in emulated source. 074 @J2ktIncompatible 075 private static final long serialVersionUID = 0; 076}