首页 > 编程语言 > 详细

Java基础知识强化之集合框架笔记54:Map集合之HashMap集合(HashMap<String,String>)的案例

时间:2015-10-09 21:21:21      阅读:344      评论:0      收藏:0      [点我收藏+]

1. HashMap集合

HashMap集合(HashMap<String,String>)的案例

 

2. 代码示例:

 1 package cn.itcast_02;
 2 
 3 import java.util.HashMap;
 4 import java.util.Set;
 5 
 6 /*
 7  * HashMap:是基于哈希表的Map接口实现。
 8  * 哈希表的作用是用来保证键的唯一性的。
 9  * 
10  * HashMap<String,String>
11  * 键:String
12  * 值:String
13  */
14 public class HashMapDemo {
15     public static void main(String[] args) {
16         // 创建集合对象
17         HashMap<String, String> hm = new HashMap<String, String>();
18 
19         // 创建元素并添加元素
20         // String key1 = "it001";
21         // String value1 = "马云";
22         // hm.put(key1, value1);
23 
24         hm.put("it001", "马云");
25         hm.put("it003", "马化腾");
26         hm.put("it004", "乔布斯");
27         hm.put("it005", "张朝阳");
28         hm.put("it002", "裘伯君"); // wps
29         hm.put("it001", "比尔盖茨");
30 
31         // 遍历
32         Set<String> set = hm.keySet();
33         for (String key : set) {
34             String value = hm.get(key);
35             System.out.println(key + "---" + value);
36         }
37     }
38 }

运行效果,如下:

技术分享

 

HashMap:是基于哈希表的Map接口实现。哈希表的作用是用来保证键的唯一性的。HashMap 中作为键的对象必须重写Object的hashCode()方法和equals()方法。这里String类已经重写了Object的hashCode()方法和equals()方法。所以这里可以这样使用。

技术分享

Java基础知识强化之集合框架笔记54:Map集合之HashMap集合(HashMap<String,String>)的案例

原文:http://www.cnblogs.com/hebao0514/p/4865010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!