011_TreeMap对键实现了Comparable接口的对象排序
生活随笔
收集整理的這篇文章主要介紹了
011_TreeMap对键实现了Comparable接口的对象排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. TreeMap對鍵實現了Comparable接口的對象排序
import java.util.TreeMap; import java.util.Map.Entry;/*** TreeMap對鍵實現了Comparable接口的對象排序 */ public class TreeMapComparable {public static void main(String[] args) {TreeMap<Person, Integer> tm = new TreeMap<Person, Integer>();tm.put(new Person("zhangsan", 19), 1005);tm.put(new Person("cuihua", 16), 1000);tm.put(new Person("fanbingbing", 39), 1009);tm.put(new Person("zhaoliying", 29), 1001);for (Entry<Person, Integer> kv : tm.entrySet()) {System.out.println(kv.getKey() + ":" + kv.getValue());}} }class Person implements Comparable<Person>{public String name;public int age;public Person(String name, int age) {this.name = name;this.age = age;}public int compareTo(Person o) {return o.age - age;}public String toString() {return "[name = " + name + " age = " + age + "]";} }?
總結
以上是生活随笔為你收集整理的011_TreeMap对键实现了Comparable接口的对象排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 010_TreeMap键使用Compar
- 下一篇: 012_Comparable和Compa