package mianshiti;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
public class T1 {
/**
* 1.选择合适的Map集合保存5位学员的学号和姓名,然后按学号的自然顺序的倒序将这些键值对一一打印出来。
*/
public static void main(String[] args) {
TreeMap<Integer, String> tp = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int num = o2 - o1;
return num;
}
});
tp.put(1,"a");
tp.put(2,"b");
tp.put(3,"c");
tp.put(4,"d");
tp.put(5,"e");
for (Map.Entry<Integer, String> mp : tp.entrySet()) {
System.out.println(mp.getKey() + "=" + mp.getValue());
}
//demo1(tp);
}
/**
* @param tp
*/
private static void demo1(TreeMap<String, Integer> tp) {
Collection<Integer> value = tp.values();
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer s1, Integer s2) {
int num = s2 - s1;
return num;
}
});
ts.addAll(value);
for (Integer integer : ts) {
for (String s : tp.keySet()) {
if(tp.get(s) == integer) {
System.out.println(s + "=" + tp.get(s));
}
}
}
}
}
|
|