|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectinf.util.HashedList<E>
E
- the element type for elements in the Listpublic class HashedList<E>
This class is a linked list implementation of the List
interface. It implements only those list operations that can be performed
efficiently, which means index-based access to the list is not supported.
The most important difference to a java.util.LinkedList
, on
which class this implementation is based, is that in addition to the internal
representation as a doubly linked list, a hash map is maintained that maps
the elements in the list to the entries in the list containing them. This
makes efficient removal from the list possible, but it also comes at a cost:
some other operations become slightly more expensive and the required memory
roughly doubles. Furthermore, the list cannot contain two or more equal
elements or null
as these are used as keys in the hash map.
In addition to implementing the List
interface, the
HashedList
class provides uniformly named methods to get,
remove and insert an element at the beginning and end of the list. These
operations allow linked lists to be used as a stack, queue, or double-ended
queue (deque).
LinkedList
implementation by Josh BlochConstructor Summary | |
---|---|
HashedList()
This constructor creates an empty hashed list. |
|
HashedList(java.util.Collection<? extends E> elements)
This constructor creates a hashed list containing all the elements from the given Collection in the order returned by the Collection's iterator. |
|
HashedList(E... elements)
This constructor creates a hashed list containing the given elements in the given order. |
Method Summary | ||
---|---|---|
boolean |
add(E elt)
This function appends the specified element to the end of this list. |
|
void |
add(int index,
E element)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
boolean |
addAll(java.util.Collection<? extends E> elements)
This function adds all the elements from the given Collection to this list. |
|
boolean |
addAll(int index,
java.util.Collection<? extends E> c)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
void |
addFirst(E elt)
This function inserts the given element at the beginning of this list. |
|
void |
addLast(E elt)
This function appends the given element to the end of this list. |
|
void |
clear()
This function removes all of the elements from this Collection. |
|
HashedList<E> |
clone()
This function returns a shallow copy of this HashedList. |
|
boolean |
contains(java.lang.Object elt)
This function returns true if this Collection contains the specified element which must not be null. |
|
boolean |
containsAll(java.util.Collection<?> elements)
This function returns true if this Collection contains all the specified elements in the given Collection which must not be null. |
|
boolean |
equals(java.util.List<E> list)
This function tests whether the this and the given List are equal. |
|
boolean |
equals(java.lang.Object o)
This function tests whether this and the given Object are equal. |
|
E |
get(int index)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
E |
getFirst()
This function returns the first element in this list. |
|
E |
getLast()
This function returns the last element in this list. |
|
int |
hashCode()
This function computes a hash value for this HashedList. |
|
int |
indexOf(java.lang.Object o)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
boolean |
isEmpty()
This function tests whether this Collection contains no elements. |
|
java.util.Iterator<E> |
iterator()
This function returns an iterator over the elements in this list in proper sequence. |
|
int |
lastIndexOf(java.lang.Object o)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
java.util.ListIterator<E> |
listIterator()
This function returns a list-iterator of the elements in this list (in proper sequence). |
|
java.util.ListIterator<E> |
listIterator(int index)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
E |
remove(int index)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
boolean |
remove(java.lang.Object elt)
This function removes the occurrence of the specified element in this list. |
|
boolean |
removeAll(java.util.Collection<?> elements)
This function removes the occurrence of all the specified elements in this list. |
|
E |
removeFirst()
This function removes and returns the first element from this list. |
|
E |
removeLast()
This function removes and returns the last element from this list. |
|
boolean |
retainAll(java.util.Collection<?> elements)
This function removes the occurrence of all the elements in this list that are not in the given Collection. |
|
E |
set(int index,
E element)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
int |
size()
This function returns the number of elements in this Collection. |
|
java.util.List<E> |
subList(int fromIndex,
int toIndex)
This class does not support index-based access to the list and an exception will be thrown if this method is called. |
|
java.lang.Object[] |
toArray()
This function returns an array containing all of the elements in this list in the correct order. |
|
|
toArray(T[] a)
This function fills a given Array with all of the elements in this list in the correct order. |
|
java.lang.String |
toString()
This function returns a printable representation of this HashedList. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public HashedList()
This constructor creates an empty hashed list.
public HashedList(java.util.Collection<? extends E> elements)
This constructor creates a hashed list containing all the elements from the given Collection in the order returned by the Collection's iterator.
elements
- the initial elements for this listpublic HashedList(E... elements)
This constructor creates a hashed list containing the given elements in the given order.
elements
- the initial elements for this listMethod Detail |
---|
public HashedList<E> clone()
This function returns a shallow copy of this HashedList. The elements themselves are not cloned.
clone
in class java.lang.Object
public boolean add(E elt)
This function appends the specified element to the end of this list. Note that this element must not be in the list already or null. Use addLast(...) for more readable code.
add
in interface java.util.Collection<E>
add
in interface java.util.List<E>
elt
- element to be appended to this list
public boolean addAll(java.util.Collection<? extends E> elements)
This function adds all the elements from the given Collection to this list. The new elements will be appended to the end of this list in the order returned by the given Collection's iterator. Note that none of the given elements must already be in the list or null.
addAll
in interface java.util.Collection<E>
addAll
in interface java.util.List<E>
elements
- the Collection of new elements to be appended
public boolean isEmpty()
This function tests whether this Collection contains no elements. If this is true, the size of the Collection is 0.
isEmpty
in interface java.util.Collection<E>
isEmpty
in interface java.util.List<E>
public int size()
This function returns the number of elements in this Collection. The result is a positive integer.
size
in interface java.util.Collection<E>
size
in interface java.util.List<E>
public boolean contains(java.lang.Object elt)
This function returns true if this Collection contains the specified element which must not be null. The test is performed by looking up the element in the hash table and should thus be efficient.
contains
in interface java.util.Collection<E>
contains
in interface java.util.List<E>
elt
- element whose presence in this list is to be tested
public boolean containsAll(java.util.Collection<?> elements)
This function returns true if this Collection contains all the specified elements in the given Collection which must not be null. The test is performed by looking up the elements in the hash table and should thus be relatively efficient.
containsAll
in interface java.util.Collection<E>
containsAll
in interface java.util.List<E>
elements
- the elements whose presence in this list is to be tested
public java.lang.Object[] toArray()
This function returns an array containing all of the elements in this list in the correct order.
toArray
in interface java.util.Collection<E>
toArray
in interface java.util.List<E>
public <T> T[] toArray(T[] a)
This function fills a given Array with all of the elements in this list in the correct order. If the given Array is too small a new Array of sufficient size is created. If the given Array is oversized a null element indicates the end of the list.
toArray
in interface java.util.Collection<E>
toArray
in interface java.util.List<E>
a
- the Array to be filled
public void clear()
This function removes all of the elements from this Collection.
clear
in interface java.util.Collection<E>
clear
in interface java.util.List<E>
public boolean remove(java.lang.Object elt)
This function removes the occurrence of the specified element in this list. If the list does not contain the element, it is unchanged.
remove
in interface java.util.Collection<E>
remove
in interface java.util.List<E>
elt
- element to be removed from this list, if present
public boolean removeAll(java.util.Collection<?> elements)
This function removes the occurrence of all the specified elements in this list. If the list does not contain any of the elements, it is unchanged.
removeAll
in interface java.util.Collection<E>
removeAll
in interface java.util.List<E>
elements
- the elements to be removed from this list, if present
public boolean retainAll(java.util.Collection<?> elements)
This function removes the occurrence of all the elements in this list that are not in the given Collection. If the list contain all the elements, it is unchanged.
retainAll
in interface java.util.Collection<E>
retainAll
in interface java.util.List<E>
elements
- the elements to be retained this list, if present
public void add(int index, E element)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
add
in interface java.util.List<E>
index
- index at which the specified element is to be insertedelement
- element to be inserted
java.lang.UnsupportedOperationException
- unconditionallypublic boolean addAll(int index, java.util.Collection<? extends E> c)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
addAll
in interface java.util.List<E>
index
- index at which the specified element is to be insertedc
- elements to be inserted into this list
java.lang.UnsupportedOperationException
- unconditionallypublic E set(int index, E element)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
set
in interface java.util.List<E>
index
- index of element to returnelement
- element to be stored at the specified position
java.lang.UnsupportedOperationException
- unconditionallypublic int indexOf(java.lang.Object o)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
indexOf
in interface java.util.List<E>
o
- element to search for
java.lang.UnsupportedOperationException
- unconditionallypublic int lastIndexOf(java.lang.Object o)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
lastIndexOf
in interface java.util.List<E>
o
- element to search for
java.lang.UnsupportedOperationException
- unconditionallypublic E get(int index)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
get
in interface java.util.List<E>
index
- index of element to return
java.lang.UnsupportedOperationException
- unconditionallypublic java.util.ListIterator<E> listIterator()
This function returns a list-iterator of the elements in this list (in
proper sequence). It obeys the general contract of
List.listIterator(int)
.
The list-iterator is fail-fast: if the list is structurally
modified at any time after the Iterator is created, in any way except
through the list-iterator's own remove
or add
methods, the list-iterator will throw a
ConcurrentModificationException
. Thus, in the face of
concurrent modification, the iterator fails quickly and cleanly, rather
than risking arbitrary, non-deterministic behaviour at an undetermined
time in the future.
listIterator
in interface java.util.List<E>
List.listIterator()
public java.util.ListIterator<E> listIterator(int index)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
listIterator
in interface java.util.List<E>
index
- index of first element to be returned from the list iterator
(by a call to the next method)
java.lang.UnsupportedOperationException
- unconditionallypublic java.util.List<E> subList(int fromIndex, int toIndex)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
subList
in interface java.util.List<E>
fromIndex
- low endpoint (inclusive) of the subListtoIndex
- high endpoint (exclusive) of the subList
java.lang.UnsupportedOperationException
- unconditionallypublic E remove(int index)
This class does not support index-based access to the list and an exception will be thrown if this method is called.
remove
in interface java.util.List<E>
index
- the index of the element to removed
java.lang.UnsupportedOperationException
- unconditionallypublic java.util.Iterator<E> iterator()
This function returns an iterator over the elements in this list in proper sequence. Actually, it returns a ListIterator.
iterator
in interface java.lang.Iterable<E>
iterator
in interface java.util.Collection<E>
iterator
in interface java.util.List<E>
public void addFirst(E elt)
This function inserts the given element at the beginning of this list.
elt
- the element to be inserted at the beginning of this listpublic void addLast(E elt)
This function appends the given element to the end of this list. (It is identical in function to the add(...) method; included only for consistency.)
elt
- the element to be inserted at the end of this listpublic E getFirst()
This function returns the first element in this list.
java.util.NoSuchElementException
- if this list is emptypublic E getLast()
This function returns the last element in this list.
java.util.NoSuchElementException
- if this list is emptypublic E removeFirst()
This function removes and returns the first element from this list.
java.util.NoSuchElementException
- if this list is emptypublic E removeLast()
This function removes and returns the last element from this list.
java.util.NoSuchElementException
- if this list is emptypublic boolean equals(java.lang.Object o)
This function tests whether this and the given Object are equal. If the given Object is a List, equality means equal elements in equal order. If it is a Collection the order is not taken into account. Otherwise they are not equal.
equals
in interface java.util.Collection<E>
equals
in interface java.util.List<E>
equals
in class java.lang.Object
o
- the Object this HashedList is compared to
public boolean equals(java.util.List<E> list)
This function tests whether the this and the given List are equal. Here, equality means equal elements in equal order.
list
- the List this one is compared to
public int hashCode()
This function computes a hash value for this HashedList. Note that this function is relatively expensive to compute (linear in the size of the List) and thus, hashing may not be very efficient.
hashCode
in interface java.util.Collection<E>
hashCode
in interface java.util.List<E>
hashCode
in class java.lang.Object
public java.lang.String toString()
This function returns a printable representation of this HashedList. It is printed as a Lisp-style list.
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |