public class WeightedSet
extends java.util.AbstractSet
implements java.io.Serializable
Inspred by the BioJava Distribution objects the WeightedSet is a map from a Key to a Weight. Unlike Distributions the Keys do not have to be Symbols. In the GA package the WeightedMap is useful for sampling Organisms according to their fitness.
When Symbols are added or their weights are set then the weights are internally normalized to 1
Constructor and Description |
---|
WeightedSet() |
Modifier and Type | Method and Description |
---|---|
boolean |
add(java.lang.Object o)
Adds a new
Object with a weight of zero. |
boolean |
addAll(java.util.Collection c) |
java.util.Map |
asMap()
Converts the Set to a map from key
Objects to Double
weights. |
void |
clear() |
boolean |
contains(java.lang.Object o) |
boolean |
containsAll(java.util.Collection c) |
protected double |
getTotalWeight()
The total weight that has been added to this Set.
|
double |
getWeight(java.lang.Object o)
Determines the normalized weight for
o |
boolean |
isEmpty() |
java.util.Iterator |
iterator()
Returns an unmodifiable iterator over the keys of the set.
|
boolean |
remove(java.lang.Object o) |
boolean |
retainAll(java.util.Collection c) |
java.lang.Object |
sample()
Randomly samples an
Object from the Set according
to its weight. |
void |
setWeight(java.lang.Object o,
double w)
Sets the weight of an
Object . |
int |
size() |
java.lang.Object[] |
toArray() |
public java.util.Map asMap()
Objects
to Double
weights.public java.lang.Object sample()
Object
from the Set
according
to its weight.public double getWeight(java.lang.Object o) throws java.util.NoSuchElementException
o
o
- the Object
you want to know the weight ofjava.util.NoSuchElementException
- if o
is not found in this setprotected double getTotalWeight()
public void setWeight(java.lang.Object o, double w)
Object
. If the Object
is
not in this Set
then it is added.o
- the Object
w
- the weight.java.lang.IllegalArgumentException
- if w
is < 0.0public boolean contains(java.lang.Object o)
contains
in interface java.util.Collection
contains
in interface java.util.Set
contains
in class java.util.AbstractCollection
public boolean remove(java.lang.Object o)
remove
in interface java.util.Collection
remove
in interface java.util.Set
remove
in class java.util.AbstractCollection
public boolean isEmpty()
isEmpty
in interface java.util.Collection
isEmpty
in interface java.util.Set
isEmpty
in class java.util.AbstractCollection
public boolean retainAll(java.util.Collection c)
retainAll
in interface java.util.Collection
retainAll
in interface java.util.Set
retainAll
in class java.util.AbstractCollection
public boolean add(java.lang.Object o)
Object
with a weight of zero. Equivalent to
setWeight(o, 0.0);add
in interface java.util.Collection
add
in interface java.util.Set
add
in class java.util.AbstractCollection
o
- the object to add.public int size()
size
in interface java.util.Collection
size
in interface java.util.Set
size
in class java.util.AbstractCollection
public boolean containsAll(java.util.Collection c)
containsAll
in interface java.util.Collection
containsAll
in interface java.util.Set
containsAll
in class java.util.AbstractCollection
public java.lang.Object[] toArray()
toArray
in interface java.util.Collection
toArray
in interface java.util.Set
toArray
in class java.util.AbstractCollection
public void clear()
clear
in interface java.util.Collection
clear
in interface java.util.Set
clear
in class java.util.AbstractCollection
public java.util.Iterator iterator()
iterator
in interface java.lang.Iterable
iterator
in interface java.util.Collection
iterator
in interface java.util.Set
iterator
in class java.util.AbstractCollection
public boolean addAll(java.util.Collection c)
addAll
in interface java.util.Collection
addAll
in interface java.util.Set
addAll
in class java.util.AbstractCollection