Map接口概述:
1、Map集合基于键(key)/值(value)映射。每个键最多只能映射一个值。键可以是任何引用数据类型的值,不可重复;值可以是任何引用数据类型的值,可以重复;键值对存放无序。
Map常用实现类:
1、HashMap:允许使用 null值和 null键;此类不保证映射的顺序;在多线程操作下不安全。
2、LinkedHashMap
3、Properties
Map接口常用方法:
1、put(K key, V value) 将键(key)/值(value)映射存放到Map集合中
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();//向HashMap中添加元素 key-value
map.put("Tom", 100);//向HashMap中添加元素key——value
}
}
泛型不能使用基本数据类型,对应的包装类(byte-Byte short-Short int-Integer float-Float double-Double boolean-Boolean char-Charactor)
2、get(Object key) 返回指定键所映射的值,没有该key对应的值则返回 null
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int scores=map.get("Tom");
System.out.println(scores);
}
}
3、size() 返回Map集合中数据数量
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("Tom", 100);//向HashMap中添加元素key——value
int scores=map.get("Tom");
System.out.println(map.size());//获取HashMap集合容器中有多少对key——value
}
}
4、clear() 清空Map集合
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
map.clear();//成对清空
}
}
5、isEmpty () 判断Map集合中是否有数据,如果没有则返回true,否则返回false
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
map.clear();
System.out.println(map.isEmpty());//HashMap为空则为true
}
}
6、remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");//删除HashMap元素,返回value
System.out.println(score1);
}
}
7、values() 返回Map集合中所有value组成的以Collection数据类型格式数据。
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");//删除HashMap元素,返回value
System.out.println(score1);
map.put("Lucy", 90);
map.put("Jim", 91);
Collection<Integer>con =map.values();//(了解)有HashMap value组成的集合
for (int scores : con) {
System.out.println(scores);
}
}
}
8、containsKey(Object key) 判断集合中是否包含指定键,包含返回 true,否则返回false
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");
System.out.println(score1);
set.containsKey("Tom");
}
}
}
9、containsValue(Object value) 判断集合中是否包含指定值,包含返回 true,否则返回false
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>(); map.put("Tom", 100);
int score1 =map.remove("Tom");
System.out.println(score1);
set.containsValue("100");
}
}
10、keySet() 返回Map集合中所有key组成的Set集合
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
Set<String> set=map.keySet();
for (String key : set) {
System.out.println(key+map.get(key));
}
Iterator<String>iterator =set.iterator();
while(iterator.hasNext()) {
String key = iterator.next();
System.out.println(key+map.get(key));
}
}
11、entrySet() 将Map集合每个key-value转换为一个Entry对象并返回由所有的Entry对象组成的Set集合
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<String,Integer> map=new HashMap<String,Integer>();
Set<Entry<String, Integer>>set1=map.entrySet();//entry类每一对key-value变成一个entry对象,返回由entry对象组成的Set集合
for (Entry<String, Integer> entry : set1) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
Iterator<Entry<String, Integer>> iterator=set1.iterator();
while(iterator.hasNext()) {
Entry<String, Integer>entry=iterator.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}