|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.util.AbstractCollection | +--java.util.AbstractSet | +--fcmodeler.jsomap.WeightedSet
A set in which each element has a corresponding real-valued weight. This class implements the
java.util.Set
interface and supports all of the normal set operations. Additionally,
each element in this set has a corresponding weight, of type double
. The weights are
obtained using the get
method, and altered using the set
method. Adding
an element(s) to this set using the add
or addAll
methods adds the
element(s) and assignes a weight of 1.0d
to it/them. Additionally, if the collection passed
to the addAll
method is of type WeightedSet
, that set is effectively copied
into this set, including its weights.
Constructor Summary | |
WeightedSet()
Creates a new empty WeightedSet . |
|
WeightedSet(java.util.Collection collection)
Creates a new WeightedSet containing the elements in the specified collection. |
Method Summary | |
boolean |
add(java.lang.Object obj)
Adds the specified object to this set if it is not already present and assigns it a weight of 1.0d . |
boolean |
addAll(java.util.Collection collection)
Adds all of the elements in the specified collection to this set if they're not already present. |
void |
clear()
Removes all elements from this set. |
boolean |
contains(java.lang.Object obj)
Returns true if this set contains the specified object. |
boolean |
equals(java.lang.Object obj)
Compares the specified object with this set for equality. |
double |
get(java.lang.Object obj)
Returns the weight associated with the specified object. |
int |
hashCode()
Returns the hash code value for this set. |
java.util.Iterator |
iterator()
Returns an iterator over the elements in this set. |
boolean |
remove(java.lang.Object obj)
Removes the specified element from this set if it is present. |
void |
set(java.lang.Object obj,
double weight)
Sets the weight of the specified object to the specified value. |
int |
size()
Returns the number of elements in this set. |
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this set. |
java.lang.Object[] |
toArray(java.lang.Object[] obj)
Returns an array containing all of the elements in this set whose runtime type is that of the specified array. |
java.lang.String |
toString()
Returns the string representation of this set. |
Methods inherited from class java.util.AbstractSet |
removeAll |
Methods inherited from class java.util.AbstractCollection |
containsAll, isEmpty, retainAll |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Set |
containsAll, isEmpty, retainAll |
Constructor Detail |
public WeightedSet()
WeightedSet
.public WeightedSet(java.util.Collection collection)
WeightedSet
containing the elements in the specified collection. If the
collection is of type WeightedSet
, all elements are assigned to their corresponding
weights; otherwise, all elements are assigned a weight of 1.0d
.collection
- the collection whose elements are to be placed in this set.Method Detail |
public boolean add(java.lang.Object obj)
1.0d
.add
in class java.util.AbstractCollection
obj
- the object to add to this set.true
if this set did not already contain the object.public boolean addAll(java.util.Collection collection)
WeightedSet
the elements are assigned their corresponding
weights; if not, they are assigned the weight 1.0d
.addAll
in class java.util.AbstractCollection
collection
- the collection whose elements are to be added to this set.true
if this set changed as a result of this call.public void clear()
clear
in class java.util.AbstractCollection
public boolean contains(java.lang.Object obj)
true
if this set contains the specified object.contains
in class java.util.AbstractCollection
obj
- the object to test for containment.true
if this set contains the specified object.public boolean equals(java.lang.Object obj)
true
if the specified
object is also a weighted set, the two sets have the same size, and every member of the specified set
is contained in this set and has the same weight.equals
in class java.util.AbstractSet
obj
- the object to be compared for equality with this set.true
if the specified object is equal to this set.public double get(java.lang.Object obj)
0.0d
if the
specified object is not contained in this set.obj
- the object whose weight is returned.public int hashCode()
Returns the hash code value for this set. The hash code of a weighted set is defined to be the sum of the hash codes of each element and it's associated weight. Specifically, the hash code of each element and its weight are combined as follows:
long bits = Double.doubleToLongBits(get(obj)); int temp = (int) (bits ^ (bits >>> 32)); result += obj.hashCode() ^ temp;
This is similar to the hash code used by java.util.Map.Entry
and uses ideas from
Bloch's book entitled Effective Java. It ensures that two weighted sets have the same hash value
if they contain the same elements with the same weights.
hashCode
in class java.util.AbstractSet
public java.util.Iterator iterator()
iterator
in class java.util.AbstractCollection
public boolean remove(java.lang.Object obj)
remove
in class java.util.AbstractCollection
obj
- the object to be removed from this set.true
if this set contained the specified object.public void set(java.lang.Object obj, double weight)
0.0d
, the specified object is removed from this set unless this set is being iterated
over (i.e. the iterator
method was called). If this set is being iterated over, the
object's weight is immediately set to 0.0 and the object is then removed from this set once the
iteration is finished (i.e. the Iterator.hasNext
method returns false
).obj
- the object whose weight is to be adjusted.weight
- the new weight of the specified object.public int size()
size
in class java.util.AbstractCollection
public java.lang.Object[] toArray()
toArray
in class java.util.AbstractCollection
public java.lang.Object[] toArray(java.lang.Object[] obj)
toArray
in class java.util.AbstractCollection
obj
- the array into which the elements of this set are to be stored, if it is big enough; otherwise,
a new array of the same runtime type is allocated for this purpose.public java.lang.String toString()
toString
in class java.util.AbstractCollection
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |