1 package waf.util;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.Iterator;
7 import java.util.LinkedHashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Map.Entry;
11
12 import waf.convert.Conv;
13
14 /**
15 *
16 * @author waf.wang
17 *
18 */
19 public class MapUtil
20 {
21
22 /**
23 * @param args
24 */
25 public static void main(String[] args)
26 {
27 // TODO Auto-generated method stub
28
29 }
30
31 @SuppressWarnings("unchecked")
32 public static void cut(Map srcMap,Map dstMap,Object key)
33 {
34 Object obj=srcMap.remove(key);
35 if(obj!=null)
36 {
37 dstMap.put(key,obj);
38 }
39 }
40
41 public static void print(Map map)
42 {
43 for(Object key:map.keySet())
44 {
45 System.out.println("key="+key+"; value="+map.get(key));
46 }
47 }
48
49 public static <ValueT> Map<String,ValueT> sortByKey(Map<String,ValueT> map)
50 {
51 Map<String,ValueT> ret=new LinkedHashMap<String,ValueT>();
52
53 List<String> keys=Conv.set2List(map.keySet());
54
55 Collections.sort(keys);
56
57 for(String key:keys)
58 {
59 ret.put(key,map.get(key));
60 }
61 keys.clear();
62 return ret;
63 }
64
65 public static <ValueT> Map<Integer,ValueT> sortByIntegerKey(Map<Integer,ValueT> map)
66 {
67 Map<Integer,ValueT> ret=new LinkedHashMap<Integer,ValueT>();
68
69 List<Integer> keys=Conv.set2List(map.keySet());
70
71 Collections.sort(keys);
72
73 for(Integer key:keys)
74 {
75 ret.put(key,map.get(key));
76 }
77 keys.clear();
78 return ret;
79 }
80
81 // public static String md5(Map<String,String> map)
82 // {
83 // String ret="";
84 //
85 // String str="";
86 // for (String key:map.keySet())
87 // {
88 // str+=key+":"+map.get(key)+";";
89 // }
90 // return ret;
91 // }
92
93 // public static Map<String,Long> sortByValue(Map<String,Long> map)
94 // {
95 // List<Map.Entry<String,Long>> wordFrenList=new ArrayList(map.entrySet());
96 //
97 // Collections.sort(wordFrenList,new Comparator<Map.Entry<String,Long>>()
98 // {
99 // public int compare(Map.Entry<String,Long> obj1,Map.Entry<String,Long>
100 // obj2)
101 // {
102 // return (int)(obj2.getValue()-obj1.getValue());
103 // }
104 // });
105 //
106 // return null;
107 // }
108
109 @SuppressWarnings("unchecked")
110 public static <KeyT> List<Map.Entry<KeyT,Long>> sortByLongValue(Map<KeyT,Long> map)
111 {
112 List<Map.Entry<KeyT,Long>> list=new ArrayList(map.entrySet());
113
114 Collections.sort(list,new Comparator<Map.Entry<KeyT,Long>>()
115 {
116 public int compare(Map.Entry<KeyT,Long> obj1,Map.Entry<KeyT,Long> obj2)
117 {
118 return (int)(obj2.getValue()-obj1.getValue());
119 }
120 });
121 return list;
122 }
123
124 @SuppressWarnings("unchecked")
125 public static <KeyT> List<Map.Entry<KeyT,Integer>> sortByIntegerValue(Map<KeyT,Integer> map)
126 {
127 List<Map.Entry<KeyT,Integer>> list=new ArrayList(map.entrySet());
128
129 Collections.sort(list,new Comparator<Map.Entry<KeyT,Integer>>()
130 {
131 public int compare(Map.Entry<KeyT,Integer> obj1,Map.Entry<KeyT,Integer> obj2)
132 {
133 return (int)(obj2.getValue()-obj1.getValue());
134 }
135 });
136 return list;
137 }
138
139 /** key改名,不创建新map,但是被改名的key的输出顺序会被更改 */
140 public static <ValueT> void renameKey(Map<String,ValueT> map,String oldKey,String newKey)
141 {
142 ValueT value=map.remove(oldKey);
143 if(value!=null)
144 {
145 map.put(newKey,value);
146 }
147 }
148
149 /** key改名,不创建新map,但是被改名的key的输出顺序会被更改 */
150 public static <ValueT> void renameKey(List<Map<String,ValueT>> list,String oldKey,String newKey)
151 {
152 for(Map<String,ValueT> map:list)
153 {
154 renameKey(map,oldKey,newKey);
155 }
156 }
157
158 public static String getKeyByValue(Map map,Object value)
159 {
160
161 String keys="";
162
163 Iterator it=map.entrySet().iterator();
164
165 while(it.hasNext())
166 {
167 Map.Entry entry=(Entry)it.next();
168
169 Object obj=entry.getValue();
170
171 if(obj!=null&&obj.equals(value))
172 {
173 keys=(String)entry.getKey();
174 break;
175 }
176 }
177 return keys;
178 }
179
180 @SuppressWarnings("unchecked")
181 public static void copy(Map src,Map dest,Object key)
182 {
183 dest.put(key,src.get(key));
184 }
185
186 @SuppressWarnings("unchecked")
187 public static void copy(Map src,Map dest,Object srcKey,Object dstKey)
188 {
189 dest.put(dstKey,src.get(srcKey));
190 }
191
192 public static void compare(Map<String,Object> map1,Map<String,Object> map2)
193 {
194 for(Map.Entry<String,Object> entry1:map1.entrySet())
195 {
196 // Object m1value=entry1.getValue()==null?"":entry1.getValue();
197 // Object m2value=map2.get(entry1.getKey())==null?"":map2.get(entry1.getKey());
198 if(!entry1.getValue().equals(map2.get(entry1.getValue())))
199 {
200 System.out.println(entry1.getKey()+","+entry1.getValue()+","+map2.get(entry1.getValue()));
201 }
202 }
203 }
204 }