public class TreeSet1 {
public static void main(String[] args) {
Map map=new HashMap();
map.put("ggw",2);
map.put("asdf",1);
//将Map转换为Map.Entry
Set set=map.entrySet();
TreeSet ts=new TreeSet(new Comparator(){
@Override
public int compare(Object o1, Object o2) {
Integer value1=(Integer) ((Map.Entry)o1).getValue();
Integer value2=(Integer) ((Map.Entry)o2).getValue();
return value1.compareTo(value2);
}});
ts.addAll(set);
Iterator ti=ts.iterator();
while(ti.hasNext())
{
System.out.println(((Map.Entry)ti.next()).getValue());
}
}
}
|