标签:
需求:
/**
* Map进行多条件排序输出
* 水果具有好吃不好吃难吃属性。
* 入口Map
* 首先按照好吃不好吃难吃排序
* 然后按照水果的标志Id排序
* 出口Map
*
* 2016年8月14日
*/
1 import java.util.ArrayList;
2 import java.util.Collections;
3 import java.util.Comparator;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.TreeMap;
11
12 /**
13 * Map进行多条件排序输出
14 * 水果具有好吃不好吃难吃属性。
15 * 入口Map
16 * 首先按照好吃不好吃难吃排序
17 * 然后按照水果的标志Id排序
18 * 出口Map
19 *
20 * 2016年8月14日
21 */
22 public class MainSort {
23
24 /**
25 * 主方法
26 * @param args
27 */
28 public static void main(String[] args) {
29 // 获取Map
30 Map<String,Fruit> fruitMap = getFruitMap();
31 // 打印未排序的Map
32 show(fruitMap);
33 System.out.println("-----------before-----------");
34 // 打印排序完了的Map
35 show(MainSort.sortMapByValue(fruitMap));
36 System.out.println("-----------after------------");
37 }
38
39 /**
40 * 循环打印Map
41 */
42 private static void show(Map<String, Fruit> fruitMap) {
43 // 循环Map
44 for (Map.Entry<String, Fruit> fruitOneMap : fruitMap.entrySet()) {
45 Fruit fruit = fruitOneMap.getValue();
46 System.out.println(fruit.getFruitName() + " " + fruit.getFruitTaste()+ " " + fruit.getId() );
47 }
48 }
49
50 /**
51 * 准备参数
52 * @return
53 */
54 private static Map<String, Fruit> getFruitMap() {
55 Map<String,Fruit> fruitMap = new TreeMap<>();
56
57 // 创建对象
58 Fruit b = new Fruit("苹果" , FruitTaste.HAOCHI, "b");
59 Fruit a = new Fruit("梨子" , FruitTaste.YIBAN , "a");
60 Fruit c = new Fruit("香蕉" , FruitTaste.HAOCHI, "c");
61 Fruit d = new Fruit("桃子" , FruitTaste.HAOCHI, "d");
62 Fruit e = new Fruit("橘子" , FruitTaste.NANGCHI , "e");
63 Fruit f = new Fruit("杨梅" , FruitTaste.YIBAN , "f");
64 Fruit g = new Fruit("黑李" , FruitTaste.NANGCHI , "g");
65 Fruit h = new Fruit("火龙果", FruitTaste.HAOCHI, "h");
66 Fruit i = new Fruit("西瓜" , FruitTaste.YIBAN, "i");
67
68 // 乱序key值
69 fruitMap.put("xniem", b);
70 fruitMap.put("asdf", i);
71 fruitMap.put("akgjjd", c);
72 fruitMap.put("uiooo", f);
73 fruitMap.put("qw84", a);
74 fruitMap.put("5845uasdf‘", d);
75 fruitMap.put("48aisdf", e);
76 fruitMap.put("458kljsf", g);
77 fruitMap.put("4jalsdfj", h);
78
79 return fruitMap;
80 }
81
82 /**
83 * 由于List能够直接使用Collections进行排序
84 * 但是Map不行。
85 * 这边所做的操作就是先将Map--》List
86 * 然后对List进行排序
87 * 然后在讲List--》转换成Map
88 * @param map
89 * @return
90 */
91 public static Map<String, Fruit> sortMapByValue(Map<String, Fruit> fruitMap) {
92 if (fruitMap == null || fruitMap.isEmpty()) {
93 return null;
94 }
95 // LinkedhashMap是有序的、或者TreeMap都是有序的
96 Map<String, Fruit> sortedMap = new LinkedHashMap<String, Fruit>();
97 List<Map.Entry<String, Fruit>> entryList = new ArrayList<Map.Entry<String, Fruit>>(fruitMap.entrySet());
98 Collections.sort(entryList, new MapValueComparator());
99 Iterator<Map.Entry<String, Fruit>> iter = entryList.iterator();
100 Map.Entry<String, Fruit> tmpEntry = null;
101 while (iter.hasNext()) {
102 tmpEntry = iter.next();
103 sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
104 }
105 return sortedMap;
106 }
107 }
108
109
110
111 /**
112 * 比较器规则类
113 * 传入对应的Map进行比较
114 * @author elcapitan
115 */
116 class MapValueComparator implements Comparator<Map.Entry<String, Fruit>> {
117
118 @Override
119 public int compare(Entry<String, Fruit> o1, Entry<String, Fruit> o2) {
120
121 // 获取比较的两个对象
122 Fruit fruit1 = o1.getValue();
123 Fruit fruit2 = o2.getValue();
124
125 // 将好吃,难吃映射成具有比较关系的字符1、2、3
126 Map<String,Integer> tasteLev = new HashMap<>();
127 tasteLev.put(FruitTaste.HAOCHI, 1);
128 tasteLev.put(FruitTaste.YIBAN, 2);
129 tasteLev.put(FruitTaste.NANGCHI, 3);
130
131 int cr = 0;
132 // 判断好吃不好吃
133 int a = tasteLev.get(fruit2.getFruitTaste())-tasteLev.get(fruit1.getFruitTaste());
134 if (a!=0) {
135 cr = (a>0) ? -1 : 2;
136 } else {
137 // 按照对应的Id排序
138 a = fruit2.getId().compareTo(fruit1.getId());
139 if (a!=0) {
140 cr = (a>0)? -2 : 1;
141 }
142 }
143 /* 注意上面对一个返回值对应的就是形成比较层次
144 * 上层
145 * --> 2
146 * --> -1
147 * 下层
148 * --> 1
149 * --> -2
150 */
151 return cr;
152 }
153 }
154
155 /**
156 * 水果实体类
157 * 包含水果名称、水果好吃程度、水果标志
158 * @author elcapitan
159 */
160 class Fruit {
161 private String fruitName;
162 private String fruitTaste;
163 private String id;
164 public String getFruitName() {
165 return fruitName;
166 }
167 public void setFruitName(String fruitName) {
168 this.fruitName = fruitName;
169 }
170 public String getFruitTaste() {
171 return fruitTaste;
172 }
173 public void setFruitTaste(String fruitTaste) {
174 this.fruitTaste = fruitTaste;
175 }
176 public String getId() {
177 return id;
178 }
179 public void setId(String id) {
180 this.id = id;
181 }
182 public Fruit(String fruitName, String fruitTaste, String id) {
183 super();
184 this.fruitName = fruitName;
185 this.fruitTaste = fruitTaste;
186 this.id = id;
187 }
188 }
189
190 /**
191 * 水果好吃程度常量类
192 * 包括好吃、一般、不好吃
193 * 可以继续添加
194 * @author elcapitan
195 */
196 class FruitTaste {
197 public static final String HAOCHI = "好吃";
198 public static final String NANGCHI = "难吃";
199 public static final String YIBAN = "一般";
200 }
实现结果:
希望帮你不加班~??????
标签:
原文地址:http://www.cnblogs.com/fly-piglet/p/5785899.html