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 + "  ");  
        }  
	}

}

你可能感兴趣的:(java map sorted 排序)