com.google.common.collect
Class LinkedListMultiset<E>

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.collect.ForwardingCollection<E>
          extended by com.google.common.collect.ForwardingMultiset<E>
              extended by com.google.common.collect.LinkedListMultiset<E>
All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>

public final class LinkedListMultiset<E>
extends ForwardingMultiset<E>

An implementation of Multiset that supports deterministic iteration order for elements. The iteration order is preserved across non-distinct elements. For example,

  Multiset<String> m = ...
  m.add("a");
  m.add("b");
  m.add("a");
In this case, the iteration order would be [a, b, a]. Unlike LinkedHashMultiset, the iteration order is kept consistent across non-distinct elements. For example, calling
  map.remove("a");
changes the iteration order to [b, a].

This class provides first(), last() and listIterator() methods on top of the standard Multiset interface.

Note that the performance of this implementation may differ from other multiset implementations such as HashMultiset. Element addition takes time and memory proportional to the number of elements being added, even if those elements are not distinct. Element removal takes constant time if the element to remove is not contained in the multiset, and otherwise takes time proportional to the number of elements in the multiset.

Author:
Mike Bostock
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E>
 
Constructor Summary
LinkedListMultiset()
          Constructs a new empty LinkedListMultiset.
LinkedListMultiset(Iterable<? extends E> elements)
          Constructs a new LinkedListMultiset containing the specified elements.
 
Method Summary
 boolean add(E e)
           
 boolean add(E e, int n)
          Adds a number of occurrences of the specified element to this multiset.
 boolean addAll(Collection<? extends E> c)
           
 void clear()
           
 Set<E> elementSet()
          Returns a view of the elements of this multiset as a set.
 Set<Multiset.Entry<E>> entrySet()
          Returns the data of this multiset as a set of Entry instances.
 E first()
          Returns the first element in this multiset.
 boolean isEmpty()
           
 Iterator<E> iterator()
           
 E last()
          Returns the last element in this multiset.
 ListIterator<E> listIterator()
           
 boolean remove(Object e)
           
 int remove(Object e, int n)
          Removes a number of occurrences of the specified element from this multiset.
 boolean removeAll(Collection<?> c)
           
 int removeAllOccurrences(Object e)
          Removes all occurrences of the specified element from this multiset.
 boolean retainAll(Collection<?> c)
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 String toString()
          
 
Methods inherited from class com.google.common.collect.ForwardingMultiset
count, delegate, equals, hashCode
 
Methods inherited from class com.google.common.collect.ForwardingCollection
contains, containsAll, size
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
contains, containsAll, size
 

Constructor Detail

LinkedListMultiset

public LinkedListMultiset()
Constructs a new empty LinkedListMultiset.


LinkedListMultiset

public LinkedListMultiset(Iterable<? extends E> elements)
Constructs a new LinkedListMultiset containing the specified elements.

Method Detail

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<E>
Overrides:
isEmpty in class ForwardingCollection<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Overrides:
iterator in class ForwardingCollection<E>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<E>
Overrides:
toArray in class ForwardingCollection<E>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface Collection<E>
Overrides:
toArray in class ForwardingCollection<E>

first

public E first()
Returns the first element in this multiset.

Throws:
NoSuchElementException - if the multiset is empty

last

public E last()
Returns the last element in this multiset.

Throws:
NoSuchElementException - if the multiset is empty

add

public boolean add(@Nullable
                   E e)
Specified by:
add in interface Collection<E>
Overrides:
add in class ForwardingCollection<E>

add

public boolean add(@Nullable
                   E e,
                   int n)
Description copied from interface: Multiset
Adds a number of occurrences of the specified element to this multiset.

Specified by:
add in interface Multiset<E>
Overrides:
add in class ForwardingMultiset<E>
Parameters:
e - the element to add
n - the number of occurrences to add
Returns:
true if the collection changed as a result (this should always be the case unless occurrences is zero)

remove

public boolean remove(@Nullable
                      Object e)
Specified by:
remove in interface Collection<E>
Overrides:
remove in class ForwardingCollection<E>

remove

public int remove(@Nullable
                  Object e,
                  int n)
Description copied from interface: Multiset
Removes a number of occurrences of the specified element from this multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed.

Specified by:
remove in interface Multiset<E>
Overrides:
remove in class ForwardingMultiset<E>
Parameters:
e - the element whose occurrences should be removed
n - the number of occurrences of this element to remove
Returns:
the number of occurrences that were successfully removed (zero if the element was not present)

removeAllOccurrences

public int removeAllOccurrences(@Nullable
                                Object e)
Description copied from interface: Multiset
Removes all occurrences of the specified element from this multiset. This method complements Multiset.remove(java.lang.Object, int), which removes only one occurrence at a time.

Specified by:
removeAllOccurrences in interface Multiset<E>
Overrides:
removeAllOccurrences in class ForwardingMultiset<E>
Parameters:
e - the element whose occurrences should all be removed
Returns:
the number of occurrences successfully removed, possibly zero

addAll

public boolean addAll(Collection<? extends E> c)
Specified by:
addAll in interface Collection<E>
Overrides:
addAll in class ForwardingCollection<E>

removeAll

public boolean removeAll(Collection<?> c)
Specified by:
removeAll in interface Collection<E>
Overrides:
removeAll in class ForwardingCollection<E>

retainAll

public boolean retainAll(Collection<?> c)
Specified by:
retainAll in interface Collection<E>
Overrides:
retainAll in class ForwardingCollection<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>
Overrides:
clear in class ForwardingCollection<E>

listIterator

public ListIterator<E> listIterator()

elementSet

public Set<E> elementSet()
Description copied from interface: Multiset
Returns a view of the elements of this multiset as a set. All remove operations on the returned set "write through" to the underlying multiset, removing all occurrences of the given elements. No add operations are supported. Note that you can use elementSet().size() to find the number of distinct elements in this multiset.

Specified by:
elementSet in interface Multiset<E>
Overrides:
elementSet in class ForwardingMultiset<E>
Returns:
the distinct elements of this multiset, viewed as a set

entrySet

public Set<Multiset.Entry<E>> entrySet()
Description copied from interface: Multiset
Returns the data of this multiset as a set of Entry instances. This set contains precisely one Entry instance for each distinct element of the multiset. The iteration order of this set is implementation-dependent.

Specified by:
entrySet in interface Multiset<E>
Overrides:
entrySet in class ForwardingMultiset<E>
Returns:
the entries of this multiset, viewed as a set

toString

public String toString()

Returns a string representation of this multiset. The string representation consists of a list of elements in the order returned by iterator(), enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Adjacent equal elements are collapsed; for example, "a, a, a" is represented as "a x 3".

For example:

  Multiset<String> multiset = new LinkedListMultiset<String>();
  multiset.add("a", 3);
  multiset.add("b", 2);
  multiset.add("c");
  return multiset.toString();
returns the string "[a x 3, b x 2, c]".

Overrides:
toString in class ForwardingObject