---
title: "CountSortTool "
author: "phantom"
date: "2007-06-28T23:50:54Z"
tags: []
slug: "countsorttool"
draft: false
lastmod: "2007-06-28T23:50:54Z"
id: 95226
url: "https://phantom.iteye.com/blog/95226"
---

java 代码

01. package org.jh.app.count;
02. import java.util.ArrayList;
03. import java.util.Collections;
04. import java.util.List;
05. import java.util.Set;
06. import org.apache.commons.collections.Bag;
07. import org.apache.commons.collections.BagUtils;
08. import org.apache.commons.collections.bag.HashBag;
09. public class CountSortTool {
10. static public class Sort implements Comparable&lt;Sort&gt; {
11. Object key;
12. int num = 1;
13. Sort(Object key) {
14. this.key = key;
15. }
16. public Sort(Object o, int count) {
17. this.key = o;
18. num = count;
19. }
20. public String toString() {
21. return key + ":" + num + "\\n";
22. }
23. public boolean equals(Object obj) {
24. System.out.println("equals");
25. if (this == obj)
26. return true;
27. if (obj instanceof Sort) {
28. Sort s = (Sort) obj;
29. if (s.key.equals(s.key)) {
30. num++;
31. s.num++;
32. return true;
33. }
34. }
35. return false;
36. }
37. public Object getKey() {
38. return key;
39. }
40. public void setKey(Object key) {
41. this.key = key;
42. }
43. public int getNum() {
44. return num;
45. }
46. public void setNum(int num) {
47. this.num = num;
48. }
49. public int compareTo(Sort o) {
50. return -num + o.num;
51. }
52. }
53. public void push(String key) {
54. bag.add(key);
55. }
56. Bag bag = BagUtils.typedBag(new HashBag(), String.class);
57. public List&lt;Sort&gt; sort() {
58. Set objs = bag.uniqueSet();
59. List&lt;Sort&gt; listR = new ArrayList&lt;Sort&gt; ();
60. for (Object o : objs) {
61. listR.add(new Sort(o, bag.getCount(o)));
62. }
63. Collections.sort(listR);
64. return listR;
65. }
66. }