一、Map接口概述:
- Map集合基于键(key)/值(value)映射。每个键最多只能映射一个值。键可以是任何引用 数据类型的值,不可重复;值可以是任何引用数据类型的值,可以重复;键值对存放无序。
- Map常用实现类有:HashMap, LinkedHashMap和Properties.
- Map不可以直接实例化,需要通过其实现类进行实例化.
二、Map接口常用方法总结:
注:该博客代码中引包代码均省略,eclipse用户可通过CTRL+shift+o来进行快捷引包
- put(K key, V value) 将键(key)/值(value)映射存放到Map集合中
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);//此时该HashMap中存储了"Tom"-91这一组键值对
}
}
- get(Object key) 返回指定键所映射的值,没有该key对应的值则返回 null
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
int score = scores.get("Tom");
System.out.println(score);//此处输出为91
}
}
- size() 返回Map集合中数据数量
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
int numbers = scores.size();
System.out.println(numbers);//此处输出为3
}
}
- clear() 清空Map集合
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
scores.clear();
int numbers = scores.size();
System.out.println(numbers);//此处输出为0
}
}
- isEmpty () 判断Map集合中是否有数据,如果没有则返回true,否则返回false
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
System.out.println(scores.isEmpty());//此处输出为false
scores.clear();
System.out.println(scores.isEmpty());//此处输出为ture
}
}
- remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值。
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
System.out.println(scores.remove("Tom"));//此处输出91
System.out.println(scores.get("Tom"));//此处输出为null
}
}
- values() 返回Map集合中所有value组成的以Collection数据类型格式数据。 eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
System.out.println(scores.values());//此处输出为[91, 89, 95]
}
}
- containsKey(Object key) 判断集合中是否包含指定键,包含返回 true,否则返回false
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
System.out.println(scores.containsKey("Tom"));//此处输出ture
System.out.println(scores.containsKey("tom"));//此处输出false
}
}
- containsValue(Object value) 判断集合中是否包含指定值,包含返回 true,否则返回false
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
System.out.println(scores.containsValue(91));//此处输出为ture
System.out.println(scores.containsValue(100));//此处输出为false
}
}
- keySet() 返回Map集合中所有key组成的Set集合
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
System.out.println(scores.keySet());//输出为[Tom, Jack, Jim]
}
}
- entrySet() 将Map集合每个key-value转换为一个Entry对象并返回由所有的Entry对象组成的Set 集合
eg:
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<String, Integer>();
scores.put("Tom", 91);
scores.put("Jim", 89);
scores.put("Jack", 95);
System.out.println(scores.entrySet());//输出为:[Tom=91, Jack=95, Jim=89]
}
}