集合框架-Map集合-HashMap存储自定义对象

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
集合框架-Map集合-HashMap存储自定义对象的更多相关文章
- 集合框架-Map集合
* Map集合和Collection集合的区别? * Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的.可以把这个理解为:夫妻对 * Collection集合存储元素是单独出现的, ...
- 集合框架-Map集合练习-Map查表法
1 package cn.itcast.p10.map.test; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public c ...
- 集合框架-Map集合练习-记录字母次数思路及代码
1 package cn.itcast.p10.map.test; 2 3 import java.util.Iterator; 4 import java.util.Map; 5 import ja ...
- 集合框架-Map集合重点方法keySet演示
1 package cn.itcast.p6.map.demo; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import ...
- 集合框架-Map集合特点及常用方法
1 package cn.itcast.p6.map.demo; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import ...
- 集合框架-Map集合-TreeMap存储自定义对象
1 package cn.itcast.p8.treemap.demo; 2 3 4 import java.util.Iterator; 5 import java.util.Map; 6 impo ...
- 集合框架-Map集合-LinkedHashMap及关联源码操作
1 package cn.itcast.p9.linkedhashmap.demo; 2 3 import java.util.HashMap; 4 import java.util.Iterator ...
- java集合框架——Map
一.概述 1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...
- Java集合框架——Map接口
第三阶段 JAVA常见对象的学习 集合框架--Map集合 在实际需求中,我们常常会遇到这样的问题,在诸多的数据中,通过其编号来寻找某一些信息,从而进行查看或者修改,例如通过学号查询学生信息.今天我们所 ...
随机推荐
- typora 基本使用和漂亮的主题样式
以下是使用博客园的markdown的效果: typora 基本使用和漂亮的主题样式 一.typora 基本使用 ps:文字排版,使用markdown nice 可以一键复制到公众号.知乎:https: ...
- 【LeetCode】788. Rotated Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- C. Unfair Poll
http://codeforces.com/problemset/problem/758/C C. Unfair Poll time limit per test 1 second memory li ...
- CSS实现水平垂直居中的方式有哪些?
CSS实现水平垂直居中的方式有哪些? 基本结构样式: .box { width: 400px; height: 400px; background-color: red; } .inner { wid ...
- Chapter 21 G-Methods for Time-Varying Treatments
目录 21.1 The g-formula for time-varying treatments 21.2 IP weighting for time-varying treatments 21.3 ...
- Chapter 6 Graphical Representation of Causal Effects
目录 6.1 Causal diagrams 6.2 Causal diagrams and marginal independence 6.3 Causal diagrams and conditi ...
- [android]打印C++的输出信息在安卓logcat上调试
#include <android/log.h> //宏定义全局函数:C++打印log到android-debug模式下帮助调试(勿删) //调用方式:slogd("test n ...
- 使用所学Spring知识,实现简易的图书查询系统功能。实现查询全部图书。 根据书籍编号查询信息。 根据书名查询书籍信息。 根据状态查询书籍信息。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用所学Spring知识,实现简易的图书查询系统功能. 要求如下: 查询全部图书. 根据书籍编号查询信息. 根据 ...
- SpringBoot集成Actuator监控管理
1.说明 本文详细介绍Spring Boot集成Actuator监控管理的方法, 基于已经创建好的Spring Boot工程, 然后引入Actuator依赖, 介绍监控管理相关功能的使用. Sprin ...
- docker构建.net core运行的镜像
在docker很火的今天,越来越多的应用现在都在往docker上迁移,.net core怎么能落后? 项目要运行在docker上,我们需要先制作镜像,可以基于centos来制作,当然也可以基于Ubun ...