JAVA对HashMap按照key排序

直接上代码
package com.sun.test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Test {
		
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<Integer, Integer> map=new HashMap<>();
		map.put(1, 100);
		map.put(8, 200);
		map.put(3, 300);
		map.put(110, 11000);
		map.put(12, 1200);
		

		Object[] key_arr = map.keySet().toArray();     
		Arrays.sort(key_arr);     
		for  (Object key : key_arr) {
			int kk=(int)key;
			int vv=map.get(key);
			if(kk<vv){
				System.out.println(key);
			}
		} 
	}
}

输出结果为:

1

3

8

12

110

猜你喜欢

转载自blog.csdn.net/u014182497/article/details/62416738