package com.ren.HashMap;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test1 {
public static void main(String[] args) {
//最早最大遍历
Bean bean1 = new Bean("2016", "type1", "200");
Bean bean2 = new Bean("2016", "type2", "1000");
Bean bean3 = new Bean("2016", "type3", "100");
Bean bean4 = new Bean("2016", "type4", "2000");
Bean bean5 = new Bean("2017", "type1", "500");
Bean bean6 = new Bean("2017", "type2", "600");
Bean bean7 = new Bean("2017", "type3", "200");
Bean bean8 = new Bean("2017", "type4", "900");
HashMap hashMap = new HashMap();
hashMap.put(1, bean1);
hashMap.put(2, bean2);
hashMap.put(3, bean3);
hashMap.put(4, bean4);
hashMap.put(5, bean5);
hashMap.put(6, bean6);
hashMap.put(7, bean7);
hashMap.put(8, bean8);
Set> entrySet = hashMap.entrySet();
for(Entry entry : entrySet){
Integer key = entry.getKey();
Bean bean = entry.getValue();
System.out.println(key+","+bean.getTime()+","+bean.getType()+","+bean.getNum());
}
List> list = new LinkedList>();
list.addAll(hashMap.entrySet());
Collections.sort(list, new Comparator>() {
public int compare(Map.Entry obj1, Map.Entry obj2) {//从高往低排序
int num = Integer.parseInt(obj1.getValue().getTime())-Integer.parseInt(obj2.getValue().getTime());
int num2 = (num==0) ? (Integer.parseInt(obj2.getValue().getNum())-Integer.parseInt(obj1.getValue().getNum()))
: num;
return num2;
}
});
System.out.println("*********************");
for(Map.Entry entry : list){
Integer key = entry.getKey();
Bean bean = entry.getValue();
System.out.println(key+","+bean.getTime()+","+bean.getType()+","+bean.getNum());
}
}
}
package com.ren.HashMap;
public class Bean {
private String time;
private String type;
private String num;
public Bean(){
}
public Bean(String time,String type,String num){
this.time = time;
this.type = type;
this.num = num;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((num == null) ? 0 : num.hashCode());
result = prime * result + ((time == null) ? 0 : time.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bean other = (Bean) obj;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
if (time == null) {
if (other.time != null)
return false;
} else if (!time.equals(other.time))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
1,2016,type1,200
2,2016,type2,1000
3,2016,type3,100
4,2016,type4,2000
5,2017,type1,500
6,2017,type2,600
7,2017,type3,200
8,2017,type4,900
*********************
4,2016,type4,2000
2,2016,type2,1000
1,2016,type1,200
3,2016,type3,100
8,2017,type4,900
6,2017,type2,600
5,2017,type1,500
7,2017,type3,200