com.myjavatools.lib.foundation
Class Maps

java.lang.Object
  extended by com.myjavatools.lib.foundation.Maps

public abstract class Maps
extends java.lang.Object


Constructor Summary
Maps()
           
 
Method Summary
static
<X,Y> java.util.Map<X,Y>
arrayToMap(java.lang.Object[] nameValuePairs)
          makes a Map from an array key-value pairs
static
<X,Y,Z> java.util.Map<X,Z>
compose(java.util.Map<X,Y> f, java.util.Map<Y,Z> g)
          composes two Maps (like functions, see Functions#compose)
static
<X,Y> java.util.Map<Y,X>
inverse(java.util.Map<X,Y> f)
          Inverses a Map
static
<X,Y> java.util.Collection<Y>
map(java.util.Map<X,Y> m, java.util.Collection<X> domain)
          Maps a Collection using Map.
static
<X,Y> java.lang.Iterable<Y>
map(java.util.Map<X,Y> m, java.lang.Iterable<X> iterable)
          Maps an Iterable using Map.
static
<X,Y> java.util.Iterator<Y>
map(java.util.Map<X,Y> m, java.util.Iterator<X> iterator)
          Maps an Iterator using Map.
static
<X,Y> java.util.List<Y>
map(java.util.Map<X,Y> m, java.util.List<X> domain)
          Maps a List using Map.
static
<X,Y> java.util.Set<X>
resolve(java.util.Map<X,Y> f, Y y)
          finds all map keys that map to a specified value
static
<X,Y> java.util.Map<X,Y>
restrict(java.util.Map<X,Y> map, java.util.Collection<X> keys)
          Restricts a Map to a Collection Resulting Map is a virtual map that has an intersection of map's keyset and keys as a keyset, and maps them to the same values as the original map does.
static
<X,Y> java.util.Map<Y,java.util.Set<X>>
revert(java.util.Map<X,Y> f)
          reverts a Map f, producing a new one that maps values of f to sets of keys of f
static
<X,Y> java.util.Map<X,Y>
toMap(java.util.Map.Entry<X,Y>... pairs)
          makes a Map from key-value pairs
static
<X,Y> java.util.Map<X,Y>
toMap(java.lang.Object... pairs)
          makes a Map from a vararg of key-value pairs
static
<X,Y> java.util.Map<X,Y>
toMap(X key, Y value)
          makes a singleton Map<X,Y> from a key-value pair
static
<X,Y> java.util.Map<X,Y>
toMap(X key1, Y value1, X key2, Y value2)
          makes a Map<X,Y> from two key-value pairs
static
<X,Y> java.util.Map<X,Y>
toMap(X key1, Y value1, X key2, Y value2, X key3, Y value3)
          makes a Map<X,Y> from three key-value pairs
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Maps

public Maps()
Method Detail

compose

public static <X,Y,Z> java.util.Map<X,Z> compose(java.util.Map<X,Y> f,
                                                 java.util.Map<Y,Z> g)
composes two Maps (like functions, see Functions#compose)

Parameters:
f - Map<X,Y> first Map
g - Map<Y,Z> second Map
Returns:
Map<X,Z> composition, x -> g(f(x));

Example:

Suppose we have the following:
Map<Integer,String> f = toMap(new Object[] {1, "one", 2, "two", 3, "three"});
Map<String,String> g = toMap(new String[] {"one", "uno", "two", "dos", "three", "tres"});

Then compose(f, g)
returns the same map as produced by
toMap(new Object[] {1, "uno", 2, "dos", 3, "tres"});


resolve

public static <X,Y> java.util.Set<X> resolve(java.util.Map<X,Y> f,
                                             Y y)
finds all map keys that map to a specified value

Parameters:
f - Map<X,Y>
y - Y - the key
Returns:
Set<X> - all X x such that y.equals(f(x)).

revert

public static <X,Y> java.util.Map<Y,java.util.Set<X>> revert(java.util.Map<X,Y> f)
reverts a Map f, producing a new one that maps values of f to sets of keys of f

Parameters:
f - Map<X,Y> original map
Returns:
Map<Y,Set<X>> resulting map: for each y it returns {x | y.equals(f(x))} This method skips any null keys and values.

Example:

Suppose we have the following:
Map<String,String> f = toMap(new String[] {"1", "odd", "2", "even", "3", "odd"});

Then revert(f)
returns the same map as produced by
Map<String,Collection<String>> g = new Map<String,Collection<String>>; g.put("even", new HashSet(Arrays.asList("2"))); g.put("odd", new HashSet(Arrays.asList("1", "3")));


inverse

public static <X,Y> java.util.Map<Y,X> inverse(java.util.Map<X,Y> f)
                                  throws java.lang.InstantiationException
Inverses a Map

Parameters:
f - Map<X,Y> to inverse, must be monomorphic (one-to-one)
Returns:
Map<Y,X> inverse to f

Example:

Suppose we have the following:
Map f = toMap(new String[] {"1", "one", "2", "two", "3", "three"});

Then inverse(f)
returns the same map as produced by
toMap(new String[] {"one", "1", "two", "2", "three", "3"});

Throws:
java.lang.InstantiationException - in case f is not one-to-one

map

public static final <X,Y> java.util.List<Y> map(java.util.Map<X,Y> m,
                                                java.util.List<X> domain)
Maps a List using Map. If Map m is considered as a map from its keys to its values, then for each x in domain m(x) is calculated and stored into resulting List

Parameters:
m - Map<X,Y> the map
domain - List<X> the domain list
Returns:
list List<Y> with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map m = new HashMap();
m.put("a", "ValueOfA"); m.put("b", "ValueOfB"); m.put("c", "ValueOfC");

Then map(m, Arrays.asList(new String[] {"b", "x", "b"})) returns a List that contains "ValueOfB", null, and "ValueOfB".


restrict

public static final <X,Y> java.util.Map<X,Y> restrict(java.util.Map<X,Y> map,
                                                      java.util.Collection<X> keys)
Restricts a Map to a Collection Resulting Map is a virtual map that has an intersection of map's keyset and keys as a keyset, and maps them to the same values as the original map does.

Parameters:
map - Map
keys - Collection
Returns:
Map

map

public static final <X,Y> java.util.Collection<Y> map(java.util.Map<X,Y> m,
                                                      java.util.Collection<X> domain)
Maps a Collection using Map. If Map m is considered as a map from its keys to its values, then for each x in domain m(x) is calculated and stored into resulting List

Parameters:
m - Map<X,Y> the map
domain - Colection<X> the domain list
Returns:
Collection<Y> collection consisting of the values of the map corresponding to the keys from domain.

Example:

Suppose we have the following: Map<String,String> m = new HashMap<String,String>();
m.put("a", "ValueOfA"); m.put("b", "ValueOfB"); m.put("c", "ValueOfC");

Then map(m, Arrays.asList(new String[] {"b", "x", "b"})) returns a Collection that contains just "ValueOfB".


map

public static final <X,Y> java.util.Iterator<Y> map(java.util.Map<X,Y> m,
                                                    java.util.Iterator<X> iterator)
Maps an Iterator using Map. If Map m is considered as a map from its keys to its values, then the iterator that map returns is the one that, for each x in iterator, returns m(x) is returned by next() call.

Parameters:
m - Map<X,Y> the map
iterator - Iterator<X> the domain iterator
Returns:
Iterator<Y> iterator with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map<String,String> m = new HashMap<String,String>();
m.put("a", "ValueOfA"); m.put("b", "ValueOfB"); m.put("c", "ValueOfC");

Then map(m, Arrays.asList(new String[] {"b", "x", "b"}).iterator()) returns an Iterator that consequently returns "ValueOfB", null, and "ValueOfB".


map

public static final <X,Y> java.lang.Iterable<Y> map(java.util.Map<X,Y> m,
                                                    java.lang.Iterable<X> iterable)
Maps an Iterable using Map. If Map m is considered as a map from its keys to its values, then the iterable that map returns is the one with the iterator that returns m.get(x) is returned by next() call for each x returned by next() of the iterator of the original Iterable.

Parameters:
m - Map<X,Y> the map
iterable - Iterable<X> the domain iterator
Returns:
Iterable<Y> iterable with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map<String,String> m = new HashMap<String,String>();
m.put("a", "ValueOfA"); m.put("b", "ValueOfB"); m.put("c", "ValueOfC");

Then map(m, Arrays.asList(new String[] {"b", "x", "b"})) returns an Iterable that contains "ValueOfB", null, and "ValueOfB".


toMap

public static <X,Y> java.util.Map<X,Y> toMap(X key,
                                             Y value)
makes a singleton Map<X,Y> from a key-value pair

Parameters:
key - X
value - Y
Returns:
Map<X,Y> that has just one key and its value

Example:
  • toMap("the key", "This is the value").get("the key"); returns "This is the value";

  • toMap

    public static <X,Y> java.util.Map<X,Y> toMap(X key1,
                                                 Y value1,
                                                 X key2,
                                                 Y value2)
    makes a Map<X,Y> from two key-value pairs

    Parameters:
    key1 - X first key
    value1 - Y first value
    key2 - X second key
    value2 - Y second value
    Returns:
    Map<X,Y> that has these two keys and values

    Example:
  • toMap(2, "kaksi", 3, "kolmi").get(3); returns "kolmi";

  • toMap

    public static <X,Y> java.util.Map<X,Y> toMap(X key1,
                                                 Y value1,
                                                 X key2,
                                                 Y value2,
                                                 X key3,
                                                 Y value3)
    makes a Map<X,Y> from three key-value pairs

    Parameters:
    key1 - X first key
    value1 - Y first value
    key2 - X second key
    value2 - Y second value
    key3 - X third key
    value3 - Y third value
    Returns:
    Map<X,Y> the map that contains these three keys and values

    Example:
  • toMap("1", "un", "2", "deux", "3", "troix").get("2"); returns "deux";

  • toMap

    public static <X,Y> java.util.Map<X,Y> toMap(java.util.Map.Entry<X,Y>... pairs)
    makes a Map from key-value pairs

    Parameters:
    pairs - Pair<X,Y>[] the array of key-value pairs
    Returns:
    Map the map that maps left pair elements from pairs array to right elements; if pairs is null, returns null

    Example:
  • toMap(new Pair<Integer, String>[] { new Pair<Integer, String>(1, "un"), new Pair<Integer, String>(2, "deux"), new Pair<Integer, String>(3, "troix")}).get(2); returns "deux";

  • toMap

    public static <X,Y> java.util.Map<X,Y> toMap(java.lang.Object... pairs)
    makes a Map from a vararg of key-value pairs

    Parameters:
    pairs - Object... odd elements of the array are keys, and even elements are values
    Returns:
    Map the map that maps odd elements from pairs array to even elements; if pairs is null, returns null

    Example:
  • toMap(new Integer(1), "un", new Integer(2), "deux", new Integer(3), "troix"}).get(2); returns "deux";
  • not recommended (but not deprecated) since 5.0 - use map(Pair<S,T>[] pairs) instead

    arrayToMap

    public static <X,Y> java.util.Map<X,Y> arrayToMap(java.lang.Object[] nameValuePairs)
    makes a Map from an array key-value pairs

    Parameters:
    nameValuePairs - Object[] odd elements of the array are keys, and even elements are values
    Returns:
    Map<X,Y> the map that maps odd elements from pairs array to even elements; if pairs is null, returns null

    Example:
  • arrayToMap(new Object[] {1, "un", 2, "deux", 3, "troix"}).get(2); returns "deux";
  • not recommended (but not deprecated) since 5.0 - use map(Pair<S,T>[] pairs) instead