ITeye博客

CountSortTool

phantom · 2007-06-28 23:50 · Markdown

java 代码

  1. package org.jh.app.count;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.Set;
  6. import org.apache.commons.collections.Bag;
  7. import org.apache.commons.collections.BagUtils;
  8. import org.apache.commons.collections.bag.HashBag;
  9. public class CountSortTool {
  10. static public class Sort implements Comparable<Sort> {
  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<Sort> sort() {
  58. Set objs = bag.uniqueSet();
  59. List<Sort> listR = new ArrayList<Sort> ();
  60. for (Object o : objs) {
  61. listR.add(new Sort(o, bag.getCount(o)));
  62. }
  63. Collections.sort(listR);
  64. return listR;
  65. }
  66. }

评论(1)

  1. #1 楼 · phantom · 2007-06-28 23:51
    package org.jh.app.count;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Set;
    
    import org.apache.commons.collections.Bag;
    import org.apache.commons.collections.BagUtils;
    import org.apache.commons.collections.bag.HashBag;
    
    public class CountSortTool {
    	static public class Sort implements Comparable<Sort> {
    		Object key;
    
    		int num = 1;
    
    		Sort(Object key) {
    			this.key = key;
    		}
    
    		public Sort(Object o, int count) {
    			this.key = o;
    			num = count;
    		}
    
    		public String toString() {
    			return key + ":" + num + "\n";
    		}
    
    		public boolean equals(Object obj) {
    			System.out.println("equals");
    			if (this == obj)
    				return true;
    			if (obj instanceof Sort) {
    				Sort s = (Sort) obj;
    				if (s.key.equals(s.key)) {
    					num++;
    					s.num++;
    					return true;
    				}
    			}
    			return false;
    		}
    
    		public Object getKey() {
    			return key;
    		}
    
    		public void setKey(Object key) {
    			this.key = key;
    		}
    
    		public int getNum() {
    			return num;
    		}
    
    		public void setNum(int num) {
    			this.num = num;
    		}
    
    		public int compareTo(Sort o) {
    			return -num + o.num;
    		}
    
    	}
    	public void push(String key) {
    		bag.add(key);
    	}
    
    	Bag bag = BagUtils.typedBag(new HashBag(), String.class);
    
    	public List<Sort> sort() {
    
    		Set objs = bag.uniqueSet();
    		List<Sort> listR = new ArrayList<Sort> ();
    		for (Object o : objs) {
    			listR.add(new Sort(o, bag.getCount(o)));
    		}
    		Collections.sort(listR);
    		return listR;
    	}
    }
    
    

评论功能维护中,当前仅展示历史评论。