标签:treemap java 黑马程序员 java基础 map
import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.TreeMap; public class TreeMapDemos { public static void main(String[] args) { TreeMap<Studenti,String> hm = new TreeMap<Studenti,String>(new MyCompare()); hm.put(new Studenti("kk",22), "山西"); hm.put(new Studenti("jj",20), "山东"); hm.put(new Studenti("jj",20), "北京"); hm.put(new Studenti("hh",25), "上海"); Iterator<Studenti> it = hm.keySet().iterator(); while(it.hasNext()) { Studenti key = it.next(); String value = hm.get(key); System.out.println(key.toString()+" --"+value); } } public static void hashmapdemos() { // HashMap<Studenti,String> hm = new HashMap<Studenti,String>(); hm.put(new Studenti("kk",22), "山西"); hm.put(new Studenti("jj",20), "山东"); hm.put(new Studenti("jj",20), "北京"); hm.put(new Studenti("hh",25), "上海"); Iterator<Studenti> it = hm.keySet().iterator(); while(it.hasNext()) { Studenti key = it.next(); String value = hm.get(key); System.out.println(key.toString()+" --"+value); } } } //自定义一个比较器 class MyCompare implements Comparator<Studenti> { @Override public int compare(Studenti o1, Studenti o2) { int i = o1.getName().compareTo(o2.getName()); if(i==0) return o1.getAge()-o2.getAge(); return i; } }
运行程序
标签:treemap java 黑马程序员 java基础 map
原文地址:http://blog.csdn.net/zl18603543572/article/details/46561769