HashMap的两种取出方式keySet和entrySet
来源:互联网 发布:嵌入式单片机按钮模块 编辑:程序博客网 时间:2024/04/27 20:28
/*HashMap的两种取出方式keySet和entrySet每一个学生都有对应的归属地。学生Student,地址String.学生属性:姓名,年龄。注意:姓名和年龄相同的视为同一个学生。保证学生的唯一性。1,描述学生。2,定义map容器。将学生作为键,地址作为值。存入。3.获取map集合中的元素。*/class Student implements Comparable<Student>//实现比较性< ? super E>{ private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public int compareTo(Student s)//重写compareTo()方法 { int num = new integer(this.age).compareTo(new Integer(s.age));//先比较age if(num==0) return this.name.compareTo(s.name);//再比较name return num; } public int hashCode()//重写hashCode() { return name.hashCode()+age*34; } public boolean equals(Object obj)//重写equals() { if (! (obj instanceof Student)) throw new ClassCastException("类型不匹配");//类型转换异常 Student s = (Student)obj;//强制转换类型 return this.name.equals(s.name) && this.age==s.age;//判断后存入Hash表 } public String getName() { return name; } public int getAge() { return age; } public String toString() { return name+":"+age; }}class MapTest { public static void main(String[] args) { System.out.println("Hello World!"); HashMap<Studengt,String> hm = new HashMap<Studengt,String>(); hm.put(hm.put(new Student("lisi1",21),"beijing"));//put(K key, V value)存入 hm.put(hm.put(new Student("lisi1",21),"tianjin")); hm.put(hm.put(new Student("lisi2",22),"shanghai")); hm.put(hm.put(new Student("lisi3",23),"nanjing")); hm.put(hm.put(new Student("lisi4",24),"wuhan")); //第一种取出方式 keySet Set<Student> keySet = hm.keySet(); Iterator<Student> it = keySet.iterator(); while(it.hasNext()) { Student stu = it.next(); String addr = hm.get(stu);//传入值key,返回值是<k,v>里面的value System.out.println(stu+"..."+addr); } //第二种取出方式entrySet //Map.Entry是一种接口再实现内部接口 Set<Map.Entry<Student,String> entrySet = hm.next(); Iterator<Map.Entry<Student,String> iter = entrySet.iterator(); while(it.hasNext()) { Map.Entry<Student,String> me = iter.next(); Student stu = me.getKey(); String addr = me.getValue(); System.out.println(stu+"..."+addr); } }}
0 0
- HashMap的两种取出方式keySet和entrySet
- 【java编程】Map集合之HashMap--KeySet和EntrySet的两种取出方式
- Map集合中的两种取出方式keySet和entrySet
- Map集合的两种取出方式(keySet、entrySet)
- java中map集合的两种取出方式keySet和entrySet
- 集合框架——Map的两种取出方式(keySet和entrySet)
- Map遍历的两种方式keySet()和entrySet()
- 黑马程序员_集合HashMap练习:获取Map集合元素的两种方式:keySet()和entrySet()方法
- Map容器小程序,学生作为key,地址作为value,采用keySet和entrySet两种取出方式
- HashMap遍历:entrySet和keySet的比较
- Map集合的两种获取元素的方式:keySet()和entrySet()方法
- day16集合map里面的(hashtable,hashmap,treemap)的取出方式(keyset和entryset),获取该字符串的字母出现次数
- 遍历Map的两种方法:keySet()和entrySet()
- Map便利的两种方法:keySet和entrySet
- 遍历Map的两种方法:keySet()和entrySet()
- 遍历Map的两种方法:keySet()和entrySet()
- 遍历Map的两种方法:keySet()和entrySet()
- HashMap中的keySet()和entrySet()
- HDU 1394 Minimum Inversion Number (线段树)
- 【趣题】【树状数组】NKOJ 3703 HH的项链
- Leetcode题解 160. Intersection of Two Linked Lists
- 趁着还年轻
- 前端开发人员必须了解的七大技能图谱
- HashMap的两种取出方式keySet和entrySet
- 349 350. Intersection of Two Arrays I II
- kali信息收集工具之Cookie Cadger
- 网络编程文章总目录
- 只为java 抽象和接口的区别
- Single Number III
- 只为java 接口和抽象区别
- 《leetCode》:Summary Ranges
- 352. Data Stream as Disjoint Intervals