Class WeightSet<E>

java.lang.Object
net.apartium.cocoabeans.collect.WeightSet<E>
Type Parameters:
E - element type
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>

public class WeightSet<E> extends Object implements Set<E>
A set implementation with weighted keys This class is not thread safe.
  • Constructor Details

    • WeightSet

      public WeightSet()
      Constructors an empty weight set instance
    • WeightSet

      public WeightSet(WeightSet<E> weightSet)
      A copy constructor to create a new instance of WeightSet with same elements as given weight set
      Parameters:
      weightSet - given weight set
    • WeightSet

      public WeightSet(Map<? extends E,? extends Double> map)
      Construct a weight set instance from given map
      Parameters:
      map - weight set base map, consisting of elements as keys and weight as values
  • Method Details

    • put

      public OptionalDouble put(E element, double weight)
      Insert an element with given weight into the set
      Parameters:
      element - element to insert
      weight - weight of given element
      Returns:
      optional consisting of weight previously associated with given element, empty if element was not in the set
    • putAll

      public void putAll(Map<? extends E,? extends Double> map)
      Put all entries from given map into current instance. You should prefer putAll(WeightSet) instead of this method.
      Parameters:
      map - weight set base map, consisting of elements as keys and weight as values
      See Also:
    • putAll

      public void putAll(WeightSet<E> weightSet)
      Put all entries from given weight set instance into the current instance
      Parameters:
      weightSet - weight set to merge into the current one
      See Also:
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      See Also:
    • getWeight

      public OptionalDouble getWeight(E element)
      Get weight associated with given element
      Parameters:
      element - element
      Returns:
      weight of given element, or empty if not in set
      See Also:
    • getWeightOrDefault

      public double getWeightOrDefault(E element, double defaultValue)
      Get weight associated with given element
      Parameters:
      element - element
      defaultValue - default weight to return if element is not in set
      Returns:
      weight of given element, or given default value if element not in set
      See Also:
    • getPercentage

      public OptionalDouble getPercentage(E element)
      Get weight of given element as percentage of total weight in weight set
      Parameters:
      element - element
      Returns:
      weight of given element as %, or empty if element is not in set
    • values

      public Set<E> values()
      Get all elements in this weight set
      Returns:
      unmodifiable set consisting of all elements of this set
    • totalWeight

      public double totalWeight()
      Get total weight of all elements in set
      Returns:
      total weight of all elements in set
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface Set<E>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
    • iterator

      @NotNull public @NotNull Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
    • toArray

      @NotNull public @NotNull Object @NotNull [] toArray()
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
    • toArray

      @NotNull public <T> @NotNull T @NotNull [] toArray(T @NotNull [] ts)
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
    • add

      public boolean add(E element)
      You should prefer put(Object, double) over this method, since you can specify weight with it
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
    • containsAll

      public boolean containsAll(@NotNull @NotNull Collection<?> collection)
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface Set<E>
    • addAll

      public boolean addAll(@NotNull @NotNull Collection<? extends E> collection)
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface Set<E>
    • retainAll

      public boolean retainAll(@NotNull @NotNull Collection<?> collection)
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface Set<E>
    • removeAll

      public boolean removeAll(@NotNull @NotNull Collection<?> collection)
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface Set<E>
    • pickOne

      public E pickOne()
      Randomly pick one element from set based on their weight. The higher the weight the bigger chance it has of being the result.
      Returns:
      picked element, or null if list is empty
      See Also:
    • pickOne

      public E pickOne(Random random)
      Randomly pick one element from set based on their weight. The higher the weight the bigger chance it has of being the result.
      Parameters:
      random - random instance to provide random number
      Returns:
      picked element, or null if list is empty
      See Also:
    • pickMany

      public WeightSet<E> pickMany(int num)
      Randomly pick given number of elements from set based on their weight. The higher the weight the bigger chance it has of being in the resulting weightset.
      Parameters:
      num - number of elements to return
      Returns:
      a subset of this weight set with random picks
      See Also:
    • pickMany

      public WeightSet<E> pickMany(int num, Random random)
      Randomly pick given number of elements from set based on their weight. The higher the weight the bigger chance it has of being in the resulting weightset.
      Parameters:
      num - number of elements to return
      random - random instance to provide random number
      Returns:
      a subset of this weight set with random picks
      See Also: