edu.stanford.nlp.util
Class CollectionUtils

java.lang.Object
  extended by edu.stanford.nlp.util.CollectionUtils

public class CollectionUtils
extends Object

Collection of useful static methods for working with Collections. Includes methods to increment counts in maps and cast list/map elements to common types.

Author:
Joseph Smarr (jsmarr@stanford.edu)

Method Summary
static List asList(double[] a)
           
static List asList(int[] a)
           
static List asList(Object... args)
          Returns a new List containing the specified objects.
static Set asSet(Object[] o)
          Returns a new Set containing all the objects in the specified array.
static int compareLists(List<? extends Comparable> list1, List<? extends Comparable> list2)
           
static boolean containsObject(Collection c, Object o)
          Checks whether a Collection contains a specified Object.
static boolean getBoolean(List list, int index)
          Returns ((Boolean)list.get(index)).booleanValue().
static boolean getBoolean(Map map, Object key)
          Returns ((Boolean)map.get(key)).booleanValue().
static double getdouble(List list, int index)
          Returns ((Double)list.get(index)).doubleValue().
static Double getDouble(List list, int index)
          Returns (Double)list.get(index).
static double getdouble(Map map, Object key)
          Returns ((Double)map.get(key)).doubleValue().
static Double getDouble(Map map, Object key)
          Returns (Double)map.get(key).
static int getIndex(List l, Object o)
          Returns the index of the first occurrence in the list of the specified object, using object identity (==) not equality as the criterion for object presence.
static int getInt(List list, int index)
          Returns ((Integer)list.get(index)).intValue().
static int getInt(Map list, Object key)
          Returns ((Integer)map.get(key)).intValue().
static Integer getInteger(List list, int index)
          Returns (Integer)list.get(index).
static Integer getInteger(Map map, Object key)
          Returns (Integer)map.get(key).
static
<C extends Comparable>
Comparator<List<C>>
getListComparator()
           
static Map getMapFromString(String s, Class keyClass, Class valueClass, MapFactory mapFactory)
           
static String getString(List list, int index)
          Returns (String)list.get(index).
static String getString(Map map, Object key)
          Returns (String)map.get(key).
static boolean incrementCount(Map map, Object key)
          Increments the Integer count of the given key in the given Map by 1.
static boolean incrementCount(Map map, Object key, int delta)
          Adds the given delta to the Integer value stored for the given key in the given Map.
static boolean isSubList(List l1, List l)
          Returns true iff l1 is a sublist of l (i.e., every member of l1 is in l, and for every e1 < e2 in l1, there is an e1 < e2 occurrence in l).
static Collection loadCollection(File file, Class c, CollectionFactory cf)
           
static Collection loadCollection(String filename, Class c, CollectionFactory cf)
           
static void main(String[] args)
           
static
<T> List<T>
makeList(T e)
          Returns a new List containing the given object.
static
<T> List<T>
makeList(T e1, T e2)
          Returns a new List containing the given objects.
static
<T> List<T>
makeList(T e1, T e2, T e3)
          Returns a new List containing the given objects.
static boolean removeObject(List l, Object o)
          Removes the first occurrence in the list of the specified object, using object identity (==) not equality as the criterion for object presence.
static
<E> Collection<E>
sampleWithoutReplacement(Collection<E> c, int n)
          Samples without replacement from a collection.
static
<E> Collection<E>
sampleWithoutReplacement(Collection<E> c, int n, Random r)
          Samples without replacement from a collection, using your own Random number generator.
static
<E> Collection<E>
sampleWithReplacement(Collection<E> c, int n)
          Samples with replacement from a collection
static
<E> Collection<E>
sampleWithReplacement(Collection<E> c, int n, Random r)
          Samples with replacement from a collection, using your own Random number generator
static String toVerticalString(Map m)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

incrementCount

public static boolean incrementCount(Map map,
                                     Object key,
                                     int delta)
Adds the given delta to the Integer value stored for the given key in the given Map. Returns whether the entry had to be created (i.e., it wasn't in the map), in which case it's inserted with delta as the initial value.

Parameters:
map - Map from keys to Integer values representing key counts
key - key in map for Integer to increment
delta - amount to change Integer value count by
Returns:
whether a new entry for the given key was created in the process (i.e., if it wasn't in the map before).

incrementCount

public static boolean incrementCount(Map map,
                                     Object key)
Increments the Integer count of the given key in the given Map by 1.

See Also:
incrementCount(Map,Object,int)

getString

public static String getString(List list,
                               int index)
Returns (String)list.get(index).


getInteger

public static Integer getInteger(List list,
                                 int index)
Returns (Integer)list.get(index).


getInt

public static int getInt(List list,
                         int index)
Returns ((Integer)list.get(index)).intValue().


getDouble

public static Double getDouble(List list,
                               int index)
Returns (Double)list.get(index).


getdouble

public static double getdouble(List list,
                               int index)
Returns ((Double)list.get(index)).doubleValue().


getBoolean

public static boolean getBoolean(List list,
                                 int index)
Returns ((Boolean)list.get(index)).booleanValue().


getString

public static String getString(Map map,
                               Object key)
Returns (String)map.get(key).


getInteger

public static Integer getInteger(Map map,
                                 Object key)
Returns (Integer)map.get(key).


getInt

public static int getInt(Map list,
                         Object key)
Returns ((Integer)map.get(key)).intValue().


getDouble

public static Double getDouble(Map map,
                               Object key)
Returns (Double)map.get(key).


getdouble

public static double getdouble(Map map,
                               Object key)
Returns ((Double)map.get(key)).doubleValue().


getBoolean

public static boolean getBoolean(Map map,
                                 Object key)
Returns ((Boolean)map.get(key)).booleanValue().


asList

public static List asList(int[] a)

asList

public static List asList(double[] a)

asList

public static List asList(Object... args)
Returns a new List containing the specified objects.


makeList

public static <T> List<T> makeList(T e)
Returns a new List containing the given object.


makeList

public static <T> List<T> makeList(T e1,
                                   T e2)
Returns a new List containing the given objects.


makeList

public static <T> List<T> makeList(T e1,
                                   T e2,
                                   T e3)
Returns a new List containing the given objects.


asSet

public static Set asSet(Object[] o)
Returns a new Set containing all the objects in the specified array.


loadCollection

public static Collection loadCollection(String filename,
                                        Class c,
                                        CollectionFactory cf)
                                 throws Exception
Parameters:
filename - the path to the file to load the List from
c - the Class to instantiate each member of the List. Must have a String constructor.
cf -
Returns:
Throws:
Exception

loadCollection

public static Collection loadCollection(File file,
                                        Class c,
                                        CollectionFactory cf)
                                 throws Exception
Parameters:
file - the file to load the List from
c - the Class to instantiate each member of the List. Must have a String constructor.
Returns:
Throws:
Exception

getMapFromString

public static Map getMapFromString(String s,
                                   Class keyClass,
                                   Class valueClass,
                                   MapFactory mapFactory)
                            throws ClassNotFoundException,
                                   NoSuchMethodException,
                                   IllegalAccessException,
                                   InvocationTargetException,
                                   InstantiationException
Throws:
ClassNotFoundException
NoSuchMethodException
IllegalAccessException
InvocationTargetException
InstantiationException

containsObject

public static boolean containsObject(Collection c,
                                     Object o)
Checks whether a Collection contains a specified Object. Object equality (==), rather than .equals(), is used.


removeObject

public static boolean removeObject(List l,
                                   Object o)
Removes the first occurrence in the list of the specified object, using object identity (==) not equality as the criterion for object presence. If this list does not contain the element, it is unchanged.

Parameters:
l - The List from which to remove the object
o - The object to be removed.
Returns:
Whether or not the List was changed.

getIndex

public static int getIndex(List l,
                           Object o)
Returns the index of the first occurrence in the list of the specified object, using object identity (==) not equality as the criterion for object presence. If this list does not contain the element, return -1.

Parameters:
l - The List to find the object in.
o - The sought-after object.
Returns:
Whether or not the List was changed.

sampleWithoutReplacement

public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c,
                                                         int n)
Samples without replacement from a collection.

Parameters:
c - The collection to be sampled from
n - The number of samples to take
Returns:
a new collection with the sample

sampleWithoutReplacement

public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c,
                                                         int n,
                                                         Random r)
Samples without replacement from a collection, using your own Random number generator.

Parameters:
c - The collection to be sampled from
n - The number of samples to take
r - the random number generator
Returns:
a new collection with the sample

sampleWithReplacement

public static <E> Collection<E> sampleWithReplacement(Collection<E> c,
                                                      int n)
Samples with replacement from a collection

Parameters:
c - The collection to be sampled from
n - The number of samples to take
Returns:
a new collection with the sample

sampleWithReplacement

public static <E> Collection<E> sampleWithReplacement(Collection<E> c,
                                                      int n,
                                                      Random r)
Samples with replacement from a collection, using your own Random number generator

Parameters:
c - The collection to be sampled from
n - The number of samples to take
r - the random number generator
Returns:
a new collection with the sample

isSubList

public static boolean isSubList(List l1,
                                List l)
Returns true iff l1 is a sublist of l (i.e., every member of l1 is in l, and for every e1 < e2 in l1, there is an e1 < e2 occurrence in l).

Parameters:
l -
l1 -
Returns:

toVerticalString

public static String toVerticalString(Map m)

compareLists

public static int compareLists(List<? extends Comparable> list1,
                               List<? extends Comparable> list2)

getListComparator

public static <C extends Comparable> Comparator<List<C>> getListComparator()

main

public static void main(String[] args)


Stanford NLP Group