java hashmap
/*
* map集合的特点
* 将键映射值的对象,一个映射不能包含重复的值;每个键最多只能映射到一个值
*
* map集合和collection集合的区别?
* map集合存储元素是成对出现的,map集合的键是唯一的,就是可重复的。可以把这个理解为:夫妻对
* collection集合存储元素是单独出现的,collection的儿子set是唯一的,list是可重复的,可以把这个理解为:光棍
*
* 注意:
* map集合的数据结构值针对键有效,限值无效
* collection集合的数据结构是针对元素有效
*
* map集合的功能概述:
* 1:添加功能
* v put(k key,v value);//添加元素
* 如果键是第一次存储,就直接存储元素,返回null
* 如果键不是第一次存储,就用值把以前的值替换掉,返回以前的值
*
* 2:删除功能
* void clear();//移除所有的键值对元素
* v remove(object key);//根据键删除键值对元素,并把值返回
*
* 3:判断功能
* boolean containskey(object key);//判断集合是否包含指定的键
* boolean containsvalue(object value);//判断集合是否包含指定的值
* boolean isempty();//判断集合是否为空
*
* 4:获取功能
* set<map,entry<e,v>> entryset();获取键值对的对象集合
* v get(object key);//根据键获取值
* set<k> keyset();//获取集合中所有键的集合
* collection<v> values();//获取集合中所有值的集合
*
* 5:长度功能
* int size();//返回集合中的键值对的对数
* */
map集合的遍历
方式1,根据键查询值
获取所有键的集合
遍历键的集合,获取每一个键
根据键,查询值
方式2,根据键值对的对象查询键和值
获取所有键值对的对象的集合
遍历键值对的对象的集合,获取到每一个键值对的对象
根据键值对的对象,查询键和值
方式1,根据键查询值
/*
* map集合的遍历,根据键查询值
*
* 思路:
* a:获取所有的键
* b:遍历键的集合,获取得到每一个键
* c:根据键查询值
* */
import java.util.hashmap;
import java.util.map;
import java.util.set;
/*
* map集合的遍历,根据键查询值
*
* 思路:
* a:获取所有的键
* b:遍历键的集合,获取得到每一个键
* c:根据键查询值
* */
public class integerdemo {
public static void main(string[] args) {
// todo auto-generated method stub
map<string, string> map = new hashmap<string, string>();
map.put("hello", "world");
map.put("java", "c++");
map.put("sql", "os");
system.out.println(map);
// a:获取所有的键
set<string> set = map.keyset();
// b:遍历键的集合,获取得到每一个键
for (string key : set) {
// c:根据键查询值
string value = map.get(key);
system.out.println(key + "---" + value);
}
}
}
方式2,根据键值对的对象查询键和值
/*
* map集合的遍历,根据对象查询键和值
*
* 思路:
* a:获取所有的键值对对象的集合
* b:遍历键值对对象的集合,得到每一个键值对的对象
* c:获取键和值
* */
import java.util.hashmap;
import java.util.map;
import java.util.set;
/*
* map集合的遍历,根据对象查询键和值
*
* 思路:
* a:获取所有的键值对对象的集合
* b:遍历键值对对象的集合,得到每一个键值对的对象
* c:获取键和值
* */
public class integerdemo {
public static void main(string[] args) {
// todo auto-generated method stub
map<string, string> map = new hashmap<string, string>();
map.put("hello", "world");
map.put("java", "c++");
map.put("sql", "os");
system.out.println(map);
// a:获取所有的键值对对象的集合
set<map.entry<string, string>> set = map.entryset();
// b:遍历键值对对象的集合,得到每一个键值对的对象
for (map.entry<string, string> me : set) {
// c:获取键和值
string key = me.getkey();
string value = me.getvalue();
system.out.println(key + "---" + value);
}
}
}
/*
* 1:hashmap和hashtable的区别?
* hashmap线程不安全,效率高,允许null键和null值
* hashtable线程安全,效率低,不允许null键和null值
*
* 2:list,set,map等接口是否都继承于map接口?
* list,set不是继承自map接口,它们继承自collection接口
* map接口本身就是一个顶层接口
* */
import java.util.hashmap;
import java.util.hashtable;
public class integerdemo {
public static void main(string[] args) {
// todo auto-generated method stub
hashmap<string, string> hm = new hashmap<string, string>();
hashtable<string, string> ht = new hashtable<string, string>();
hm.put("hello", "world");
hm.put("java", "c++");
hm.put(null, "sql");
ht.put("hello", "world");
ht.put("java", "c++");
ht.put(null, "sql");// exception in thread "main"
// java.lang.nullpointerexception
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
更多java hashmap详解及实例代码。