TreeMap方法 演示操作

TreeMap方法 演示操作

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>{@Overridepublic int compare(Studenti o1, Studenti o2) {int i = o1.getName().compareTo(o2.getName());if(i==0)return o1.getAge()-o2.getAge();return i;}}

运行程序

,以诚感人者,人亦诚而应。

TreeMap方法 演示操作

相关文章:

你感兴趣的文章:

标签云: