import java.util.Comparator;
import java.util.TreeMap;
import com.studyself.bean.Person;
public class Demo7_TreeMap {
public static void main(String[] args) {
// demo1();
TreeMap<Person, String> tm = new TreeMap<>(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
int num = o1.getName().compareTo(o2.getName());
return num == 0 ? o1.getAge() - o2.getAge() : num;
}
});
tm.put(new Person("张三", 23), "北京");
tm.put(new Person("李四", 24), "上海");
tm.put(new Person("王五", 25), "广州");
tm.put(new Person("赵六", 26), "深圳");
System.out.println(tm);
}
private static void demo1() {
TreeMap<Person, String> tm = new TreeMap<>();
tm.put(new Person("张三", 23), "北京");
tm.put(new Person("李四", 24), "上海");
tm.put(new Person("王五", 25), "广州");
tm.put(new Person("赵六", 26), "深圳");
System.out.println(tm);
}
}
|