Map容器(Java)

1.容器介绍

1.1 容器接口结构

  a. Java存储结构
 Java当中按照存储结构可以划分成为两个大类,分别是单列集合 java.util.Collection 和 双列集合 java.util.Map, 顾名思义Collection是实现单列集合的根接口, 而Map则是实现双列集合(键值对)的根接口。

  b. 双列接口Map
在这里插入图片描述
 双列接口即为Map, 双列接口提供了一种映射关系即键值对的映射关系, 这是Map集合的核心即键–值对关系的存在,与此同时此接口下面也提供了许多不同的实现类,今天我们使用HashMap这个实现类进行Map通用方法的举例和应用.

  c. Map接口 : 键值对 + 键唯一
  需要注意的是Map接口的共同属性肯定是键值对 + 键唯一的特性的, 但是对于不同底层实现类所实现的Map接口可能因为使用的数据结构不同所导致会有一些性能的不同 ,这些性能的不同可能体现在存储顺序方面(有序或者无序)等,因为不同的底层数据结构的实现就会导致不同的结果,例如对于HashMap单纯的运用哈希表来完成这个结构,那么显然它是无法记录存储顺序的,但是对于LinkedHashMap 的实现就不一样,它还往里面加入了一个双重列表,此列表会记录下迭代的顺序从而保证了之后访问的顺序可以和存储的顺序一致。

1.2 简单解析

 A. 包路径:java.util.Map
 B. Map核心:键值对 + 键唯一
 C. 注意:下面我们将使用HashMap<K, V> 这个实现类进行Map接口的实现和举例,我们将举例的是Map接口的通用方法,对于所有Map接口下的实现类都可以通用
 D. 常用实现类举例:
  HashMap实现类 : 无序 + 键唯一 + 值可重复
在这里插入图片描述

2. 容器创建(Member functions)

  HashMap实现类创建 :

import java.util.HashMap;
import java.util.Set;
public class Main {
    
    
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
	}
}

3. 访问操作(Element access)

  元素获取
在这里插入图片描述

3.1 keySet()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;
public class Main {
    
    
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
		hashMap.put(1,10); hashMap.put(2, 20);

		Set<Integer> set = hashMap.keySet();
		for(Integer key : set){
    
    
			Integer value = hashMap.get(key);
			System.out.println(key + " " + value); // 1 10 --- 2 20
		}
	}
}

3.2 entrySet()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;
public class Main {
    
    
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
		hashMap.put(1,10); hashMap.put(2, 20);

		Set<Map.Entry<Integer, Integer>> set = hashMap.entrySet();
		for(Map.Entry<Integer, Integer> it : set){
    
     // 1 10 -- 2 20
			Integer key = it.getKey();
			Integer value = it.getValue();
			System.out.println(key + " " + value);
		}
	}
}

4. 修改操作(Modifiers)

4.1 put()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;
public class Main {
    
    
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
		hashMap.put(1,10); hashMap.put(2, 20);

		Set<Integer> set = hashMap.keySet();
		for(Integer key : set){
    
    
			Integer value = hashMap.get(key);
			System.out.println(key + " " + value); // 1 10 --- 2 20
		}
	}
}

4.2 remove()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;
public class Main {
    
    
	public static void print(HashMap<Integer, Integer> hashMap){
    
    
		Set<Integer> set = hashMap.keySet();
		for(Integer key : set){
    
    
			Integer value = hashMap.get(key);
			System.out.println(key + " " + value); // 1 10 --- 2 20
		}
	}
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

		hashMap.put(1,10); hashMap.put(2, 20);
		print(hashMap); // 1 10 --- 2 20
		hashMap.remove(1);
		print(hashMap); // 2 20
	}
}

4.3 clear()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;

public class Main {
    
    
	public static void print(HashMap<Integer, Integer> hashMap){
    
    
		Set<Integer> set = hashMap.keySet();
		for(Integer key : set){
    
    
			Integer value = hashMap.get(key);
			System.out.println(key + " " + value); // 1 10 --- 2 20
		}
	}
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

		hashMap.put(1,10); hashMap.put(2, 20);
		hashMap.clear();
		print(hashMap); // 
	}
}

5. 容量操作(Member functions)

5.1 size()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

		hashMap.put(1,10); hashMap.put(2, 20);
		System.out.println(hashMap.size()); // 2
	}
}

5.2 isEmpety()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

		hashMap.put(1,10); hashMap.put(2, 20);
		System.out.println(hashMap.isEmpty()); // false
	}
}

6. 其他操作(Other)

6.1 containsKey() / containsValue()

在这里插入图片描述

import java.util.HashMap;
import java.util.Set;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

		hashMap.put(1,10); hashMap.put(2, 20);
		System.out.println(hashMap.containsKey(1)); // true
		System.out.println(hashMap.containsKey(3)); // false
		System.out.println(hashMap.containsValue(20)); // true
		System.out.println(hashMap.containsValue(15)); // false
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_51566349/article/details/131224482