Advertisement
kntreadway

Untitled

Sep 15th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. public void sortMap(Map<String, GroupByStatsValues<Double, Double>> map) {
  2.  
  3.       long startTime = System.currentTimeMillis();
  4.  
  5.    
  6.      MaxValueComparator gbvc = new MaxValueComparator(map);
  7.       //CountValueComparator gbvc = new CountValueComparator(map);
  8.     TreeMap<String, GroupByStatsValues<Double, Double>> sorted_map = new TreeMap<String, GroupByStatsValues<Double, Double>>(
  9.         gbvc);
  10.     sorted_map.putAll(map);
  11.     long endTime = System.currentTimeMillis();
  12.  
  13.     System.out.println("time to sort: " + (endTime - startTime));
  14.     int i = 0;
  15.     for (String s : sorted_map.keySet()) {
  16.       if (i > 10) {
  17.         break;
  18.       } else {
  19.      
  20.  
  21.       double x = sorted_map.get(s).getMax();
  22.      
  23.       if (!sorted_map.containsKey(s)) {
  24.         System.out.println("yello");
  25.       } else {
  26.       System.out.println("key/value: " + s + "/"
  27.             + sorted_map.get(s).getCount());
  28.        
  29.         i++;
  30.       }
  31.      
  32.     }
  33.      
  34.     }
  35.     }
  36.    
  37.     class CountValueComparator implements Comparator {
  38.  
  39.       Map<String, GroupByStatsValues<Double, Double>> base;
  40.  
  41.     public CountValueComparator(
  42.         Map<String, GroupByStatsValues<Double, Double>> base) {
  43.       this.base = base;
  44.     }
  45.  
  46.     public int compare(Object a, Object b) {
  47.       String aStr = (String) a;
  48.       String bStr = (String) b;
  49.       if (((GroupByStatsValues<Double, Double>) base.get(aStr)).getCount() < ((GroupByStatsValues<Double, Double>) base
  50.           .get(bStr)).getCount()) {
  51.         return 1;
  52.       } else if (((GroupByStatsValues<Double, Double>) base.get(aStr))
  53.           .getCount() == ((GroupByStatsValues<Double, Double>) base
  54.           .get(bStr)).getCount()) {
  55.         return 0;
  56.       } else {
  57.         return -1;
  58.       }
  59.     }
  60.   }
  61.    
  62.      class MaxValueComparator implements Comparator {
  63.  
  64.     Map<String, GroupByStatsValues<Double, Double>> base;
  65.  
  66.     public MaxValueComparator(
  67.         Map<String, GroupByStatsValues<Double, Double>> base) {
  68.       this.base = base;
  69.     }
  70.  
  71.     public int compare(Object a, Object b) {
  72.       String aStr = (String) a;
  73.       String bStr = (String) b;
  74.       if (((GroupByStatsValues<Double, Double>) base.get(aStr)).getMax() < ((GroupByStatsValues<Double, Double>) base
  75.           .get(bStr)).getMax()) {
  76.         return 1;
  77.       } else if (((GroupByStatsValues<Double, Double>) base.get(aStr))
  78.           .getMax() == ((GroupByStatsValues<Double, Double>) base
  79.           .get(bStr)).getMax()) {
  80.         return 0;
  81.       } else {
  82.         return -1;
  83.       }
  84.     }
  85.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement