设为首页收藏本站language 语言切换
查看: 1387|回复: 0
收起左侧

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

[复制链接]
发表于 2010-2-20 13:56:09 | 显示全部楼层 |阅读模式
<p >HashMap中的对象根据成员进行自定义排序 <BR><BR>      Map是Java中最常用的存储对象的集合类之一,存储在HashMap中的对象在取出时是无序的,下文以示例介绍了如果对HashMap中存储的对象根据成员进行自定义排序。<BR><BR>应用说明:计算缓存对象的点击次数按次数排序输出。<BR><BR>1. 定义CacheObj类<BR><BR>定义了一个简单的对象,这个对象将存储在一个HashMap中,希望根据CacheObj中的整型成员变量cachedCounter进行排序输出。<BR><BR><BR>/*<BR> * Created on 2003-3-5<BR> */<BR>package com.cache;<BR>/**  * @author Weidong *  */<BR>public class CacheObj {<BR>  int cachedCounter;<BR>  Object cacheObj = null;<BR>  /**<BR>   * @return Returns the cacheObj.<BR>   */<BR>  public Object getCacheObj() {<BR>    return cacheObj;<BR>  }<BR>  /**<BR>   * @param cacheObj<BR>   *          The cacheObj to set.<BR>   */<BR>  public void setCacheObj(Object cacheObj) {<BR>    this.cacheObj = cacheObj;<BR>  }<BR>  /**<BR>   * @return Returns the cachedCounter.<BR>   */<BR>  public int getCachedCounter() {<BR>    return cachedCounter;<BR>  }<BR>  /**<BR>   * @param cachedCounter<BR>   *          The cachedCounter to set.<BR>   */<BR>  public void setCachedCounter(int cachedCounter) {<BR>    this.cachedCounter = cachedCounter;<BR>  }<BR>}<BR><BR><BR><BR><BR>2. 自定义HotItemComparator <BR><BR>HotItemComparator实现了Comparator 接口, 实现的方法非常简单就是根据cachedCounter进行比较返回比较结果<BR><BR>正序:return obj1.getCachedCounter() - obj2.getCachedCounter();<BR><BR>倒序:return obj2.getCachedCounter() - obj1.getCachedCounter();<BR><BR><BR>package com.cache;<BR>import java.util.Comparator;<BR>/**<BR> * @author Weidong <BR> */<BR>public final class HotItemComparator implements Comparator {<BR>    /*<BR>     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)<BR>     */<BR>    public int compare(Object arg0, Object arg1) {<BR>        CacheObj obj1 = (CacheObj) arg0;<BR>        CacheObj obj2 = (CacheObj) arg1;<BR>        return obj1.getCachedCounter() - obj2.getCachedCounter();<BR>    }<BR>}<BR><BR><BR>3. 测试用例<BR><BR>定义变量:public static final Comparator HOT_ITEM = new HotItemComparator(); <BR>将HashMap追加到List对象sortList中:sortList.addAll(caches.values()); <BR>对sorList应用HOT_ITEM排序:Collections.sort(sortList, HOT_ITEM);<BR><BR>package com.cache;<BR>import java.util.ArrayList;<BR>import java.util.Collections;<BR>import java.util.HashMap;<BR>import java.util.Iterator;<BR>import java.util.List;<BR>import java.util.Map;<BR>/**<BR> * @author Weidong<BR> */<BR>public class TestBean {<BR>    public static final Comparator HOT_ITEM = new HotItemComparator();<BR><BR>    public static void sortMap() {<BR>        Map caches = new HashMap();<BR>        CacheObj s1 = new CacheObj();<BR>        s1.setCachedCounter(2);<BR>        CacheObj s2 = new CacheObj();<BR>        s2.setCachedCounter(3);<BR>        CacheObj s3 = new CacheObj();<BR>        s3.setCachedCounter(1);<BR>        caches.put("1", s1);<BR>        caches.put("2", s2);<BR>        caches.put("3", s3);<BR><BR>        List sortList = new ArrayList();<BR>        sortList.addAll(caches.values());<BR>        Collections.sort(sortList, HOT_ITEM);<BR>        Iterator iter = sortList.iterator();<BR>        while (iter.hasNext()) {<BR>            CacheObj s = (CacheObj) iter.next();<BR>            System.out.println("counter is "+s.getCachedCounter());<BR>        }<BR>    }<BR>    public static void main(String[] args) {<BR>        sortMap();<BR>    }<BR>}<BR><BR>输出:文中用正序<BR><BR>counter is 1<BR>counter is 2<BR>counter is 3 <BR><FONT face="Times New Roman">(责任编辑:董建伟)</FONT><p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-6 15:48 , Processed in 0.361846 second(s), 22 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表