edu.stanford.nlp.util
Class EntryValueComparator

java.lang.Object
  extended by edu.stanford.nlp.util.EntryValueComparator
All Implemented Interfaces:
Comparator

public class EntryValueComparator
extends Object
implements Comparator

Comparator designed for the values of Map entries. This is a somewhat hacked ("overloaded") version of a comparator. But the idea is that you want to be comparing two "values". There are three cases: what you have might either be a Map.Entry, which is compared based on its value, or it might be a key in the Map optionally associated with the comparator, and then comparison is done based on the associated value, or if neither of the above is true, the object is compared as being itself a value. Values must implement Comparable to be used. Options to the comparator allow sorting in normal or reversed order, and for sorting by unsigned magnitude (which works only if the values extend Number.

Example use (sorts Map of counts or Counter with highest first):

  Map counts = ... // Object key -> Integer/Double count
  List entries=new ArrayList(counts.entrySet());
  Collections.sort(entries, new EntryValueComparator(false));
  

Author:
Joseph Smarr (jsmarr@stanford.edu), Dan Klein, Christopher Manning (make it do compare properly)

Constructor Summary
EntryValueComparator()
          Constructs a new EntryValueComparator using ascending (normal) order that works on Map.Entry objects.
EntryValueComparator(boolean ascending)
          Constructs a new EntryValueComparator that will sort in the given order and works on Map.Entry objects.
EntryValueComparator(Map m)
          Constructs a new EntryValueComparator that will sort keys for the given Map in ascending (normal) order.
EntryValueComparator(Map m, boolean ascending)
          Constructs a new EmptyValueComparator to sort keys or entries of the given map in the given order.
EntryValueComparator(Map m, boolean ascending, boolean useMagnitude)
          Constructs a new EmptyValueComparator to sort keys or entries of the given map in the given order.
 
Method Summary
 int compare(Object o1, Object o2)
          Compares the values of the two given Map.Entry objects in the given order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

EntryValueComparator

public EntryValueComparator()
Constructs a new EntryValueComparator using ascending (normal) order that works on Map.Entry objects.


EntryValueComparator

public EntryValueComparator(boolean ascending)
Constructs a new EntryValueComparator that will sort in the given order and works on Map.Entry objects.


EntryValueComparator

public EntryValueComparator(Map m)
Constructs a new EntryValueComparator that will sort keys for the given Map in ascending (normal) order. It will also sort Map.Entry objects.


EntryValueComparator

public EntryValueComparator(Map m,
                            boolean ascending)
Constructs a new EmptyValueComparator to sort keys or entries of the given map in the given order. If m is non-null, this Comparator can be used to sort its keySet() as well as its entrySet(). Otherwise it can only be used on the entries, since there's no way to get the value for a given key.

Parameters:
m - Map whose keys are to be sorted, or null if Map.Entry objects will be sorted.
ascending - whether to sort in ascending (normal) order or descending (reverse) order. Ascending order is alphabetical, descending order puts higher numbers first.

EntryValueComparator

public EntryValueComparator(Map m,
                            boolean ascending,
                            boolean useMagnitude)
Constructs a new EmptyValueComparator to sort keys or entries of the given map in the given order. If m is non-null, this Comparator can be used to sort its keySet() as well as its entrySet(). Otherwise it can only be used on the entries, since there's no way to get the value for a given key.

Parameters:
m - Map whose keys are to be sorted, or null if Map.Entry objects will be sorted.
ascending - whether to sort in ascending (normal) order or descending (reverse) order. Ascending order is alphabetical, descending order puts higher numbers first.
useMagnitude - Sort values for magnitude (absolute value). This only works if the values stored implement Number, and their magnitudes are compared according to their double value.
Method Detail

compare

public int compare(Object o1,
                   Object o2)
Compares the values of the two given Map.Entry objects in the given order.

Specified by:
compare in interface Comparator
Returns:
0 if either object is not a Map.Entry or if their values cannot be compared.


Stanford NLP Group