Java-学习Map复习总结

Map<K,V>双边队列

1. 什么是Map?

1.1 Map双边队列的概念:
  • Map是一种以键(key)值(value)对形式保存数据的机制。
1.2 Map双边队列的格式:
interface Map<K,V> {

}
1.3 Map双边队列的两大实现类:
  1. HashMap<K,V>: 底层采用的是哈希表的存储机制。
  2. TreeMap<K,V>: 底层采用的是平衡二叉树的存储机制。

2. Map<K,V>的特征

  1. Map双边队列中key是唯一的,不可以重复,但是value值可以重复。
  2. 需要为key提供对应的比较方式(如果添加的元素没有自然顺序的话)(Comparable或者Comparator)。
    关于Comparator比较器的使用:
public class Demo3 {
	public static void main(String[] args) {
		//因为String类型带有自然顺序,所以不需要为其提供比较方式
		TreeMap<String, String> map = new TreeMap<String,String>();
		
		map.put("李四", "1");
		map.put("王五", "1");
		map.put("赵六", "1");
		map.put("张三", "1");
		
		System.out.println(map);
	
		//Dog类是自定义的实体类,没有自然顺序,使用Dog类对象作为Key值时,
		//需要为其提供比较方式
		TreeMap<Dog, String> map2 = new TreeMap<Dog,String>(new Comparator<Dog>() {

			@Override
			public int compare(Dog o1, Dog o2) {
				
				return o1.getAge() - o2.getAge();
			}
			
		});
		
		map2.put(new Dog("旺财", 5), "111"); 
		map2.put(new Dog("王可可", 1), "111"); 
		
		map2.put(new Dog("豆豆", 3), "111"); 
		map2.put(new Dog("老黄", 4), "111"); 
		map2.put(new Dog("八公", 2), "111"); 
	
		System.out.println(map2);
	}
}

3. Map<K,V>的常用方法:

  1. 增:
    public V put(K key , V value) :存入一个键值对类型,key和value都要符合泛型约束。
    public void putAll(Map<? extends K,? extends V> m) : 存入另外一个Map双边队列到当前Map中,并且要求添加的Map双边队列的k和v都要和当前Map中存储的k和v一致或者为其子类对象。
//创建一个Map对象,约束其K,V为String
HashMap<String, String> map = new HashMap<String,String>();
		
//添加键值对
map.put("吴京", "谢楠");
map.put("邓超", "娘娘");
map.put("黄磊", "孙莉");
map.put("王宝强", "XX");
		
System.out.println(map);
		
HashMap<String, String> map2 = new HashMap<String,String>();
		
map2.put("Tom", "Jerry");
map2.put("张晋", "蔡少芬");
map2.put("舒克", "贝塔");
map2.put("哪吒", "敖丙");
		
System.out.println(map2);
		
//添加另外一个Map双边队列到当前队列中
map.putAll(map2);
System.out.println(map);
  1. 删:
    public V remove (Object key) : 删除指定key对应的键值对,返回值是被删除的键值对中的value值。
//删除指定key对于的键值对
map.remove("Tom");
System.out.println(map);
  1. 改:
    public V put(K key , V value) :使用指定的value值替换掉指定key对应的键值对中的value。
//修改,使用指定的元素替换掉指定key对应键值对中的value
map.put("Tom", "老苏");
System.out.println(map);
  1. 查:
    public int size( ) : 得到当前Map双边队列中有效键值对的个数。
    public boolean isEmpty( ) : 判断当前Map双边队列是否为空。
    public boolean containsKey(Object key) : 判断包含当前指定key的键值对是否存在。
    public boolean containsValue(Obejct value) : 判断包含指定value的键值对是否存在。
    public V get(Object key) : 得到指定key值对应的键值对中的value值。
    public Set< K > keySet( ) : 得到当前Map双边队列中所有的key值,返回一个Set集合。
    public Collection< V > values( ) : 得到当前Map双边队列中所有的value值,返回一个Collection集合。
//当前Map双边队列的有效键值对的个数
System.out.println(map.size());
		
//判断当前Map双边队列是否为空
System.out.println(map.isEmpty());
		
//判断指定key的键值对是否存在
System.out.println(map.containsKey("Tom"));
		
//判断指定value的键值对是否存在
System.out.println(map.containsValue("老苏"));
		
//得到指定key值所对应键值对的value值
System.out.println(map.get("Tom"));
		
//得到当前Map双边队列中所有的key值,放入到Set集合当中
Set<String> keySet = map.keySet();
System.out.println(keySet);
		
//得到当前Map双边队列中所有的value值,放入到Collection集合中
Collection<String> values = map.values();
System.out.println(values);

public Set<Map.Entry<k,v>> entrySet( ) : 将当前Map双边队列中所有的键值对每一个分别包装为一个Map.Entry类对象,再将其输出为一个Set集合。

HashMap<String, String> map = new HashMap<String,String>();
	
map.put("吴京", "谢楠");
map.put("邓超", "娘娘");
map.put("黄磊", "孙莉");
map.put("王宝强", "XX");

//entrySet方法将当前Map双边队列中所有的键值对包装成Map.Entry对象
//返回一个Set集合
Set<Entry<String, String>> entrySet = map.entrySet();

4. Map<K,V>中的Entry<K,V>内部类:

  • 在Map双边队列中,有这么一个内部类,Entry<K,V>,可以将每一个键值对打包成一个Entry类对象进行使用。
  • Method:
    public Set<Map.Entry<k,v>> entrySet( ) : 将当前Map双边队列中所有的键值对每一个分别包装为一个Map.Entry类对象,再将其输出为一个Set集合。
    public K getKey( ) : Entry类当中的方法,使用Entry类对象调用,可以得到当前Entry对象中保存键值对中的Key值。
    public V getValue( ) : Entry类当中的方法,使用Entry类对象调用,可以得到当前Entry对象中保存键值对中的value值。
public class Demo2 {
	public static void main(String[] args) {
		HashMap<String, String> map = new HashMap<String,String>();
	
		map.put("吴京", "谢楠");
		map.put("邓超", "娘娘");
		map.put("黄磊", "孙莉");
		map.put("王宝强", "XX");
	
		//entrySet方法将当前Map双边队列中所有的键值对包装成Map.Entry对象
		//返回一个Set集合
		Set<Entry<String, String>> entrySet = map.entrySet();
		
		Iterator<Entry<String, String>> iterator = entrySet.iterator();
		
		while(iterator.hasNext()) {
			Entry<String, String> next = iterator.next();
			
			System.out.println("Key :" + next.getKey() + " Value :" + next.getValue());
		}
	
	}
}

5.补充-遍历Map的四种方式

public class Test{
     public static void main(String[] args) {
      Map<String, String> map = new HashMap<String, String>();
      map.put("1", "value1");
      map.put("2", "value2");
      map.put("3", "value3");
      
      //第一种:普遍使用,二次取值
      System.out.println("通过Map.keySet遍历key和value:");
      for (String key : map.keySet()) {
       System.out.println("key= "+ key + " and value= " + map.get(key));
      }
      
      //第二种
      System.out.println("通过Map.entrySet使用iterator遍历key和value:");
      Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
      while (it.hasNext()) {
       Map.Entry<String, String> entry = it.next();
       System.out.println("key= " + entry.getKey() + 
       					" and value= " +entry.getValue());
      }
      
      //第三种:推荐,尤其是容量大时
      System.out.println("通过Map.entrySet遍历key和value");
      for (Map.Entry<String, String> entry : map.entrySet()) {
       System.out.println("key= " + entry.getKey() + 
       					" and value= " + entry.getValue());
      }
    
      //第四种
      System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
      for (String v : map.values()) {
       System.out.println("value= " + v);
      }
     }
}
发布了12 篇原创文章 · 获赞 13 · 访问量 3932

猜你喜欢

转载自blog.csdn.net/cccccv_/article/details/104657715