目录
Map接口重要方法
java.util.Map接口中的常用方法; 1、Map和collection没有继承关系。 2、Map集合以key和value的方式存储数据:键值对。 key和value都是引用数据类型. key和value都是存储对象的内存地址。 key起到主导的地位,value是key的一个附属品。 3、Map集合中的常用方法: V put(K key, V value) 向Map集合中添加某个键值对 V get(Object key) 通过Key获取value void clear() 清空Map集合 boolean containsKey(Object key) 判断Map集合中是否包含某个Key boolean containsValue(Object value) 判断Map中是否包含某个value boolean isEmpty() 判断Map集合中元素个数是否为0 Set<K> keySet() 获取Map集合中所有的Key V remove(Object key) 通过K删除键值对 int size() 获取Map集合中键值对的个数。 Collection<V> values() 获取Map集合中所有的value,返回一个Collection Set<Map.Entry<K,V>> entrySet() 将Map集合转换成Set集合 假设现在有一个Map集合,如下所示: Map1集合对象 key value ---------------------- 1 zhangsan 2 lisi 3 wangwu 4 zhaoliu Set set = map1.entrySet(); Set集合对象 1=zhangsan 【注意:Map集合通过entrySet()方法转换成Set这个集合,Set集合中元素的类型是Map.Entry<K,V>】 【Map.Entry和String一样,都是一种类型名字,只不过:Map.Entry是静态内部类,是Map中的静态内部类】 2=lisi 3=wangwu 4=zhaoliu ------->这个东西是什么?Map.Entry
package edu.tjdz.javaSE.collection.Map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class MapTest01 {
public static void main(String[] args) {
// 创建Map集合的集合对象
Map<Integer,String> map = new HashMap<>();
// 向map集合中添加键值对
map.put(1,"zhangsan");
map.put(2,"lisi");
map.put(3,"wangwu");
map.put(4,"zhaoliu");
// 通过key获取Value
String value = map.get(2);
System.out.println(value);
//获取键值对数量
System.out.println("键值对的数量:"+map.size());
//通过key删除value
map.remove(2);
System.out.println("键值对的数量:"+map.size());
//判断是否包含某个key
//contains方法底层调用的都是equals进行对比的,所以自定义的类型要看重写的equals方法。
System.out.println(map.containsKey(new Integer(4))); //true
//判断是否包含某个Value
System.out.println(map.containsValue("wangwu")); //true
//获取所有的Value
Collection<String> values = map.values();
for(String s: values){
System.out.println(s);
}
//清空集合
map.clear();
System.out.println("键值对的数量:"+map.size());
//判断是否为空
System.out.println(map.isEmpty()); //true
}
}
Map集合的遍历
package edu.tjdz.javaSE.collection.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*
Map集合的遍历
*/
public class MapTest02 {
public static void main(String[] args) {
// 第一种方式:获取所有的key,通过遍历key,来遍历value
Map<Integer,String> map = new HashMap<>();
map.put(1,"zs");
map.put(2,"ls");
map.put(3,"ww");
map.put(4,"wjw");
map.put(5,"lyf");
/*
//遍历Map集合
//获取所有有的key,所有的key是一个Set集合
Set<Integer> keys = map.keySet();
//遍历key,通过key获取value
//迭代器对象
Iterator<Integer> it = keys.iterator();
/*
Integer key = it.next();
String value = map.get(key);
System.out.println(key +"=" +value);
}*/
/*
//foreach也可以
for(Integer key:keys){
System.out.println(key +" = "+ map.get(key));
}
*/
//第二种方式:Set<Map.Entry<K,V>> entrySet()
//以上这个方法是把Map集合全部转换成Set集合。
//Set集合中的类型是:Map.Entry
Set<Map.Entry<Integer, String>> set = map.entrySet();
/*
//遍历Set集合,每一次取出一个Node
//迭代器
Iterator<Map.Entry<Integer,String>> it1 = set.iterator();
while(it1.hasNext()){
Map.Entry<Integer,String> node = it1.next();
Integer key = node.getKey();
String value = node.getValue();
System.out.println(key + " = " +value);
}
*/
//foreach
//这种方式效率比较高,适用于大数据量
for(Map.Entry<Integer,String> node : set){
System.out.println(node.getKey()+" = "+node.getValue());
}
}
}