public class WeightedSet extends AbstractSet implements 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(Object o)
Adds a new
Object with a weight of zero. |
boolean |
addAll(Collection c) |
Map |
asMap()
Converts the Set to a map from key
Objects to Double
weights. |
void |
clear() |
boolean |
contains(Object o) |
boolean |
containsAll(Collection c) |
protected double |
getTotalWeight()
The total weight that has been added to this Set.
|
double |
getWeight(Object o)
Determines the normalized weight for
o |
boolean |
isEmpty() |
Iterator |
iterator()
Returns an unmodifiable iterator over the keys of the set.
|
boolean |
remove(Object o) |
boolean |
retainAll(Collection c) |
Object |
sample()
Randomly samples an
Object from the Set according
to its weight. |
void |
setWeight(Object o,
double w)
Sets the weight of an
Object . |
int |
size() |
Object[] |
toArray() |
equals, hashCode, removeAll
toArray, toString
public WeightedSet()
public Map asMap()
Objects
to Double
weights.public Object sample()
Object
from the Set
according
to its weight.public double getWeight(Object o) throws NoSuchElementException
o
o
- the Object
you want to know the weight ofNoSuchElementException
- if o
is not found in this setprotected double getTotalWeight()
public void setWeight(Object o, double w)
Object
. If the Object
is
not in this Set
then it is added.o
- the Object
w
- the weight.IllegalArgumentException
- if w
is < 0.0public boolean contains(Object o)
contains
in interface Collection
contains
in interface Set
contains
in class AbstractCollection
public boolean remove(Object o)
remove
in interface Collection
remove
in interface Set
remove
in class AbstractCollection
public boolean isEmpty()
isEmpty
in interface Collection
isEmpty
in interface Set
isEmpty
in class AbstractCollection
public boolean retainAll(Collection c)
retainAll
in interface Collection
retainAll
in interface Set
retainAll
in class AbstractCollection
public boolean add(Object o)
Object
with a weight of zero. Equivalent to
setWeight(o, 0.0);add
in interface Collection
add
in interface Set
add
in class AbstractCollection
o
- the object to add.public int size()
size
in interface Collection
size
in interface Set
size
in class AbstractCollection
public boolean containsAll(Collection c)
containsAll
in interface Collection
containsAll
in interface Set
containsAll
in class AbstractCollection
public Object[] toArray()
toArray
in interface Collection
toArray
in interface Set
toArray
in class AbstractCollection
public void clear()
clear
in interface Collection
clear
in interface Set
clear
in class AbstractCollection
public Iterator iterator()
iterator
in interface Iterable
iterator
in interface Collection
iterator
in interface Set
iterator
in class AbstractCollection
public boolean addAll(Collection c)
addAll
in interface Collection
addAll
in interface Set
addAll
in class AbstractCollection
Copyright © 2014 BioJava. All rights reserved.