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

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by com.google.common.collect.SortedArraySet<E>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>, java.util.SortedSet<E>

public final class SortedArraySet<E>
extends java.util.AbstractSet<E>
implements java.util.SortedSet<E>, java.io.Serializable

A sorted set that keeps its elements in a sorted ArrayList. Null elements are allowed when the SortedArraySet is constructed with an explicit comparator that supports nulls.

This class is useful when you may have many sorted sets that only have zero or one elements each. The performance of this implementation does not scale to large numbers of elements as well as TreeSet, but it is much more memory-efficient per entry.

Each SortedArraySet has a capacity, because it is backed by an ArrayList. The capacity is the size of the array used to store the elements in the set. It is always at least as large as the set size. As elements are added to the set, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has O(lg n) amortized time cost.

An application can increase the capacity of the set before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

This implementation is not synchronized. As with ArrayList, external synchronization is needed if multiple threads access a SortedArraySet instance concurrently and at least one adds or deletes any elements.

Author:
Matthew Harris, Mike Bostock
See Also:
Serialized Form

Constructor Summary
SortedArraySet()
          Constructs a new empty sorted set, sorted according to the element's natural order, with an initial capacity of ten.
SortedArraySet(java.util.Collection<? extends E> collection)
          Creates a new sorted set with the same elements as the specified collection.
SortedArraySet(java.util.Comparator<? super E> comparator)
          Creates a new empty sorted set, sorted according to the specified comparator, with the initial capacity of ten.
SortedArraySet(java.util.Comparator<? super E> comparator, int initialCapacity)
          Creates a new empty sorted set, sorted according to the specified comparator, with the specified initial capacity.
SortedArraySet(int initialCapacity)
          Constructs a new empty sorted set, sorted according to the element's natural order, with the specified initial capacity.
SortedArraySet(java.util.SortedSet<E> set)
          Creates a new sorted set with the same elements and the same ordering as the specified sorted set.
 
Method Summary
 boolean add(E o)
           
 boolean addAll(java.util.Collection<? extends E> c)
           
 void clear()
           
 java.util.Comparator<? super E> comparator()
          Returns the comparator associated with this sorted set, or Comparators.naturalOrder if it uses its elements' natural ordering.
 boolean contains(java.lang.Object o)
           
 void ensureCapacity(int minCapacity)
          Increases the capacity of this sorted set, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
 boolean equals(java.lang.Object o)
           
 E first()
           
 java.util.SortedSet<E> headSet(E toElement)
           
 java.util.Iterator<E> iterator()
           
 E last()
           
 boolean remove(java.lang.Object o)
           
 int size()
           
 java.util.SortedSet<E> subSet(E fromElement, E toElement)
           
 java.util.SortedSet<E> tailSet(E fromElement)
           
 void trimToSize()
          Trims the capacity of this sorted set to be the set's current size.
 
Methods inherited from class java.util.AbstractSet
hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
containsAll, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 

Constructor Detail

SortedArraySet

public SortedArraySet()
Constructs a new empty sorted set, sorted according to the element's natural order, with an initial capacity of ten. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutally comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers), the add method may throw a ClassCastException.

See Also:
Comparable, Comparators.naturalOrder()

SortedArraySet

public SortedArraySet(int initialCapacity)
Constructs a new empty sorted set, sorted according to the element's natural order, with the specified initial capacity. All elements inserted into the set must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers), the add method may throw a ClassCastException.

Parameters:
initialCapacity - the initial capacity of the list
Throws:
java.lang.IllegalArgumentException - if initialCapacity is negative
See Also:
Comparators.naturalOrder()

SortedArraySet

public SortedArraySet(java.util.Comparator<? super E> comparator)
Creates a new empty sorted set, sorted according to the specified comparator, with the initial capacity of ten. All elements inserted into the set must be mutually comparable by the specified comparator: comparator.compare(e1,e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint, the add method may throw a ClassCastException.

Parameters:
comparator - the comparator used to sort elements in this set

SortedArraySet

public SortedArraySet(java.util.Comparator<? super E> comparator,
                      int initialCapacity)
Creates a new empty sorted set, sorted according to the specified comparator, with the specified initial capacity. All elements inserted into the set must be mutually comparable by the specified comparator: comparator.compare(e1,e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to the set that violates this constraint, the add method may throw a ClassCastException.

Parameters:
comparator - the comparator used to sort elements in this set

SortedArraySet

public SortedArraySet(java.util.Collection<? extends E> collection)
Creates a new sorted set with the same elements as the specified collection. If the specified collection is a SortedSet instance, this constructor behaves identically to SortedArraySet(SortedSet). Otherwise, the elements are sorted according to the elements' natural order; see SortedArraySet().

Parameters:
collection - the elements that will comprise the new set
Throws:
java.lang.ClassCastException - if the elements in the specified collection are not mutually comparable

SortedArraySet

public SortedArraySet(java.util.SortedSet<E> set)
Creates a new sorted set with the same elements and the same ordering as the specified sorted set.

Parameters:
set - the set whose elements will comprise the new set
Method Detail

ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this sorted set, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity

trimToSize

public void trimToSize()
Trims the capacity of this sorted set to be the set's current size. An application can use this operation to minimize the storage of a sorted set.


add

public boolean add(E o)
Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>
Overrides:
add in class java.util.AbstractCollection<E>

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Specified by:
addAll in interface java.util.Collection<E>
Specified by:
addAll in interface java.util.Set<E>
Overrides:
addAll in class java.util.AbstractCollection<E>

clear

public void clear()
Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.Set<E>
Overrides:
clear in class java.util.AbstractCollection<E>

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.Set<E>
Overrides:
contains in class java.util.AbstractCollection<E>

equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Collection<E>
Specified by:
equals in interface java.util.Set<E>
Overrides:
equals in class java.util.AbstractSet<E>

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.Set<E>
Specified by:
iterator in class java.util.AbstractCollection<E>

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>
Overrides:
remove in class java.util.AbstractCollection<E>

size

public int size()
Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.Set<E>
Specified by:
size in class java.util.AbstractCollection<E>

comparator

public java.util.Comparator<? super E> comparator()
Returns the comparator associated with this sorted set, or Comparators.naturalOrder if it uses its elements' natural ordering.

Specified by:
comparator in interface java.util.SortedSet<E>

subSet

public java.util.SortedSet<E> subSet(E fromElement,
                                     E toElement)
Specified by:
subSet in interface java.util.SortedSet<E>

headSet

public java.util.SortedSet<E> headSet(E toElement)
Specified by:
headSet in interface java.util.SortedSet<E>

tailSet

public java.util.SortedSet<E> tailSet(E fromElement)
Specified by:
tailSet in interface java.util.SortedSet<E>

first

public E first()
Specified by:
first in interface java.util.SortedSet<E>

last

public E last()
Specified by:
last in interface java.util.SortedSet<E>


Copyright © 2008 Google. All Rights Reserved.