Map,Set都会用到 [key,value],但Set中的value是用一个PRESENT常量来替代,实际过程中并没有使用。
特点
- Map与Collection并列共存,用于保存具有映射关系的数据:key-value
- key和value,可以是任意类型的数据,会封装到HashMap$Node对象中
- key不允许重复,且无序,原因与Set相同。当有相同的key时,等价于替换,而Set中是以链表的方式存储
- value可以重复
- key可以为空,但只能有一个null(同3),value可以多个null
- 常用String类作为key,但因为是Object key,所以只要是Object子类,都可以作为key
- key和value中存在单向一对一的关系,通过指定key,总能找到相应的value
- key-value放在Node结点中 0531 为了方便程序员遍历,key-value底层创建了一个EntrySet集合,存放Entry<k,v>,即 transient<Entry<k,v>>EntrySet,而其中的<k,v>并没有存放数据,<k,v>是指向HashMap$Node node = New Node(hash,key,value,null)中的k,v
- EntrySet 定义的类型是 Map.Entry,实际上存放的是HashMap$Node,之所以能存放这个是因为 static class Node<k,v> implements Map.Entry
- 如何方便遍历 :Map.Entry中有两个重要的方法 : getKey() getValue()
常用方法
put remove get size isEmpty clear containsKey
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings({"all"})
public class Map_Method {
public static void main(String[] args) {
Map map = new HashMap();
// put添加
map.put("q",1);
map.put("q",new Employee("iso",18));
map.put("w",2);
map.put(null,1);
map.put(null,null);
map.put("e",null);
System.out.println(map);
// remove移除
map.remove("w");
map.remove(null,null);
System.out.println(map);
//get 根据键获取值
Object obj = map.get("q");
System.out.println(obj);
//size 获取键的个数
System.out.println(map.size());
//isEmpty 判断是否为空
System.out.println(map.isEmpty());
//clear 清除
map.clear();
System.out.println(map);
//containsKey 查找键是否存在
System.out.println(map.containsKey("q"));
}
}
遍历方式
第一组:先取出所有的key,再用key获取对应的value
@SuppressWarnings({"all"})
public record MapFor() {
public static void main(String[] args) {
Map map = new HashMap();
map.put("q",1);
map.put("q",new Employee("iso",18));
map.put("w",2);
map.put(null,1);
map.put(null,null);
map.put("e",null);
//第一组:先取出所有的key,再用key获取对应的value
Set Keyset = map.keySet();
//(1)增强for
System.out.println("==========先取出所有的key,再用key获取对应的value==========");
System.out.println("==========增强for==========");
for (Object key:Keyset){
System.out.println("key="+key+map.get(key));
}
//(2)迭代器
System.out.println("==========Iterator==========");
Iterator iterator = Keyset.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println("key="+next+map.get(next));
}
第二组:将所有的value取出
//第二组:将所有value取出
Collection values = map.values();
//可以使用所有Collections使用的遍历方法
//增强for
//(1)增强for
System.out.println("==========取出所有的value==========");
System.out.println("==========增强for==========");
for (Object value : values) {
System.out.println(value);
}
//(2)迭代器
System.out.println("==========Iterator==========");
Iterator iterator1 = values.iterator();
while (iterator1.hasNext()) {
Object next = iterator1.next();
System.out.println(next);
}
第三组:通过EntrySet来获取key-value
//第三组:通过EntrySet来获取k-v
Set entryset = map.entrySet();
//(1)增强for
System.out.println("==========使用EntrySet==========");
System.out.println("==========增强for==========");
for (Object entry : entryset) {
//EntrySet中存放的是entry
//Entry转成 Map.Entry
Map.Entry m =(Map.Entry) entry;
System.out.println(m.getKey() +""+ m.getValue());
}
//(2)迭代器
System.out.println("==========迭代器==========");
Iterator iterator2 = entryset.iterator();
while (iterator2.hasNext()) {
Object next = iterator2.next();
//System.out.println(next.getClass());
//此时类型为Node,implements Map.Entry ,所以可以转为Map.Entry,为了使用getKey()
Map.Entry m = (Map.Entry) next;
System.out.println(m.getKey() +""+ m.getValue());
}
}
}
练习
使用HashMap新建三个员工对象,要求:
- 键:员工id
- 值:员工对象
遍历工资大于18000的对象
package chapter.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@SuppressWarnings({"all"})
public class HashMap_Exercise {
public static void main(String[] args) {
Map map = new HashMap();
map.put(1,new Employee1("王",1,20000));
map.put(2,new Employee1("温",2,8000));
map.put(3,new Employee1("宜",3,18000));
// 增强for,获取所有value值
for (Object value : map.values()) {
Employee1 employee1 =(Employee1) value;
if(employee1.getSal()>=18000){
System.out.println(employee1);
}
}
// EntrySet 迭代器
Set entryset = map.entrySet();
Iterator iterator = entryset.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
Employee1 employee1 = (Employee1) entry.getValue();
if(employee1.getSal()>=18000){
System.out.println(employee1);
}
}
}
}
class Employee1{
private String name;
private int id;
private double sal;
public Employee1(String name, int id, double sal) {
this.name = name;
this.id = id;
this.sal = sal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
@Override
public String toString() {
return "Employee1{" +
"name='" + name + '\'' +
", id=" + id +
", sal=" + sal +
'}';
}
}
PS:将其向下转型为员工,再判断工资后遍历