Map集合和TreeSet集合的使用:自定义比较器
来源:互联网 发布:秦岚演技知乎 编辑:程序博客网 时间:2024/06/11 16:05
<pre name="code" class="java">/**每一个学生都是有对应的归属地‘有自己的姓名和年龄注意,姓名和年龄相同者视为同一学生保证学生唯一性描述学生,将学生作为键,地址作为值,存入Map集合。 然后再获取学生。<pre name="code" class="java">将学生对象作为Map集合实例对象的参数传递。
import java.util.*;class MapAboutStudentInfo {public static void main(String[] args) {HashMap<Student,String> maps=new HashMap<Student,String>();maps.put(new Student(23,"zhangsan"),"beijing");maps.put(new Student(30,"lisi"),"shanghai");maps.put(new Student(17,"wangwu"),"tianjing");Set<Map.Entry<Student,String>> entryset=maps.entrySet();Iterator<Map.Entry<Student,String>> it=entryset.iterator();while (it.hasNext()){Map.Entry<Student,String> entry=it.next();Student key=entry.getKey();String value=entry.getValue();System.out.println(key+"=="+value);}}}class Student implements Comparable<Student>{private int age;private String name;Student(int age,String name){this.age=age;this.name=name;}public int getAge(){return age;}public String getName(){return name;}//复写hashCode方法public int hashCode(){return name.hashCode()+age*34;}//复写toString方法public String toString(){return name+":"+age;}//复写equals方法public boolean equals(Object obj){if (!(obj instanceof Student))throw new ClassCastException("类型不匹配。");Student s=(Student)obj;return this.name.equals(s.name)&&this.age==s.age;}public int compareTo(Student s){int num= new Integer(this.age).compareTo(new Integer(s.age));if (num==0)return this.name.compareTo(s.name);return num;}}
0 0
- Map集合和TreeSet集合的使用:自定义比较器
- TreeSet集合中自定义比较器
- 集合TreeSet的使用
- TreeSet集合存放自定义类型的对象,可以使用比较器,实现Comparable接口自己设置排序方式
- 集合框架--TreeSet集合Comparator比较器
- 使用TreeSet集合比较Comparable接口和Comparator接口的区别
- 使用TreeSet集合比较Comparable接口和Comparator接口的区别
- 使用TreeSet集合比较Comparable接口和Comparator接口的区别
- 使用TreeSet集合比较Comparable接口和Comparator接口的区别
- TreeSet集合的一个比较器排序去重案例
- java集合框架TreeSet的使用和HashSet的使用
- 黑马程序员 集合ArrayList、HashSet和TreeSet的使用
- HashSet集合和TreeSet集合
- 集合第四步: TreeSet比较顺序和比较重复机制
- Map集合的Key比较
- 22-(集合框架-TreeSet集合) 1 2 23-(集合框架-TreeSet集合-Comparator比较器) 24(集合框架-TreeSet集合 25 -字符串长度排xu
- java基础—自定义一个比较器,对TreeSet 集合中的元素按指定方法来排序(java集合六)
- Map集合的使用
- C++11新特性:类型别名,auto类型,decltype类型
- 让ArralList集合具备判重功能。去除重复元素
- android 打开amazon appstore
- 用Set集合,按照字符串的长度排序的方法
- 好的网站链接或文章链接(二)
- Map集合和TreeSet集合的使用:自定义比较器
- 瀑布式迭代与敏捷
- python 查看相关的help函数
- qt利用委托:QStyledItemDelegate,实现tableview 的表格项的自定义显示,如插入图片,绘制控件
- 2014亚洲区域赛网络赛广州赛区A Corrupt Mayor's Performance Art(hdoj5023)
- 时间戳与字符串相互转换
- 进入游戏开发公司后的感想
- Codeforces Round #267 (Div. 2) E Alex and Complicated Task
- 机器学习方法概论2