jpaul.DataStructs
Class ArraySet<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<T>
          extended by jpaul.DataStructs.ArraySet<T>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

public class ArraySet<T>
extends java.util.AbstractSet<T>
implements java.io.Serializable

ArraySet is an immutable, array-backed set. It consumes minimal memory; very good for small sets.

Version:
$Id: ArraySet.java,v 1.9 2006/03/14 02:55:23 salcianu Exp $
Author:
Alex Salcianu - salcianu@alum.mit.edu
See Also:
Serialized Form

Constructor Summary
ArraySet(java.util.Collection<T> coll)
          Creates an ArraySet containing the distinct elements from the colection coll.
ArraySet(java.util.Collection<T> coll, boolean collHasDistinctElements)
          Powerful and unsafe constructor: creates an ArraySet containing the distinct elements present in the colection coll.
ArraySet(java.util.Set<T> set)
          Creates an ArraySet containing the elements present in the set set.
ArraySet(T... ts)
          Creates an ArraySet with the elements given as a variable-length list of arguments (instead of a collection).
 
Method Summary
 boolean contains(java.lang.Object o)
           
 java.util.Iterator<T> iterator()
           
 int size()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, containsAll, isEmpty, remove, 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
add, addAll, clear, containsAll, isEmpty, remove, retainAll, toArray, toArray
 

Constructor Detail

ArraySet

public ArraySet(java.util.Set<T> set)
Creates an ArraySet containing the elements present in the set set. The created ArraySet contains the elements of set at the point this constructor is invoked; future changes to set are not reflected by the ArraySet.


ArraySet

public ArraySet(java.util.Collection<T> coll)
Creates an ArraySet containing the distinct elements from the colection coll. coll may contain duplicates, but only distinct elements will appear in the created ArraySet. Future changes to coll are not reflected by this ArraySet.

Note: This constructor is more general than ArraySet(Set), which may lead to a little bit of confusion: a set is also a collection, so both constructors apply in certain cases. Semantically, whether you invoke one constructor or the other, it is the same thing. Still, the constructor that takes a set knows that the elements of the set are unique (by the contract of a set); the constructor that takes a collection has no such guarantee, so it needs to avoid inserting equal elements to the constructed ArraySet. So, the constructor that takes a set is faster; it's a classic example of using the type system to speed-up the program execution.


ArraySet

public ArraySet(T... ts)
Creates an ArraySet with the elements given as a variable-length list of arguments (instead of a collection). E.g., new ArraySet<T>(e1, e2, e3, e4) is equivalent to new ArraySet<T>(Arrays.asList(e1, e2, e3, e4)).


ArraySet

public ArraySet(java.util.Collection<T> coll,
                boolean collHasDistinctElements)
Powerful and unsafe constructor: creates an ArraySet containing the distinct elements present in the colection coll. If the second parameter collHasDistinctElements is true, then this constructor assumes that coll contains only distinct elements and skips the costly step of determining the unique elements from coll. This feature is unsafe and should be used with maximal care!.

The created ArraySet contains the elements of coll at the point this constructor is invoked; future changes to coll are not reflected by the ArraySet.

Method Detail

contains

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

iterator

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

size

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


Copyright 2005 Alexandru Salcianu - salcianu@alum.mit.edu