java map sorted 排序

public class TestMap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Map<String, Employee> staff = new HashMap<>();
		staff.put("222", new Employee("222", "zhang"));
		staff.put("333", new Employee("333", "wang"));
		staff.put("111", new Employee("111", "tao"));
		staff.put("555", new Employee("1555", "duan"));
		
		System.out.println(staff);
		
		for(Map.Entry<String, Employee> entry:staff.entrySet()){
			
			String key = entry.getKey();
			Employee value = entry.getValue();
			System.out.println("key=" + key + ",value = " + value);
		}
		
		List<Map.Entry<String, Employee>> test = new ArrayList<Map.Entry<String,Employee>>(staff.entrySet());
		Collections.sort(test, new Comparator<Map.Entry<String, Employee>>() {
			    public int compare(Map.Entry<String, Employee> o1,
			            Map.Entry<String, Employee> o2) {
			        return (o2.getKey().compareTo(o1.getKey()));
			    }
			});

		for (int i = 0; i < test.size(); i++) {  
            String id = test.get(i).toString();  
            System.out.print(id + "  ");  
        }  
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值