首页 技术 正文
技术 2022年11月18日
0 收藏 392 点赞 3,697 浏览 3135 个字

 1 package cn.itcast.p6.hashmap.demo;
2
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 import cn.itcast.p2.bean.Student;
8
9 public class HashMapDemo {
10
11 public static void main(String[] args) {
12 // TODO Auto-generated method stub
13 /*
14 * 将学生对象HashMap<K, V>通过键与值存储到map集合中。
15 */
16
17 HashMap<Student,String> hm = new HashMap<Student,String>();
18 hm.put(new Student("lisi",38), "北京");
19 hm.put(new Student("zhaoliu",24), "上海");
20 hm.put(new Student("xiaoqiang",31), "沈阳");
21 hm.put(new Student("wangcai",28), "大连");
22 hm.put(new Student("zhaoliu",24), "铁岭");
23
24 // Set<Student> keySet = hm.keySet();
25 //
26 // Iterator<Student> it = keySet.iterator();
27
28 Iterator<Student> it = hm.keySet().iterator();
29 while (it.hasNext()) {
30 Student key = it.next();
31 String value = hm.get(key);
32 System.out.println(key.getName()+":"+key.getAge()+"---"+value);
33 }
34 }
35
36 }

HashMapDemo

 1 package cn.itcast.p2.bean;
2
3 public class Person implements Comparable<Person>{
4 private String name;
5 private int age;
6
7
8 public Person() {
9 super();
10 // TODO Auto-generated constructor stub
11 }
12
13 public Person(String name, int age) {
14 super();
15 this.name = name;
16 this.age = age;
17 }
18 public int compareTo(Person p) {//指定比较类型,传入比较类型
19 // Person p= (Person)obj;
20 int temp = this.age-p.age;
21 return temp==0?this.name.compareTo(p.name):temp;
22 }
23
24
25 @Override
26 public int hashCode() {
27 final int prime = 31;
28 int result = 1;
29 result = prime * result + age;
30 result = prime * result + ((name == null) ? 0 : name.hashCode());
31 return result;
32 }
33
34 @Override
35 public boolean equals(Object obj) {
36 if (this == obj)
37 return true;
38 if (obj == null)
39 return false;
40 if (getClass() != obj.getClass())
41 return false;
42 Person other = (Person) obj;
43 if (age != other.age)
44 return false;
45 if (name == null) {
46 if (other.name != null)
47 return false;
48 } else if (!name.equals(other.name))
49 return false;
50 return true;
51 }
52
53 // @Override
54 // public boolean equals(Object obj) {//不能把Object改为Person,equals方法来自Object,Object没有定义泛型
55 // // TODO Auto-generated method stub
56 // if (this == obj) {
57 // return true;
58 // }
59 // if (!(obj instanceof Person)) {
60 // throw new RuntimeException()
61 // }
62 // Person p = (Person)obj;
63 // return super.equals(obj);
64 // }
65 /*
66 * @Override public int hashCode() { final int prime = 31; int result = 1;
67 * result = prime * result + age; result = prime * result + ((name == null) ? 0
68 * : name.hashCode()); return result; }
69 *
70 * @Override public boolean equals(Object obj) { if (this == obj) return true;
71 * if (obj == null) return false; if (getClass() != obj.getClass()) return
72 * false; Person other = (Person) obj; if (age != other.age) return false; if
73 * (name == null) { if (other.name != null) return false; } else if
74 * (!name.equals(other.name)) return false; return true; }//alt+shift+s
75 * hashCode和equals
76 */
77 public String getName() {
78 return name;
79 }
80 public void setName(String name) {
81 this.name = name;
82 }
83 public int getAge() {
84 return age;
85 }
86 public void setAge(int age) {
87 this.age = age;
88 }
89
90 @Override
91 public String toString() {
92 return "Person:"+getName()+":"+getAge();
93 }
94
95
96 }

Person

 1 package cn.itcast.p2.bean;
2
3 public class Student extends Person {
4
5 public Student() {
6 super();
7 // TODO Auto-generated constructor stub
8 }
9
10 public Student(String name, int age) {
11 super(name, age);
12 // TODO Auto-generated constructor stub
13 }
14
15 @Override
16 public String toString() {
17 // TODO Auto-generated method stub
18 return "Student:"+getName()+":"+getAge();
19 }
20
21 }

Student

上一篇: dos2unix、diff命令
下一篇: MySQL语句SQL应用
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922