Holger's
Java API

com.antelmann.util
Class Sets

java.lang.Object
  extended by com.antelmann.util.Sets

public class Sets
extends Object

utility class for calculations on sets, lists and other collections.

Author:
Holger Antelmann

Method Summary
static
<P,T> HashMap<P,BigDecimal>
aggregateBigDecimal(Map<T,BigDecimal> data, PatternExtractor<? super T,P> pattern)
           
static
<T,N extends Number>
HashMap<T,Double>
aggregatedDoubleValues(Map<T,N> data, int maxEntries, T remainingValueKey, Comparator<Map.Entry<T,N>> comparator)
           
static
<P,T> HashMap<P,Double>
aggregateDouble(Map<T,? extends Number> data, PatternExtractor<? super T,P> pattern)
           
static
<T> List<T>
asList(Collection<T> col)
          convenience method to display a collection in a JList for example
static
<T,N extends Number>
HashMap<T,Double>
bottomDoubleValues(Map<T,N> data, int maxEntries, T remainingValueKey)
          aggregates the given Map into one that only contains top values from the bottom.
static
<T> HashSet<T>
difference(Collection<? extends T> set1, Collection<? extends T> set2)
          returns a set that contains all elements from set1 that are not in set2
static
<T,P> HashSet<P>
distinctValues(Collection<T> col, PatternExtractor<? super T,P> extractor, Filter<? super T> filter)
           
static
<T> ArrayList<T>
extract(Collection<T> col, Filter<? super T> filter)
          returns a new Collection that will only contain those elements that passed the filter; the given col remains untouched.
static
<T> ArrayList<T>
filter(Collection<T> col, Filter<? super T> filter)
          filters all objects out of the given col that are not accepted by the filter; the dropout is returned as a separate Collection.
static TreeMap<Date,BigDecimal> groupAndFill(Map<Date,? extends Number> data, TimeInterval interval)
          groups the numbers based on dates so that there is one date per given interval; in addition, all missing invervals within the range are filled with BigDecimal.ZERO.
static
<T,P> HashMap<P,ArrayList<T>>
groupBy(Collection<T> col, PatternExtractor<? super T,P> extractor, Filter<? super T> filter)
          splits the given collection into many, where the extractor is the discriminator
static
<T> int
indexOf(T obj, Collection<T> col, Discriminator<T> d)
           
static
<K,V> K
keyForValue(Map<K,V> map, V value)
           
static
<K,V> Set<K>
keysForValue(Map<K,V> map, V value)
           
static
<T> HashSet<T>
makeSet(T[] array)
           
static
<T> HashSet<T>
makeSet(T[] array, boolean ignoreDuplicates)
           
static
<T,P> HashMap<P,T>
map(Collection<T> col, PatternExtractor<? super T,P> extractor, Filter<? super T> filter)
          if a single pattern is extracted more than once from the collection, simply the last occurrence is mapped to that pattern
static
<T> T
randomElement(Collection<T> col)
           
static
<K,V> HashMap<V,K>
reverseMap(Map<K,V> map)
          generates a reversed map, mapping values to keys
static boolean same(Map<?,?> m1, Map<?,?> m2)
           
static
<K,C,V extends Comparable<C>>
SortedMap<K,V>
sortByValues(Map<K,V> map)
          returns a sorted map that is initialized with the given map and sorted by the values
static
<K,C,V extends Comparable<C>>
SortedMap<K,V>
sortByValues(Map<K,V> map, boolean ascending)
          returns a sorted map that is initialized with the given map and sorted by the values
static
<K,V> SortedMap<K,V>
sortByValues(Map<K,V> map, Comparator<? super V> comparator)
          returns a sorted map that is initialized with the given map and sorted by the values
static
<T> HashMap<T,BigDecimal>
topBigDecimalValues(Map<T,? extends Number> data, int maxEntries, T remainderKey)
          converts the given Map into one that only contains top values.
static
<T,N extends Number>
HashMap<T,Double>
topDoubleValues(Map<T,N> data, int maxEntries, T remainingValueKey)
          aggregates the given Map into one that only contains top values.
static String toString(Collection<?> col)
           
static String toString(Map<?,?> map)
           
static void verifyCollection(Collection<?> col, Class<?> type)
          verifies that every element of the given collection is indeed assignable from the given type
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toString

public static String toString(Collection<?> col)

toString

public static String toString(Map<?,?> map)
See Also:
Strings.mapAsString(Map)

asList

public static <T> List<T> asList(Collection<T> col)
convenience method to display a collection in a JList for example

Returns:
the given collection itself if it already is a list or an ArrayList.

filter

public static <T> ArrayList<T> filter(Collection<T> col,
                                      Filter<? super T> filter)
filters all objects out of the given col that are not accepted by the filter; the dropout is returned as a separate Collection. The given col collection will contain only elements that passed the filter.


aggregateBigDecimal

public static <P,T> HashMap<P,BigDecimal> aggregateBigDecimal(Map<T,BigDecimal> data,
                                                              PatternExtractor<? super T,P> pattern)

aggregateDouble

public static <P,T> HashMap<P,Double> aggregateDouble(Map<T,? extends Number> data,
                                                      PatternExtractor<? super T,P> pattern)

topDoubleValues

public static <T,N extends Number> HashMap<T,Double> topDoubleValues(Map<T,N> data,
                                                                     int maxEntries,
                                                                     T remainingValueKey)
aggregates the given Map into one that only contains top values.

Parameters:
maxEntries - defines the maximum size of the returned Map
remainingValueKey - used to map all remaining values that are not top values; if null, the remaining values are simply dropped from the returned Map
Returns:
a HashMap that only contains the top values while the remainder is either left out or aggregated to a single entry.

bottomDoubleValues

public static <T,N extends Number> HashMap<T,Double> bottomDoubleValues(Map<T,N> data,
                                                                        int maxEntries,
                                                                        T remainingValueKey)
aggregates the given Map into one that only contains top values from the bottom.

Parameters:
maxEntries - defines the maximum size of the returned Map
remainingValueKey - used to map all remaining values that are not top values; if null, the remaining values are simply dropped from the returned Map
Returns:
a HashMap that only contains the top values while the remainder is either left out or aggregated to a single entry.

aggregatedDoubleValues

public static <T,N extends Number> HashMap<T,Double> aggregatedDoubleValues(Map<T,N> data,
                                                                            int maxEntries,
                                                                            T remainingValueKey,
                                                                            Comparator<Map.Entry<T,N>> comparator)

topBigDecimalValues

public static <T> HashMap<T,BigDecimal> topBigDecimalValues(Map<T,? extends Number> data,
                                                            int maxEntries,
                                                            T remainderKey)
converts the given Map into one that only contains top values.

Parameters:
maxEntries - defines the maximum size of the returned Map
remainderKey - used to map all remaining values that are not top values; if null, the remaining values are simply dropped from the returned Map
Returns:
a Map that only contains the top values while the remainder is either left out or aggregated to a single entry.

extract

public static <T> ArrayList<T> extract(Collection<T> col,
                                       Filter<? super T> filter)
returns a new Collection that will only contain those elements that passed the filter; the given col remains untouched. The filter may be null, in which case the returned list contains all elements.


groupBy

public static <T,P> HashMap<P,ArrayList<T>> groupBy(Collection<T> col,
                                                    PatternExtractor<? super T,P> extractor,
                                                    Filter<? super T> filter)
splits the given collection into many, where the extractor is the discriminator


distinctValues

public static <T,P> HashSet<P> distinctValues(Collection<T> col,
                                              PatternExtractor<? super T,P> extractor,
                                              Filter<? super T> filter)

map

public static <T,P> HashMap<P,T> map(Collection<T> col,
                                     PatternExtractor<? super T,P> extractor,
                                     Filter<? super T> filter)
if a single pattern is extracted more than once from the collection, simply the last occurrence is mapped to that pattern


groupAndFill

public static TreeMap<Date,BigDecimal> groupAndFill(Map<Date,? extends Number> data,
                                                    TimeInterval interval)
                                             throws IllegalArgumentException
groups the numbers based on dates so that there is one date per given interval; in addition, all missing invervals within the range are filled with BigDecimal.ZERO.

Throws:
IllegalArgumentException

reverseMap

public static <K,V> HashMap<V,K> reverseMap(Map<K,V> map)
generates a reversed map, mapping values to keys


keyForValue

public static <K,V> K keyForValue(Map<K,V> map,
                                  V value)

keysForValue

public static <K,V> Set<K> keysForValue(Map<K,V> map,
                                        V value)

sortByValues

public static <K,C,V extends Comparable<C>> SortedMap<K,V> sortByValues(Map<K,V> map)
returns a sorted map that is initialized with the given map and sorted by the values


sortByValues

public static <K,C,V extends Comparable<C>> SortedMap<K,V> sortByValues(Map<K,V> map,
                                                                        boolean ascending)
returns a sorted map that is initialized with the given map and sorted by the values


sortByValues

public static <K,V> SortedMap<K,V> sortByValues(Map<K,V> map,
                                                Comparator<? super V> comparator)
returns a sorted map that is initialized with the given map and sorted by the values


indexOf

public static <T> int indexOf(T obj,
                              Collection<T> col,
                              Discriminator<T> d)

makeSet

public static <T> HashSet<T> makeSet(T[] array)
                          throws IllegalArgumentException
Throws:
IllegalArgumentException - if the array contains duplicates

makeSet

public static <T> HashSet<T> makeSet(T[] array,
                                     boolean ignoreDuplicates)
                          throws IllegalArgumentException
Throws:
IllegalArgumentException - if the array contains duplicates and ignoreDuplicates is false

difference

public static <T> HashSet<T> difference(Collection<? extends T> set1,
                                        Collection<? extends T> set2)
returns a set that contains all elements from set1 that are not in set2


same

public static boolean same(Map<?,?> m1,
                           Map<?,?> m2)

verifyCollection

public static void verifyCollection(Collection<?> col,
                                    Class<?> type)
                             throws InconsistentContentException
verifies that every element of the given collection is indeed assignable from the given type

Parameters:
col - the collection to be verified
type - the class that is required to be a superclass of every element of the given collection
Throws:
InconsistentContentException - if one element is not a subclass of the given type

randomElement

public static <T> T randomElement(Collection<T> col)


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads