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) 032@ElementTypesAreNonnullByDefault 033public final class HashMultiset<E extends @Nullable Object> extends AbstractMapBasedMultiset<E> { 034 035 /** Creates a new, empty {@code HashMultiset} using the default initial capacity. */ 036 public static <E extends @Nullable Object> HashMultiset<E> create() { 037 return create(ObjectCountHashMap.DEFAULT_SIZE); 038 } 039 040 /** 041 * Creates a new, empty {@code HashMultiset} with the specified expected number of distinct 042 * elements. 043 * 044 * @param distinctElements the expected number of distinct elements 045 * @throws IllegalArgumentException if {@code distinctElements} is negative 046 */ 047 public static <E extends @Nullable Object> HashMultiset<E> create(int distinctElements) { 048 return new HashMultiset<E>(distinctElements); 049 } 050 051 /** 052 * Creates a new {@code HashMultiset} containing the specified elements. 053 * 054 * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. 055 * 056 * @param elements the elements that the multiset should contain 057 */ 058 public static <E extends @Nullable Object> HashMultiset<E> create( 059 Iterable<? extends E> elements) { 060 HashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); 061 Iterables.addAll(multiset, elements); 062 return multiset; 063 } 064 065 HashMultiset(int distinctElements) { 066 super(distinctElements); 067 } 068 069 @Override 070 ObjectCountHashMap<E> newBackingMap(int distinctElements) { 071 return new ObjectCountHashMap<>(distinctElements); 072 } 073 074 @GwtIncompatible // Not needed in emulated source. 075 @J2ktIncompatible 076 private static final long serialVersionUID = 0; 077}