/* * 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集合的遍历,根据键查询值 * * 思路: * 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);
}
}
}
/* * 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
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有