Class HashSet<T>
java.lang.Object
HashSet<T>
Resizable implementation of the Deque and List interface via a standard
HashSet implementation. Does not allow for insertion of
null
elements.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidThis method is unsupported, except for the case whenindex == 0 || index == sizebooleanInserts the specified element into the queue represented by this HashSet if it is possible to do so (ie, if this item isn't already contained in the set), returning true upon success.booleanaddAll(int index, Collection<? extends T> c) Unsupported method.booleanaddAll(Collection<? extends T> c) Adds all of the elements in the specified collection at the end of this HashSet, as if by callingaddLast(T)on each one, in the order that they are returned by the collection's iterator.voidInserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).voidInserts the specified element at the end of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).voidclear()Removes all of the elements from this collection.clone()Creates and returns a shallow copy of this object.booleanReturnstrueif this HashSet contains the specified element.booleancontainsAll(Collection<?> items) Returnstrueif this collection contains all of the elements in the specified collection.Returns an iterator over the elements in this HashSet in reverse sequential order.element()Equivalent togetFirst().get(int index) Returns the element at the specified position in this list.Returns the item in this HashSet that is equal to the specified item, ornullif no such item existsgetFirst()Retrieves, but does not remove, the first element of this HashSet.getLast()Retrieves, but does not remove, the last element of this HashSet.intReturns the index of the first occurrence of the specified element in this list, or-1if this list does not contain the element.booleanisEmpty()Returnstrueif this collection contains no elements.iterator()Returns an iterator over this HashSet in the proper sequence.intEquivalent toindexOf(java.lang.Object).Returns a list iterator over the elements in this list (in proper sequence).listIterator(int index) Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.booleanEquivalent toadd(Object).booleanofferFirst(T item) Inserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).booleanEquivalent toadd(Object).peek()Equivalent togetFirst().Equivalent togetFirst().peekLast()Equivalent togetLast().poll()Equivalent toremoveFirst().Equivalent toremoveFirst().pollLast()Equivalent toremoveLast().pop()Equivalent toremoveFirst().voidEquivalent toaddFirst(T).remove()Equivalent toremoveFirst().remove(int index) Removes the element at the specified position in this list.booleanRemoves the specified element from this HashSet.booleanremoveAll(Collection<?> c) Removes all of this collection's elements that are also contained in the specified collection.Retrieves and removes the first element of this HashSet.booleanEquivalent toremove(Object)Retrieves and removes the last element of this HashSet.booleanEquivalent toremove(Object)booleanretainAll(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection.Unsupported method.intsize()Returns the number of items in this HashSet.subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specifiedfromIndex,inclusive, andtoIndex, exclusive.Object[]toArray()Returns an array containing all of the elements in this collection in the correct order.<A> A[]toArray(A[] a) Returns an array containing all of the elements in this collection in the correct order; the runtime type of the returned array is that of the specified array.toString()Returns a string representation of the object.Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
equals, hashCode, replaceAll, sort, spliterator
-
Field Details
-
size
protected int sizeThe number of items in this HashSet
-
-
Constructor Details
-
HashSet
public HashSet()Creates an empty HashSet.
-
-
Method Details
-
size
public int size()Returns the number of items in this HashSet. -
iterator
Returns an iterator over this HashSet in the proper sequence. -
get
Returns the item in this HashSet that is equal to the specified item, ornullif no such item existsNote that these need not be explicitly the same item.
- Parameters:
item- the item to be found in the HashSet- Returns:
- the item in this HashSet that is equal to the specified item, or
nullif no such item exists
-
getFirst
Retrieves, but does not remove, the first element of this HashSet. -
getLast
Retrieves, but does not remove, the last element of this HashSet. -
contains
Returnstrueif this HashSet contains the specified element.More formally, returns
trueif and only if this HashSet contains an elementesuch thatObjects.equals(o, e). -
containsAll
Returnstrueif this collection contains all of the elements in the specified collection.- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceList<T>- Parameters:
items- collection to be checked for containment in this collection- Returns:
trueif this collection contains all of the elements in the specified collection.
-
removeFirst
Retrieves and removes the first element of this HashSet.- Specified by:
removeFirstin interfaceDeque<T>- Returns:
- the first element of this HashSet.
-
removeLast
Retrieves and removes the last element of this HashSet.- Specified by:
removeLastin interfaceDeque<T>- Returns:
- the last element of this HashSet.
-
add
Inserts the specified element into the queue represented by this HashSet if it is possible to do so (ie, if this item isn't already contained in the set), returning true upon success. -
toString
Returns a string representation of the object. -
isEmpty
public boolean isEmpty()Returnstrueif this collection contains no elements. -
toArray
Returns an array containing all of the elements in this collection in the correct order. The returned array's runtime component type is Object. -
toArray
public <A> A[] toArray(A[] a) Returns an array containing all of the elements in this collection in the correct order; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.- Specified by:
toArrayin interfaceCollection<T>- Specified by:
toArrayin interfaceList<T>- Parameters:
a- the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing all of the elements in this collection in the correct order.
-
remove
Removes the specified element from this HashSet. If the HashSet does not contain the element, it is unchanged. More formally, removes the elementesuch thatObjects.equals(o, e)(if such an element exists). Returnstrueif this HashSet contained the specified element (or equivalently, if this HashSet changed as a result of the call). -
addAll
Adds all of the elements in the specified collection at the end of this HashSet, as if by callingaddLast(T)on each one, in the order that they are returned by the collection's iterator. -
removeAll
Removes all of this collection's elements that are also contained in the specified collection. After this call returns, this collection will contain no elements in common with the specified collection. -
retainAll
Retains only the elements in this collection that are contained in the specified collection. In other words, removes from this collection all of its elements that are not contained in the specified collection. -
clear
public void clear()Removes all of the elements from this collection. The collection will be empty after this method returns. -
addFirst
Inserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet). -
addLast
Inserts the specified element at the end of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet). -
offerFirst
Inserts the specified element at the front of this HashSet if it is possible to do so (ie, if the item is not already contained in this HashSet).- Specified by:
offerFirstin interfaceDeque<T>- Parameters:
item- the element to add- Returns:
trueif the element was added to this HashSet, elsefalse.
-
offerLast
Equivalent toadd(Object). -
pollFirst
Equivalent toremoveFirst(). -
pollLast
Equivalent toremoveLast(). -
peekFirst
Equivalent togetFirst(). -
peekLast
Equivalent togetLast(). -
removeFirstOccurrence
Equivalent toremove(Object)- Specified by:
removeFirstOccurrencein interfaceDeque<T>- Parameters:
o- element to be removed from this HashSet, if present- Returns:
trueif this HashSet contained the specified element.
-
removeLastOccurrence
Equivalent toremove(Object)- Specified by:
removeLastOccurrencein interfaceDeque<T>- Parameters:
o- element to be removed from this HashSet, if present- Returns:
trueif this HashSet contained the specified element.
-
offer
Equivalent toadd(Object). -
remove
Equivalent toremoveFirst(). -
poll
Equivalent toremoveFirst(). -
element
Equivalent togetFirst(). -
peek
Equivalent togetFirst(). -
push
Equivalent toaddFirst(T). -
pop
Equivalent toremoveFirst(). -
descendingIterator
Returns an iterator over the elements in this HashSet in reverse sequential order.- Specified by:
descendingIteratorin interfaceDeque<T>- Returns:
- an iterator over the elements in this HashSet in reverse sequential order.
-
clone
Creates and returns a shallow copy of this object. -
addAll
Unsupported method.- Specified by:
addAllin interfaceList<T>- Returns:
- N/A: this method isn't supported
- Throws:
UnsupportedOperationException
-
get
Returns the element at the specified position in this list. -
set
Unsupported method.- Specified by:
setin interfaceList<T>- Returns:
- N/A: this method isn't supported
- Throws:
UnsupportedOperationException
-
add
This method is unsupported, except for the case whenindex == 0 || index == size -
remove
Removes the element at the specified position in this list. -
indexOf
Returns the index of the first occurrence of the specified element in this list, or-1if this list does not contain the element. -
lastIndexOf
Equivalent toindexOf(java.lang.Object).- Specified by:
lastIndexOfin interfaceList<T>- Parameters:
o- element to search for- Returns:
- the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
-
listIterator
Returns a list iterator over the elements in this list (in proper sequence).- Specified by:
listIteratorin interfaceList<T>- Returns:
- a list iterator over the elements in this list (in proper sequence).
-
listIterator
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.- Specified by:
listIteratorin interfaceList<T>- Parameters:
index- index of the first element to be returned from the list iterator (by a call to next)- Returns:
- a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
-
subList
Returns a view of the portion of this list between the specifiedfromIndex,inclusive, andtoIndex, exclusive. (IffromIndexandtoIndexare equal, the returned list is empty.)
-