java.util
Interface Set
- Collection
- SortedSet
- AbstractSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
A collection that contains no duplicates. In other words, for two set
elements e1 and e2,
e1.equals(e2)
returns false. There
are additional stipulations on
add
,
equals
and
hashCode
, as well as the requirements that constructors
do not permit duplicate elements. The Set interface is incompatible with
List; you cannot implement both simultaneously.
Note: Be careful about using mutable objects in sets. In particular,
if a mutable object changes to become equal to another set element, you
have violated the contract. As a special case of this, a Set is not
allowed to be an element of itself, without risking undefined behavior.
- 1.2
Collection
, List
, SortedSet
, HashSet
, TreeSet
, LinkedHashSet
, AbstractSet
, Collections.singleton(Object)
, Collections.EMPTY_SET
boolean | add(Object o) - Adds the specified element to the set if it is not already present
(optional operation).
|
boolean | addAll(Collection c) - Adds all of the elements of the given collection to this set (optional
operation).
|
void | clear() - Removes all elements from this set (optional operation).
|
boolean | contains(Object o) - Returns true if the set contains the specified element.
|
boolean | containsAll(Collection c) - Returns true if this set contains all elements in the specified
collection.
|
boolean | equals(Object o) - Compares the specified object to this for equality.
|
int | hashCode() - Returns the hash code for this set.
|
boolean | isEmpty() - Returns true if the set contains no elements.
|
Iterator | iterator() - Returns an iterator over the set.
|
boolean | remove(Object o) - Removes the specified element from this set (optional operation).
|
boolean | removeAll(Collection c) - Removes from this set all elements contained in the specified collection
(optional operation).
|
boolean | retainAll(Collection c) - Retains only the elements in this set that are also in the specified
collection (optional operation).
|
int | size() - Returns the number of elements in the set.
|
Object[] | toArray() - Returns an array containing the elements of this set.
|
Object[] | toArray(Object[] a) - Returns an array containing the elements of this set, of the same runtime
type of the argument.
|
add , addAll , clear , contains , containsAll , equals , hashCode , isEmpty , iterator , remove , removeAll , retainAll , size , toArray , toArray |
add
public boolean add(Object o)
Adds the specified element to the set if it is not already present
(optional operation). In particular, the comparison algorithm is
o == null ? e == null : o.equals(e)
. Sets need not permit
all values, and may document what exceptions will be thrown if
a value is not permitted.
- add in interface Collection
o
- the object to add
- true if the object was not previously in the set
UnsupportedOperationException
- if this operation is not allowedClassCastException
- if the class of o prevents it from being addedIllegalArgumentException
- if some aspect of o prevents it from
being addedNullPointerException
- if null is not permitted in this set
addAll
public boolean addAll(Collection c)
Adds all of the elements of the given collection to this set (optional
operation). If the argument is also a Set, this returns the mathematical
union of the two. The behavior is unspecified if the set is
modified while this is taking place.
- addAll in interface Collection
c
- the collection to add
- true if the set changed as a result
UnsupportedOperationException
- if this operation is not allowedClassCastException
- if the class of an element prevents it from
being addedIllegalArgumentException
- if something about an element prevents
it from being addedNullPointerException
- if null is not permitted in this set, or
if the argument c is null
add(Object)
contains
public boolean contains(Object o)
Returns true if the set contains the specified element. In other words,
this looks for o == null ? e == null : o.equals(e)
.
- contains in interface Collection
o
- the object to look for
- true if it is found in the set
ClassCastException
- if the type of o is not a valid type
for this set.NullPointerException
- if o is null and this set doesn't
support null values.
equals
public boolean equals(Object o)
Compares the specified object to this for equality. For sets, the object
must be a set, the two must have the same size, and every element in
one must be in the other.
- equals in interface Collection
- equals in interface Object
o
- the object to compare to
- true if it is an equal set
hashCode
public int hashCode()
Returns the hash code for this set. In order to satisfy the contract of
equals, this is the sum of the hashcode of all elements in the set.
- hashCode in interface Collection
- hashCode in interface Object
- the sum of the hashcodes of all set elements
equals(Object)
isEmpty
public boolean isEmpty()
Returns true if the set contains no elements.
- isEmpty in interface Collection
- true if the set is empty
iterator
public Iterator iterator()
Returns an iterator over the set. The iterator has no specific order,
unless further specified.
- iterator in interface Collection
- a set iterator
remove
public boolean remove(Object o)
Removes the specified element from this set (optional operation). If
an element e exists, o == null ? e == null : o.equals(e)
,
it is removed from the set.
- remove in interface Collection
o
- the object to remove
- true if the set changed (an object was removed)
UnsupportedOperationException
- if this operation is not allowedClassCastException
- if the type of o is not a valid type
for this set.NullPointerException
- if o is null and this set doesn't allow
the removal of a null value.
size
public int size()
Returns the number of elements in the set. If there are more
than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is
the cardinality of the set.
- size in interface Collection
- the number of elements
toArray
public Object[] toArray()
Returns an array containing the elements of this set. If the set
makes a guarantee about iteration order, the array has the same
order. The array is distinct from the set; modifying one does not
affect the other.
- toArray in interface Collection
- an array of this set's elements
toArray(Object[])
toArray
public Object[] toArray(Object[] a)
Returns an array containing the elements of this set, of the same runtime
type of the argument. If the given set is large enough, it is reused,
and null is inserted in the first unused slot. Otherwise, reflection
is used to build a new array. If the set makes a guarantee about iteration
order, the array has the same order. The array is distinct from the set;
modifying one does not affect the other.
- toArray in interface Collection
a
- the array to determine the return type; if it is big enough
it is used and returned
- an array holding the elements of the set
ArrayStoreException
- if the runtime type of a is not a supertype
of all elements in the setNullPointerException
- if a is null
toArray()
Set.java -- A collection that prohibits duplicates
Copyright (C) 1998, 2001, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.