✈【【零基础 快速学Java】韩顺平 零基础30天学会Java】
JDK 8 的Map 接口特点
【举个栗子】
package com.dingjiaxiong.map_;
import java.util.HashMap;
import java.util.Map;
/**
* ClassName: Map_
* date: 2022/9/5 19:58
*
* @author DingJiaxiong
*/
@SuppressWarnings({"all"})
public class Map_ {
public static void main(String[] args) {
Map map = new HashMap();
map.put("no1","韩老师"); // k - v
map.put("no2","张无忌"); // k-v
map.put("no1","张三丰"); //当出现相同的k , 则进行替换
map.put("no3","张三丰");
map.put(null,null);
map.put(null,"abc");
map.put("no4",null);
map.put("no5",null);
map.put(1,"赵敏");
map.put(new Object(),"银角大王");
System.out.println(map.get("no2")); //根据k 获取 v
System.out.println("map = " + map);
}
}
运行结果
package com.dingjiaxiong.map_;
import java.util.HashMap;
import java.util.Map;
/**
* ClassName: MapMethod
* date: 2022/9/5 20:03
*
* @author DingJiaxiong
*/
@SuppressWarnings({"all"})
public class MapMethod {
public static void main(String[] args) {
Map map = new HashMap();
map.put("邓超",new Book("",100));
map.put("邓超","孙俪");
map.put("xxx","yyy");
map.put("aaa",null);
map.put(null,"ccc");
System.out.println("map = " + map);
// remove 根据键 删除映射关系
map.remove(null);
System.out.println("map = " + map);
//get 根据键获取值
System.out.println(map.get("aaa"));
//size 获取元素个数
System.out.println(map.size());
//isEmpty 判断个数是否为0
System.out.println(map.isEmpty()); //F
//clear 清除k - v
map.clear();
System.out.println("map = " + map);
//containsKey : 查找键是否存在
System.out.println(map.containsKey("邓超"));//F
}
}
class Book{
private String name ;
private int num;
public Book(String name, int num) {
this.name = name;
this.num = num;
}
}
运行结果
【遍历原理图】
【举个栗子】
package com.dingjiaxiong.map_;
import java.util.*;
/**
* ClassName: MapFor
* date: 2022/9/5 20:11
*
* @author DingJiaxiong
*/
@SuppressWarnings({"all"})
public class MapFor {
public static void main(String[] args) {
Map map = new HashMap();
map.put("邓超","孙俪");
map.put("xxx","yyy");
map.put("aaa",null);
map.put(null,"ccc");
//1 先取出所有key,通过key取出对应的value
Set keyset = map.keySet();
//增强for
System.out.println("======第一种方式=======");
for (Object key : keyset){
System.out.println(key + "-" + map.get(key));
}
//迭代器
System.out.println("======第二种方式=======");
Iterator iterator = keyset.iterator();
while (iterator.hasNext()){
Object key = iterator.next();
System.out.println(key + "-" + map.get(key));
}
//2 把所有的values取出
Collection values = map.values();
//增强for
System.out.println("---取出所有的 value 增强 for----");
for (Object value : values){
System.out.println(value);
}
//迭代器
System.out.println("---取出所有的 value 迭代器----");
Iterator iterator1 = values.iterator();
while (iterator1.hasNext()){
Object value = iterator1.next();
System.out.println(value);
}
//3 通过 EntrySet 来获取 k- v
Set entrySet = map.entrySet();
//增强for
System.out.println("----使用 EntrySet 的 for 增强----");
for(Object entry : entrySet){
//将entry 转为 Map.Entry
Map.Entry m = (Map.Entry) entry;
System.out.println(m.getKey() + "-" + m.getValue());
}
//迭代器
System.out.println("----使用 EntrySet 的 迭代器----");
Iterator iterator2 = entrySet.iterator();
while (iterator2.hasNext()){
Object entry = iterator2.next();
Map.Entry m = (Map.Entry) entry;
System.out.println(m.getKey() + "-" + m.getValue());
}
}
}
运行结果