【Java】HashMap自定义排序

HashMap中的对象根据成员进行自定义排序

 Map是Java中最常用的存储对象的集合类之一,存储在HashMap中的对象在取出时是无序的,下文以示例介绍了如果对HashMap中存储的对象根据成员进行自定义排序。

应用说明:计算缓存对象的点击次数按次数排序输出。

1. 定义CacheObj类

定义了一个简单的对象,这个对象将存储在一个HashMap中,希望根据CacheObj中的整型成员变量cachedCounter进行排序输出。

  
/*
 * Created on 2003-3-5
 */
package com.cache;
/**  * @author Weidong *  */
public class CacheObj {
  int cachedCounter;
  Object cacheObj = null;
  /**
   * @return Returns the cacheObj.
   */
  public Object getCacheObj() {
    return cacheObj;
  }
  /**
   * @param cacheObj
   *          The cacheObj to set.
   */
  public void setCacheObj(Object cacheObj) {
    this.cacheObj = cacheObj;
  }
  /**
   * @return Returns the cachedCounter.
   */
  public int getCachedCounter() {
    return cachedCounter;
  }
  /**
   * @param cachedCounter
   *          The cachedCounter to set.
   */
  public void setCachedCounter(int cachedCounter) {
    this.cachedCounter = cachedCounter;
  }
}


 

2. 自定义HotItemComparator 

HotItemComparator实现了Comparator 接口, 实现的方法非常简单就是根据cachedCounter进行比较返回比较结果

正序:return obj1.getCachedCounter() - obj2.getCachedCounter();

倒序:return obj2.getCachedCounter() - obj1.getCachedCounter();

  
package com.cache;
import java.util.Comparator;
/**
 * @author Weidong 
 */
public final class HotItemComparator implements Comparator {
    /*
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
     */
    public int compare(Object arg0, Object arg1) {
        CacheObj obj1 = (CacheObj) arg0;
        CacheObj obj2 = (CacheObj) arg1;
        return obj1.getCachedCounter() - obj2.getCachedCounter();
    }
}


3. 测试用例

定义变量:public static final Comparator HOT_ITEM = new HotItemComparator(); 
将HashMap追加到List对象sortList中:sortList.addAll(caches.values()); 
对sorList应用HOT_ITEM排序:Collections.sort(sortList, HOT_ITEM);

package com.cache;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * @author Weidong
 */
public class TestBean {
    public static final Comparator HOT_ITEM = new HotItemComparator();
 
    public static void sortMap() {
        Map caches = new HashMap();
        CacheObj s1 = new CacheObj();
        s1.setCachedCounter(2);
        CacheObj s2 = new CacheObj();
        s2.setCachedCounter(3);
        CacheObj s3 = new CacheObj();
        s3.setCachedCounter(1);
        caches.put("1", s1);
        caches.put("2", s2);
        caches.put("3", s3);
 
        List sortList = new ArrayList();
        sortList.addAll(caches.values());
        Collections.sort(sortList, HOT_ITEM);
        Iterator iter = sortList.iterator();
        while (iter.hasNext()) {
            CacheObj s = (CacheObj) iter.next();
            System.out.println("counter is "+s.getCachedCounter());
        }
    }
    public static void main(String[] args) {
        sortMap();
    }
}

输出:文中用正序

counter is 1
counter is 2
counter is 3 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值