JAVA从Redis(存储类型为HashMap)取值并进行排序

得到x=XXX格式的X和XXX值(2)

redis存储数据格式是HashMap

1.通过ArrayList构造函数将map.entrySet()转换成list

List<Map.Entry<String,String>> list= new ArrayList<>(jedis.hgetAll(“redis的key值”).entrySet());

2.通过比较器进行比较排序
   Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
    	@Override
   public int compare(Map.Entry<String, String> entry1, Map.Entry<String, String> 			 entry2) {
    		int entry1key=Integer.parseInt(entry1.getKey());
    		int entry2key=Integer.parseInt(entry2.getKey());
    		if(entry1key == entry2key) {
    			return 0;
    		}else {
    			return entry1key>entry2key? 1 : -1 ;
    		}		
        }           
    });
3.将排序后的结果打印出来
		//System.out.println("排序后:");
		List resultList = new ArrayList<>();
		for (Map.Entry<String, String> entry : list) {
			//System.out.println(entry.getKey() + ":" + entry.getValue());
			Map m = new HashMap<>();
			String a= entry.getKey().toString();
			String b= entry.getValue().toString();
			m.put("a", a);
			m.put("b", b);	
			// 将排序后的值存入resultList
			resultList.add(m);			
		 }

猜你喜欢

转载自blog.csdn.net/qq_45525250/article/details/116302637