CountSortTool

本文介绍了一个使用Java实现的计数排序工具类。该工具利用Apache Commons Collections库中的Bag接口来收集并统计不同元素出现的次数,然后通过自定义的Sort类对这些元素进行排序。Sort类实现了Comparable接口,使得元素可以根据它们出现的频率进行比较。
java 代码
  1. package org.jh.app.count;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.Collections;   
  5. import java.util.List;   
  6. import java.util.Set;   
  7.   
  8. import org.apache.commons.collections.Bag;   
  9. import org.apache.commons.collections.BagUtils;   
  10. import org.apache.commons.collections.bag.HashBag;   
  11.   
  12. public class CountSortTool {   
  13.     static public class Sort implements Comparable<Sort> {   
  14.         Object key;   
  15.   
  16.         int num = 1;   
  17.   
  18.         Sort(Object key) {   
  19.             this.key = key;   
  20.         }   
  21.   
  22.         public Sort(Object o, int count) {   
  23.             this.key = o;   
  24.             num = count;   
  25.         }   
  26.   
  27.         public String toString() {   
  28.             return key + ":" + num + "\n";   
  29.         }   
  30.   
  31.         public boolean equals(Object obj) {   
  32.             System.out.println("equals");   
  33.             if (this == obj)   
  34.                 return true;   
  35.             if (obj instanceof Sort) {   
  36.                 Sort s = (Sort) obj;   
  37.                 if (s.key.equals(s.key)) {   
  38.                     num++;   
  39.                     s.num++;   
  40.                     return true;   
  41.                 }   
  42.             }   
  43.             return false;   
  44.         }   
  45.   
  46.         public Object getKey() {   
  47.             return key;   
  48.         }   
  49.   
  50.         public void setKey(Object key) {   
  51.             this.key = key;   
  52.         }   
  53.   
  54.         public int getNum() {   
  55.             return num;   
  56.         }   
  57.   
  58.         public void setNum(int num) {   
  59.             this.num = num;   
  60.         }   
  61.   
  62.         public int compareTo(Sort o) {   
  63.             return -num + o.num;   
  64.         }   
  65.   
  66.     }   
  67.     public void push(String key) {   
  68.         bag.add(key);   
  69.     }   
  70.   
  71.     Bag bag = BagUtils.typedBag(new HashBag(), String.class);   
  72.   
  73.     public List<Sort> sort() {   
  74.   
  75.         Set objs = bag.uniqueSet();   
  76.         List<Sort> listR = new ArrayList<Sort> ();   
  77.         for (Object o : objs) {   
  78.             listR.add(new Sort(o, bag.getCount(o)));   
  79.         }   
  80.         Collections.sort(listR);   
  81.         return listR;   
  82.     }   
  83. }   
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值