赞
踩
1.Map接口[1]
Map接口,用于存储具有“键”“值”一一映射的数据对,也即同时存储具有两个数据对象的对象。
注意:键不能为null
1.1 Map的基本操作
实例化
Map map = new HashMap();
插入元素
map.put("xiao ming", 98);
获取键对应的值
int score = map.get("xiao ming");
移除键值对
map.remove("xiao ming");
更新键值对
map.remove("xiao ming");
map.put("xiao ming", 100);
清空map
map.clear();
1.2 Map数据结构遍历
遍历常用到的函数
1.Set entrySet( )
返回此映射中包含的映射关系的 Set 视图。
2.Set keySet( )
返回此映射中包含的键的 Set 视图。
1.2.1 增强型for循环
entrySet()遍历
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + " :" + entry.getValue());
}
keySet()遍历
for (String key : map.keySet()) {
System.out.println(key + " :" + map.get(key));
}
1.2.2 迭代器Iterator遍历
1.entrySet()遍历
Iterator> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
System.out.println(entry.getKey() + " :" + entry.getValue());
}
keySet()遍历
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + " :" + map.get(key));
}
1.3 底层实现
待续
2.Set接口
2.1 Set的基本操作
实例化
Set set = new HashSet();
插入元素
set.add("xiao ming");
移除set中指定的元素
set.remove("xiao ming");
返回一个包含 set 中所有元素的数组
String[] string = set.toArray();
清空set
set.clear();
2.2 Set数据结构遍历
2.2.1 增强型for循环
for(String value: set){
System.out.println(value);
}
2.2.2 迭代器Iterator遍历
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
2.3 set底层实现
待续
3.HashMap类
HashMap实现了接口Map,就是说HashMap实现了Map所有的方法。所有基本和Map的操作是一样的
3.1 HashMap的基本操作
实例化
Map map = new HashMap();
插入元素
map.put("xiao ming", 98);
获取键对应的值
int score = map.get("xiao ming");
移除键值对
map.remove("xiao ming");
更新键值对
map.remove("xiao ming");
map.put("xiao ming", 100);
清空map
map.clear();
3.2 Map数据结构遍历
4.HashSet类
HashSet实现了接口Set,就是说HashSet实现了Set所有的方法。所有基本和Set的操作是一样的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。