一.特征
通过键-值(key-value)对的形式来存储数据
Map的实现:HashMap
Map中,key可以为任意类型,但这里建议使用String,value也可以是任意类型
Map里面多个value可以是不同类型
Map里面key是可以重复的,当key重复时,后存入的数据会覆盖前面的数据
Map里面,value可以重复.
Map里面的key可以为null,但是只能有一个,多个的时候,后面的会覆盖前面的
Map中value可以是null,多个value可以同时为null
二.HashMap原理
,哈希表存储采用数组+链表+红黑树实现
当链表长度超过阈值(8),先判断table的长度是否大于64,就转换为红黑树,这样大大减少了查询时间
如果小于64,就通过扩容的方式来解决,避免红黑树结构化.
三.HashMap的遍历
1.通过entrySet()方法,得到一个Entry对象的集合,这里用Set来装载.
2.通过Set中的迭代器,迭代出每一个Entry对象.
3.针对每一个Entry对象,通过getKey()和getValue()方法,来分别获取到对应的key和value
四.泛型
Map<String,Object> map = new HashMap<String,Object>();
五,Collections 集合的工具类
public static <T>
boolean addAll(Collection<T> c, T... elements): 往集合中添加一些元素
public static void shuffle(List<?> list): 打乱集合顺序
public static <T> void sort(List<T> list): 将集合中元素按照默认规则排序
public static <T> void sort(List<T> list,Comparator<? super T>): 将集合中元素按照指定规则排序
六,Map接口常用方法
1.clear
方法全称为:void clear(),移除map里所有的映射关系,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
map.clear();
}
2.containsKey
方法全称为:boolean containsKey(Object key),containsKey底层调用了equals方法,查询key中是否包含某个元素,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
boolean res = map.containsKey(3);
System.out.println(res);
}
3.containsValue
方法全称为:boolean containsValue(Object value),查询value中是否包含某个元素,containsValue底层用了equals方法,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
boolean res = map.containsValue("张三");
System.out.println(res);
}
4.get
方法全称为:Object get(Object key),查询key对应的value值,返回value值,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
Object o = map.get(3);
System.out.println(o);
}
5.isEmpty
方法全称为:boolean isEmpty(),用来判断map集合中是否为空,若为空则返回true,使用方法如下:
public static void main(String[] args)
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
boolean res = map.isEmpty();
System.out.println(res);
}
6.keySet
方法全称为:Set keySet(),用来获取map集合中所有的key,并将所有的key中存入set集合中,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
Set set = map.keySet();
for (Object o : set) {
System.out.println(o);
}
}
7.put
方法全称为:put(Object key,Object value),给map集合中添加键值对,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
}
8.remove
方法全称为:remove(),利用key删除map集合中的元素,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
map.remove(3);
}
9.size
方法全称为:int size(),获取map集合中键值对的个数,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
int i = map.size();
System.out.println(i);
}
10.values
方法全称为:Collection values(),获取map集合中所有的value值,并将value值存入Collection集合中返回,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
Collection values = map.values();
//Collection遍历也可用迭代和获取下标的方法
for (Object value : values) {
System.out.println(value);
}
}
11.entrySet
方法全称为:Set(Map.Entry<K,V>) entrySet(),将map集合转换为set集合,set集合中元素的类型是Map.entry<K,V>,Map.Entry和String一样,都是一种类型的名字,只不过Map.Entry是静态内部类,是Map中的静态内部类,使用方法如下:
public static void main(String[] args) {
Map map=new HashMap();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
Set set = map.entrySet();
for (Object o : set) {
System.out.println(o);
}
}
12.map集合的遍历
第一种方式:
map集合不能迭代,也不能使用foreach,所有只能先获取去所有的key,遍历key然后通过key获取每一个key和value,代码和结果如下:
public static void main(String[] args) {
Map<Integer,String> map=new HashMap<>();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
Set<Integer> keys = map.keySet();
for (Integer key : keys) {
String value = map.get(key);
System.out.println("key="+key+",value="+value);
}
}
第二种方式:
把map集合全部转换成set集合,set集合中元素的类型是Map.Entry,取出Map.entry对象之后,可以获取到key和value,代码和结果如下:
public static void main(String[] args) {
Map<Integer,String> map=new HashMap<>();
map.put(1,"张三");
map.put(2,"李四");
map.put(3,"王麻子");
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey();
String value = entry.getValue();
}
}