双列集合的特点:
1、双列集合一次需要存一对数据,分别为键和值
2、键不能重复,值可以重复
3、键和值是一 一对应的,每一个键只能找到自己对应的值
4、键 + 值这个整体,我们称之为 “ 键值对 ” 或者 “ 键值对对象 ” ,在Java中叫做 “ Entry对象 ”
Map的常见API
Map是双列集合的顶层接口,它的功能是全部双列集合都可以继承使用的
//添加元素
V put(K key,V value)
//根据键删除键值对元素
V remove(Object key)
//移除所有的键值对元素
void clear()
//判断集合是否包含指定的键
boolean containsKey(Object key)
//判断集合是否包含指定的值
boolean containsValue(Object value)
//判断集合是否为空
boolean isEmpty()
//集合的长度,也就是集合中键值对的个数
int size()
具体使用:
public static void main(String[] args) {
//创建Map的对象
//因为Map本身为一个接口,所以不能创建其对象
//只能创建其实现类的对象
//Map接口中含有两个泛型K,V,所以创建时
//需要对泛型进行指定类型
Map<String, String> m = new HashMap<>();
//添加元素
//1、添加元素
//2、覆盖元素
//在添加数据的时候,如果键不存在,那么直接把键值对对象添加到map集合中,方
法返回null
//如果键是存在的,那么会把原有的键值对对象覆盖,会把覆盖的值进行返回
m.put("a", "aa");
m.put("b", "bb");
m.put("c", "cc");
//{a=aa, b=bb, c=cc}
//删除元素
//返回被删除的键值对的键值
//1、当remove方法中只有一个参数时,此参数为Key,指删除当前Key的键值对,
并返回删除的键值对
//2、当remove方法中有两个参数时,此时参数为Key和Value,指删除当前Key和
Value同时满足的键值对
//若删除成功则返回true,否则返回false
m.remove("a");
//{b=bb, c=cc}
//清空元素
//m.clear();
//{}
//判断是否包含
boolean keyResult1 = m.containsKey("b");
System.out.println(keyResult1);
boolean keyResult2 = m.containsKey("d");
System.out.println(keyResult2);
boolean valueResult1=m.containsValue("bb");
System.out.println(valueResult1);
boolean valueResult2=m.containsValue("dd");
System.out.println(valueResult2);
//true
//false
//true
//false
//判断集合是否为空
boolean result=m.isEmpty();
//false
//集合的长度
int size=m.size();
System.out.println(size);
//2
//打印集合
System.out.println(m);
//{a=aa, b=bb, c=cc}
}
Map的遍历方式
1、键找值:keySet()
2、键值对:entrySet()
3、Lambda表达式:forEach()
1、键找值:获取所有的键并将其放入到单列集合中
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
//键找值
//获取所有的键并将其放入到单列集合中
Set<String> keys = map.keySet();
//增强for
for (String key : keys) {
String value = map.get(key);
System.out.println(key + "=" + value);
}
//Lambda表达式
keys.forEach((String s)-> {
String s1 = map.get(s);
System.out.println(s + "=" + s1);
});
//迭代器
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String result = iterator.next();
//利用map集合中的键获取对应的值:get
System.out.println(result + "=" + map.get(result));
}
//a=aa
//b=bb
//c=cc
}
2、键值对
public static void main(String[] args) {
/**
* Map集合的遍历方式
* 2、键值对
*/
Map<String, String> map = new HashMap<>();
map.put("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
Set<Map.Entry<String, String>> entries = map.entrySet();
//增强for
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "=" + value);
}
//Lambda表达式
entries.forEach(stringStringEntry -> {
String key = stringStringEntry.getKey();
String value = stringStringEntry.getValue();
System.out.println(key + "=" + value);
});
//迭代器
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
String key = next.getKey();
String value = next.getValue();
System.out.println(key + "=" + value);
}
//a=aa
//b=bb
//c=cc
}
3、Lambda表达式
public static void main(String[] args) {
/**
* Map集合的遍历方式
* 3、Lambda表达式
*/
Map<String, String> map = new HashMap<>();
map.put("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
map.forEach((key, value) ->
System.out.println(key + "=" + value));
}
下面将会讲解Map集合的所有实现类
觉得有用就点点赞吧~