Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例
1. HashMap集合(HashMap<String,Student>)的案例
HashMap是最常用的Map集合,它的键值对在存储时要根据键的哈希码来确定值放在哪里。
HashMap的底层是利用hash算法算出键对应的哈希码,然后我们把值存放在这个哈希码对应的存储位置。当我们需要取出这个值的时候,我们利用hash算法算出键对应的哈希码,然后就可以快速定位到哈希码对应的存储地方的值。
哈希表结构主要作用:快速定位查找。
2. 代码示例:
(1)Student.java:
package cn.itcast_02;
public class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(2)代码测试类HashMapDemo3 :
package cn.itcast_02; import java.util.HashMap;
import java.util.Set; /*
* HashMap<String,Student>
* 键:String 学号
* 值:Student 学生对象
*/
public class HashMapDemo3 {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, Student> hm = new HashMap<String, Student>(); // 创建学生对象
Student s1 = new Student("周星驰", 58);
Student s2 = new Student("刘德华", 55);
Student s3 = new Student("梁朝伟", 54);
Student s4 = new Student("刘嘉玲", 50); // 添加元素
hm.put("9527", s1);
hm.put("9522", s2);
hm.put("9524", s3);
hm.put("9529", s4); // 遍历
Set<String> set = hm.keySet();
for (String key : set) {
// 注意了:这次值不是字符串了
// String value = hm.get(key);
Student value = hm.get(key);
System.out.println(key + "---" + value.getName() + "---"
+ value.getAge());
}
}
}
运行效果,如下:

Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例的更多相关文章
- Java基础知识强化之IO流笔记56:IO流练习之 登录注册IO版
1. 登录注册IO版的Java项目框架,如下: 2. 具体代码实现: (1)User.java(cn.itcast.game): package cn.itcast.pojo; /** * 这是用户 ...
- Java基础知识强化之IO流笔记77:NIO之 Selector
Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 1. ...
- Java基础知识强化之IO流笔记33:转换流之InputStreamReader的使用
1. InputStreamReader的使用 InputStreamReader(InputStream is):用默认的编码读取数据 InputStreamReader(InputStream i ...
- Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入
1. 如何实现数据的换行? (1) package com.himi.fileoutputstream; import java.io.FileNotFoundException; import j ...
- Java基础知识强化之IO流笔记68:Properties和IO流集合使用
1. Properties和IO流集合使用 这里的集合必须是Properties集合: public void load(Reader reader):把文件中的数据读取到集合中 public v ...
- Java基础知识强化之IO流笔记66:Properties的概述 和 使用(作为Map集合使用)
1. Properties的概述 Properties:属性集合类.是一个可以和IO流相结合使用的集合类. 该类主要用于读取以项目的配置文件(以.properties结尾的文件 和 xml文件). ...
- Java基础知识强化之IO流笔记46:IO流练习之 把文本文件中数据存储到集合中的案例
1. 把文本文件中数据存储到集合中 需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合. 分析: 通过题目的意思我们可以知道如下的一些内容, 数据 ...
- Java基础知识强化之IO流笔记45:IO流练习之 把集合中的数据存储到文本文件案例
1. 把集合中的数据存储到文本文件案例: 需求:把ArrayList集合中的字符串数据存储到文本文件 ? (1)分析:通过题目的意思我们可以知道如下的一些内容,ArrayList集合里存储的是字 ...
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
使用HttpClient进行Get方式通信,通过HttpClient建立网络链接,使用HttpGet方法读取数据,并且通过Response获取Entity返回值. 使用HttpClient进行Post ...
- Java基础知识强化之网络编程笔记17:Android网络通信之 使用Http的Post方式读取网络数据(基于HTTP通信技术)
使用Http的Post方式与网络交互通信.Post方式需要向网络传输一部分数据,同时具有输入流和输出流. 详见:Android(java)学习笔记210:采用post请求提交数据到服务器(qq登录案例 ...
随机推荐
- http://www.cnblogs.com/xdp-gacl/p/3951952.html
http://www.cnblogs.com/xdp-gacl/p/3951952.html http://www.cnblogs.com/kristain/articles/2409021.html
- 子查询解嵌套not in 无法展开改写
SQL> explain plan for select * from OPS$CZTEST1.SAVJ_ATOMJOURBAK where ((list_flag = '1' and prt_ ...
- 【CF】3B Lorry
这道题目网上有几个题解,均有问题.其实就是简单的贪心+排序,没必要做的那么复杂.一旦tot+curv > v时,显然curv==2, 有三种可能:(1)取出最小的curv==1的pp,装入当前的 ...
- SharePoint 2010 母版页制作的简单介绍
转:http://www.cnblogs.com/jianyus/archive/2012/01/11/2319621.html 1. 首先打开SharePoint Designer 2010,找到 ...
- 水题:HDU 5119 Happy Matt Friends
Matt has N friends. They are playing a game together.Each of Matt's friends has a magic number. In t ...
- HDOJ -- 4632 区间DP
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- Nido.Common.Utilities.MD5类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- 作品第一课----循环改变DIV颜色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery获取属性之自己遇到的问题
刚开始是这种写法 用的 attr 结果获取不到 if($("#reg_username_span").attr("display") == 'block') ...
- 1 weekend110的hdfs源码跟踪之打开输入流 + hdfs源码跟踪之打开输入流总结
3种形式的元数据,fsimage是在磁盘上,meta.data是在内存上, 我们继续,前面呢,断点是打在这一行代码处, FileSystem fs = FileSystem.get(conf); we ...