Diff of /util/CollectionsEx.java [000000] .. [4fba4e]

Switch to unified view

a b/util/CollectionsEx.java
1
package util;
2
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Comparator;
6
import java.util.HashMap;
7
import java.util.LinkedHashMap;
8
import java.util.LinkedList;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Set;
12
13
/**
14
 * Provides additional methods for collection operation
15
 * @author zhengc
16
 *
17
 */
18
public class CollectionsEx {
19
    @SuppressWarnings("hiding")
20
    /**
21
     * Returns sorted HashMap by descend Value
22
     * @param map the original HashMap 
23
     * @return sorted HashMap by descend Value
24
     */
25
    public static <String, Double extends Comparable<? super Double>> Map<String, Double> sortByValueR( Map<String, Double> map ) {
26
        /* sort map by value reversed*/
27
        List<Map.Entry<String, Double>> list = new LinkedList<>(map.entrySet());
28
        Collections.sort(list, Collections.reverseOrder(new Comparator<Map.Entry<String, Double>>() {
29
            
30
            public int compare( Map.Entry<String, Double> o1, Map.Entry<String, Double> o2 ) {
31
                return ( o1.getValue() ).compareTo( o2.getValue() );
32
            }
33
        }) );
34
        
35
36
        Map<String, Double> result = new LinkedHashMap<>();
37
        for (Map.Entry<String, Double> entry : list){
38
            result.put( entry.getKey(), entry.getValue() );
39
        }
40
        return result;
41
    }
42
    
43
    @SuppressWarnings("hiding")
44
    /**
45
     * Returns sorted HashMap by Value
46
     * @param map the original HashMap 
47
     * @return sorted HashMap by Value
48
     */
49
    public static <String, Double extends Comparable<? super Double>> Map<String, Double> sortByValue( Map<String, Double> map ) {
50
        /* sort map by value*/
51
        List<Map.Entry<String, Double>> list = new LinkedList<>(map.entrySet());
52
        Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
53
            
54
            public int compare( Map.Entry<String, Double> o1, Map.Entry<String, Double> o2 ) {
55
                return ( o1.getValue() ).compareTo( o2.getValue() );
56
            }
57
        } );
58
        
59
60
        Map<String, Double> result = new LinkedHashMap<>();
61
        for (Map.Entry<String, Double> entry : list){
62
            result.put( entry.getKey(), entry.getValue() );
63
        }
64
        return result;
65
    }
66
    
67
//  public static Map<String, String> sortByKey(Map<String, String> map ) {
68
//      /* sort map by key*/
69
//      Map<String, String> sortedmap = new HashMap<String, String>();
70
//      Set<String> keys = map.keySet();
71
//      List<String> key_list = setToList(keys);
72
//      Collections.sort(key_list);
73
//      
74
//      for(String key : key_list) {
75
//          System.out.println(key);
76
//          sortedmap.put(key, map.get(key));
77
//      }
78
//      return sortedmap;
79
//  }
80
    
81
    /**
82
     * Creates a list from a set
83
     * @param set a set
84
     * @return a list
85
     */
86
    public static List<String> setToList(Set<String> set) {
87
        List<String> list = new ArrayList<String>();
88
        for (String s: set) {
89
            list.add(s);
90
        }
91
        Collections.sort(list);
92
        return list;
93
    }
94
95
}