Package net.cscott.jutil
Class UniqueVector<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
net.cscott.jutil.UniqueVector<E>
- All Implemented Interfaces:
Cloneable,Iterable<E>,Collection<E>,List<E>,SequencedCollection<E>,Set<E>
- Direct Known Subclasses:
UniqueStack
A unique vector refuses to addElement duplicates.
Conforms to the JDK 1.2 Collections API.
- Author:
- C. Scott Ananian <cananian@alumni.princeton.edu>
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty UniqueVector.UniqueVector(int initialCapacity) Constructs an empty UniqueVector with the specified initial capacity.UniqueVector(Collection<? extends E> c) Constructs a vector containing the elements of the specifiedCollection, in the order they are returned by the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position in this list.booleanAdds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector.voidaddElement(E obj) Adds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector.intcapacity()Returns the current capacity of this vector.voidclear()Removes all of the elements from this collection.clone()Returns a clone of this vector.booleanTests if the specified object is a component in this vector.voidCopies the components of this vector into the specified array.elementAt(int index) Returns the component at the specified index.elements()Returns an enumeration of the components of this vector.voidensureCapacity(int minCapacity) Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.booleanReturns the first component of this vector.get(int index) Returns the element at the specified posision in this vector.inthashCode()intReturns the first (and only) occurrence of the given argument, testing for equality using theequalsmethod.intReturns the first occurrence of the given argument, beginning the search atindex, and testing for equality using theequalsmethod.voidinsertElementAt(E obj, int index) Inserts the specified object as a component in this vector at the specifiedindex.booleanisEmpty()Tests if this vector has no components.Returns the last component of the vector.intlastIndexOf(Object elem) Returns the index of the last (and only) occurrence of the specified object in this vector.intlastIndexOf(Object elem, int index) Searches backwards for the specified object, starting from the specified index, and returns an index to it.remove(int index) Removes the element at the specified position in this vector.voidRemoves all components from this vector and sets its size to zero.final booleanremoveElement(Object obj) Removes the first (and only) occurance of the argument from this vector.voidremoveElementAt(int index) Deletes the component at the specified index.Replaces the element at the specified position in this vector with the specified element.voidsetElementAt(E obj, int index) Sets the component at the specifiedindexof this vector to be the specified object.intsize()Returns the number of components in this vector.Creates aSpliteratorover the elements of this vector, in order.Object[]toArray()<T> T[]toArray(T[] a) toString()Returns a string representation of this vector.voidTrims the capacity of this vector to be the vector's current size.Methods inherited from class java.util.AbstractList
addAll, iterator, listIterator, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
addAll, containsAll, remove, removeAll, retainAllMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addAll, addFirst, addLast, containsAll, getFirst, getLast, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort
-
Constructor Details
-
UniqueVector
public UniqueVector()Constructs an empty UniqueVector. -
UniqueVector
public UniqueVector(int initialCapacity) Constructs an empty UniqueVector with the specified initial capacity. -
UniqueVector
Constructs a vector containing the elements of the specifiedCollection, in the order they are returned by the collection's iterator. Duplicate elements are skipped.
-
-
Method Details
-
addElement
Adds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector. Duplicate elements are thrown away. The capacity of the vector is increased if necessary.- Parameters:
obj- the component to be added.
-
add
Inserts the specified element at the specified position in this list. To maintain uniqueness, any previous instance of this element in the vector is removed prior to insertion. -
add
Adds the specified component to the end of this vector, increasing its size by one, if it doesn't already exist in the vector. Duplicate elements are thrown away. The capacity of the vector is increased if necessary. -
capacity
public int capacity()Returns the current capacity of this vector.- Throws:
UnsupportedOperationException- not supported.
-
clone
Returns a clone of this vector.- Overrides:
clonein classObject- Returns:
- a clone of this vector.
- Throws:
CloneNotSupportedException- if the UniqueVector cannot be cloned.
-
contains
Tests if the specified object is a component in this vector.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
elem- an object- Returns:
trueif the specified object is a component in this vector;falseotherwise.
-
copyInto
Copies the components of this vector into the specified array. The array must be big enough to hold all the objects in this vector.- Parameters:
anArray- the array into which the components get copied.
-
toArray
-
toArray
public <T> T[] toArray(T[] a) -
elementAt
Returns the component at the specified index.- Parameters:
index- an index into this vector.- Returns:
- the component at the specified index.
- Throws:
ArrayIndexOutOfBoundsException- if an invalid index was given.
-
get
Returns the element at the specified posision in this vector. -
elements
Returns an enumeration of the components of this vector.- Returns:
- an enumeration of the components of this vector.
-
ensureCapacity
public void ensureCapacity(int minCapacity) Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.- Parameters:
minCapacity- the desired minimum capacity.
-
firstElement
Returns the first component of this vector.- Returns:
- the first component of this vector.
- Throws:
NoSuchElementException- if this vector has no components.
-
indexOf
Returns the first (and only) occurrence of the given argument, testing for equality using theequalsmethod. -
indexOf
Returns the first occurrence of the given argument, beginning the search atindex, and testing for equality using theequalsmethod.- Parameters:
elem- an object.index- the index to start searching from.- Returns:
- the index of the first occurrence of the object argument in this
vector at position
indexor later in the vector; returns-1if the object is not found.
-
insertElementAt
Inserts the specified object as a component in this vector at the specifiedindex. Each component in this vector with an index greater or equal to the specifiedindexis shifted upward to have an index one greater than the value it had previously.The index must be a value greater than or equal to
0and less than or equal to the current size of the vector.To maintain uniqueness, removed any previous instance of the component in the vector before insertion.
- Parameters:
obj- the component to insert.index- where to insert the new component.- Throws:
ArrayIndexOutOfBoundsException- if the index was invalid.
-
isEmpty
public boolean isEmpty()Tests if this vector has no components. -
lastElement
Returns the last component of the vector.- Returns:
- the last component of the vector, i.e., the component at
index
size()-1. - Throws:
NoSuchElementException- if this vector is empty.
-
lastIndexOf
Returns the index of the last (and only) occurrence of the specified object in this vector.- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Parameters:
elem- the desired component.- Returns:
- the index of the last occurrence of the specified object in
this vector; returns
-1if the object is not found.
-
lastIndexOf
Searches backwards for the specified object, starting from the specified index, and returns an index to it.- Parameters:
elem- the desired component.index- the index to start searching from.- Returns:
- the index of the last occurrence of the specified object in
this vector at position less than
indexin the vector;-1if the object is not found.
-
removeAllElements
public void removeAllElements()Removes all components from this vector and sets its size to zero. -
clear
public void clear()Removes all of the elements from this collection. -
removeElement
Removes the first (and only) occurance of the argument from this vector. If the object is found in this vector, each component in the vector with an index greater or equal to the object's index is shifted downward to have an index one smaller than the value it had previously.- Parameters:
obj- the component to be removed.- Returns:
trueif the argument was a component of this vector;falseotherwise.
-
removeElementAt
public void removeElementAt(int index) Deletes the component at the specified index. Each component in this vector with an index greater than or equal to the specifiedindexis shifted downward to have an index one smaller than the value it had previously.The index must be a value greater than or equal to
0and less than the current size of the vector.- Parameters:
index- the index of the object to remove.- Throws:
ArrayIndexOutOfBoundsException- if the index was invalid.
-
remove
Removes the element at the specified position in this vector. -
setElementAt
Sets the component at the specifiedindexof this vector to be the specified object. The previous component at that position is discarded.The index must be a value greater than or equal to
0and less than the current size of the vector.Nothing is done if the component at index is equal to obj. To maintain uniqueness, any component equal to obj is removed before the setElementAt() is done.
- Parameters:
obj- what the component is to be set to.index- the specified index.- Throws:
ArrayIndexOutOfBoundsException- if the index was invalid.
-
set
Replaces the element at the specified position in this vector with the specified element. -
size
public int size()Returns the number of components in this vector. -
toString
Returns a string representation of this vector.- Overrides:
toStringin classAbstractCollection<E>- Returns:
- a string representation of this vector.
-
trimToSize
public void trimToSize()Trims the capacity of this vector to be the vector's current size. An application can use this operation to minimize the storage of a vector. -
hashCode
public int hashCode() -
equals
-
spliterator
Creates aSpliteratorover the elements of this vector, in order. (This resolves the conflict between the default methods ofListandSet.)- Specified by:
spliteratorin interfaceCollection<E>- Specified by:
spliteratorin interfaceIterable<E>- Specified by:
spliteratorin interfaceList<E>- Specified by:
spliteratorin interfaceSet<E>- Since:
- 2.0
-