Java基础知识强化之集合框架笔记55:Map集合之HashMap集合(HashMap)的案例

1. HashMap集合(键是Integer,值是String的案例)

2. 代码示例:

 package cn.itcast_02;

 import java.util.HashMap;
import java.util.Set; /*
* HashMap<Integer,String>
* 键:Integer
* 值:String
*/
public class HashMapDemo2 {
public static void main(String[] args) {
// 创建集合对象
HashMap<Integer, String> hm = new HashMap<Integer, String>(); // 创建元素并添加元素
// Integer i = new Integer(27);
// Integer i = 27;
// String s = "林青霞";
// hm.put(i, s); hm.put(27, "林青霞");
hm.put(30, "风清扬");
hm.put(28, "刘意");
hm.put(29, "林青霞"); // 下面的写法是八进制,但是不能出现8以上的单个数据
// hm.put(003, "hello");
// hm.put(006, "hello");
// hm.put(007, "hello");
// hm.put(008, "hello"); // 遍历
Set<Integer> set = hm.keySet();
for (Integer key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
} // 下面这种方式仅仅是集合的元素的字符串表示
// System.out.println("hm:" + hm);
}
}

运行结果,如下:

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

上一篇:关于 android中的组件监听


下一篇:android 退出程序提示是否退出对话框