Java - HashMap
来源:互联网 发布:呼吸阀计算软件 编辑:程序博客网 时间:2024/05/01 23:31
//HashMap: 底层是哈希表数据结构,允许使用null键或值,线程不同步import java.util.*;class Student implements Comparable<Student>{ private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public void setName(String name) { this.name=name; } public void setAge(int Age) { this.age=age; } public String getName() { return this.name; } public int getAge() { return this.age; } public String toString() { return name+":"+age; } public int hashCode() { return name.hashCode()+this.age*23; } 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; }}class HashMapTest{ public static void main(String[] args){ HashMap<Student,String> hm=new HashMap<Student,String>() ; hm.put(new Student("test1",21),"beijing"); //新增元素 hm.put(new Student("test2",22),"nanjing"); hm.put(new Student("test3",23),"shanghai"); hm.put(new Student("test4",24),"hangzhou"); //map 第一种取出方式:keySet Set<Student> keyset =hm.keySet(); Iterator<Student> it=keyset.iterator(); while(it.hasNext()) { Student stu= it.next(); String addr=hm.get(stu); System.out.println(stu+" "+addr); } //map 第二种取出方式:entrySet Set<Map.Entry<Student,String>> mapentry = hm.entrySet(); Iterator<Map.Entry<Student,String>> itr=mapentry.iterator(); while(itr.hasNext()) { Map.Entry<Student,String> me= itr.next(); Student s = me.getKey(); String addr = me.getValue(); System.out.println(s+" "+addr); } }}
0 0
- java hashMap
- Java HashMap
- java---hashmap
- java hashmap
- Java HashMap
- JAVA-HASHMAP
- java Hashmap
- java HashMap
- Java HashMap
- java HashMap
- Java - HashMap
- Java hashMap
- Java HashMap
- java-hashmap
- Java HashMap
- 【java】HashMap
- JAVA HashMap
- java-----HashMap
- CAFFE实验学习笔记(1)——SSD(Single Shot MultiBox Detector)
- JQuery初步
- 反射基础
- 加载HTML代码
- struts结果类型
- Java - HashMap
- 身份证号码验证
- GUI编程 鼠标事件和键盘事件
- Linux学习笔记一
- 6.30(HTML)
- 第一次想使用插件,就看看啦!jquery-confirm
- httputils用法
- modal模态框
- nvl函数