1、使用默认的TreeMap 构造函数,其中key值需要有比较规则。

2、使用默认的TreeMap 构造函数,Key中添加自定义类型,自定义类型必须继承Comparator。

3、使用比较器类来来实现排序,自定义类型不用来继承Comparator

试例代码:

public class Demo {

public static void main(String[] args) {

//1、使用默认的TreeMap 构造函数,其中key值需要有比较规则

TreeMap<Integer, String> map =new TreeMap<>();

map.put(new Integer(2), "BB");

map.put(new Integer(1), "AA");

map.put(new Integer(5), "EE");

map.put(new Integer(3), "CC");

map.put(new Integer(4), "DD");

map.put(new Integer(2), "AA");   //验证重复key是否能够插入

//使用遍历EntrySet方式

for(Entry<Integer, String> entry:map.entrySet()){

System.out.println("Key:"+entry.getKey()+ " --- value:"+entry.getValue());

}

//2、使用默认的TreeMap 构造函数,Key中添加自定义类型,自定义类型必须继承Comparator

System.out.println("-------------------2、使用默认的TreeMap 构造函数,Key中添加自定义类型,自定义类型必须继承Comparator-----------------------");

TreeMap<person,String> mapPer=new TreeMap<>();

mapPer.put(new person("张三",22), "6K");

mapPer.put(new person("老王",35), "29K");

mapPer.put(new person("小张",31), "11K");

for(Entry<person, String> entry:mapPer.entrySet()){

System.out.println("Key:"+entry.getKey()+ " --- value:"+entry.getValue());

}

//3、使用比较器类来来实现排序,自定义类型不用来继承Comparator

System.out.println("-------------------3、使用比较器类来来实现排序,自定义类型不用来继承Comparator-----------------------");

TreeMap<Book,String> mapBook =new TreeMap<>(new BookComparator());

mapBook.put(new Book("流浪地球",60),"200页");

mapBook.put(new Book("三体",100),"400页");

mapBook.put(new Book("大秦帝国",180),"900页");

for(Entry<Book, String> entry:mapBook.entrySet()){

System.out.println("Key:"+entry.getKey()+ " --- value:"+entry.getValue());

}

}   }

//自定义person类

class person implements Comparable<person> {

String name;  int age;

public person(String name,int age) {

this.name=name;

this.age=age;  }

@Override

public String toString() {

return "姓名:"+this.name +" 年龄:" +this.age;

}

@Override

public int compareTo(person o) {

if(o.age>this.age) {

return 1;

} else if(o.age<this.age) {

return -1;

}

return 0;

}  }

//自定义book类

class Book {

String name;

double price;

public Book(String name,double price) {

this.name=name;

this.price=price;  }

@Override

public String toString() {

return "书名:"+this.name+" 价格:"+this.price;

}  }

//book比较器类

class BookComparator implements Comparator<Book>{

@Override

public int compare(Book o1, Book o2) {

if(o1.price>o2.price) {

return 1;

} else if(o1.price<o2.price) {

return -1;

}

return 0;

}  }

最新文章

  1. android UI 仿 win 8 模块化 标题,并实现 可长按拖动交换图片位置、可点击,且伴随动画特效
  2. PHP5.3.3+Apache2.2.16+MySQL5.1.49
  3. Flex 关闭浏览器
  4. [20160701]DevideByZeroWithoutNoException——from 《Java How To Program (Early Objects), 10th》
  5. hadoop datanode启动失败
  6. dpkg用法详解
  7. CLR VIA C#委托
  8. Visual Studio添加dll程序集引用操作步骤
  9. (转)IOS内存管理 retain release
  10. android圆角View实现及不同版本这间的兼容
  11. swt,jface,rcp
  12. [Android学习笔记]Android调试
  13. C#之lambda表达式
  14. 解决打开MATLAB时出现“Waring:could not read file classpath.txt”,等问题
  15. Java数据结构和算法 - 数组
  16. Java反射定义、获取Class三种方法
  17. Lab 11-3
  18. # Writing your-first Django-app-part 4-simple-form
  19. jquery+easyui开发、培训文档
  20. #leetcode刷题之路39-组合总和

热门文章

  1. React16源码解读:开篇带你搞懂几个面试考点
  2. angularjs路由菜单强制刷新
  3. Windows10内嵌Ubuntu子系统初始化设置
  4. rhel
  5. 无法解析导入javax.mail
  6. List自定义排序 (例子省份排序)
  7. 学习 lind api 十月 第5弹
  8. python读取文件使用相对路径的方法
  9. Quantitative Trading with R(一):两个简单的策略
  10. potel99se 文件损坏修复