package timeflow.util; import java.util.*; public class Bag implements Iterable { HashMap table; int max; public Bag() { table=new HashMap(); } public Bag(Iterable i) { for (T x:i) add(x); } public Bag(T[] array) { for (int i=0; i listTop(int n) { int count=0; Iterator i=list().iterator(); List top=new ArrayList(); while (count unordered() { List result=new ArrayList(); result.addAll(table.keySet()); return result; } public List list() { List result=new ArrayList(); result.addAll(table.keySet()); Collections.sort(result, new Comparator() { public int compare(T x, T y) { return num(y)-num(x); } }); return result; } public int num(T x) { Count c=table.get(x); if (c!=null) return c.num; else return 0; } public int add(T x) { Count c=table.get(x); int n=0; if (c!=null) n=++c.num; else { table.put(x, new Count(1)); n=1; } max=Math.max(n,max); return n; } class Count { int num; public Count(int num) { this.num=num; } } public int size() { return table.size(); } public int removeLessThan(int cut) { Set small=new HashSet(); for (T x: table.keySet()) { if (num(x) b=new Bag(); b.add("a"); b.add("b"); b.add("a"); System.out.println(b.num("a")); System.out.println(b.num("b")); System.out.println(b.num("c")); List s=b.list(); for (int i=0; i iterator() { return table.keySet().iterator(); } }