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; 020 021/** 022 * A {@code Multiset} implementation with predictable iteration order. Its iterator orders elements 023 * according to when the first occurrence of the element was added. When the multiset contains 024 * multiple instances of an element, those instances are consecutive in the iteration order. If all 025 * occurrences of an element are removed, after which that element is added to the multiset, the 026 * element will appear at the end of the iteration. 027 * 028 * <p>See the Guava User Guide article on <a href= 029 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multiset"> {@code 030 * Multiset}</a>. 031 * 032 * @author Kevin Bourrillion 033 * @author Jared Levy 034 * @since 2.0 035 */ 036@GwtCompatible(serializable = true, emulated = true) 037@SuppressWarnings("serial") // we're overriding default serialization 038public final class LinkedHashMultiset<E> extends AbstractMapBasedMultiset<E> { 039 040 /** Creates a new, empty {@code LinkedHashMultiset} using the default initial capacity. */ 041 public static <E> LinkedHashMultiset<E> create() { 042 return create(ObjectCountHashMap.DEFAULT_SIZE); 043 } 044 045 /** 046 * Creates a new, empty {@code LinkedHashMultiset} with the specified expected number of distinct 047 * elements. 048 * 049 * @param distinctElements the expected number of distinct elements 050 * @throws IllegalArgumentException if {@code distinctElements} is negative 051 */ 052 public static <E> LinkedHashMultiset<E> create(int distinctElements) { 053 return new LinkedHashMultiset<E>(distinctElements); 054 } 055 056 /** 057 * Creates a new {@code LinkedHashMultiset} containing the specified elements. 058 * 059 * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. 060 * 061 * @param elements the elements that the multiset should contain 062 */ 063 public static <E> LinkedHashMultiset<E> create(Iterable<? extends E> elements) { 064 LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); 065 Iterables.addAll(multiset, elements); 066 return multiset; 067 } 068 069 LinkedHashMultiset(int distinctElements) { 070 super(distinctElements); 071 } 072 073 @Override 074 void init(int distinctElements) { 075 backingMap = new ObjectCountLinkedHashMap<>(distinctElements); 076 } 077}