Map集合遍历键值对方式和HashMap存储自定义类型键值

Map集合遍历键值对方式

Set<Map.Entry<K, V>> entrySet() 返回此映射中包含的映射关系的Set视图

1、使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存储到一个Set集合中
2、遍历Set集合,获取每一个Entry对象
3、使用Entry对象中的方法getKey()和getValue()获取键与值

 

package Demo_Map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
Map集合遍历的第二种方式:使用Entry对象遍历Map集合中的方法:
set<Map.Entry<K,v>> entrySet()返回此映射中包含的映射关系的 Set视图。
实现步骤:
1.使用Map集合中的方法entrySet( ),把Map集合中多个Entry对象取出来,存储到一个Set集合中

2.遍历set集合,获取每一个Entry对象

3.使用Entry对象中的方法getKey ()和getvalue()获取键与值
 */
public class Demo02_EntrySet {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("蒙恬",789);
        map.put("夏侯惇",563);
        map.put("猪八戒",6544);

        //1.使用wap集合中的方法entrySet( ),把sap集合中多个Entry对象取出来,存储到一个set集合中
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
       //2.遍历set集合,获取每一个Entry对象//使用迭代器遍历set集合
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Integer> next = iterator.next();
            String key = next.getKey();
            Integer value = next.getValue();
            System.out.println(key+"="+value);
        }
      

    }
}

 

 

 

Map集合遍历的第二种方式:使用Entry对象遍历Map集合中的方法:
set<Map.Entry<K,v>> entrySet()返回此映射中包含的映射关系的 Set视图。
实现步骤:

1.使用Map集合中的方法entrySet( ),把Map集合中多个Entry对象取出来,存储到一个Set集合中

2.遍历set集合,获取每一个Entry对象

3.使用Entry对象中的方法getKey ()和getvalue()获取键与值

 

HashMap存储自定义类型键值

要在自定义类中重写hashCode()方法、equals()方法。

 

HashMap存储自定义类型键值Map集合保证key是唯一的:
作为key的元素,必须重写hashcode方法和equals方法,以保证key唯一

 

因为已有类型如字符串都已重写了hashCode方法和equals方法,当使用这些已有类型作为key的类型的时候,不用再重写了。

1.使用自定义类型作为值:

private static void show01() {
    // 首先创建HashMap集合
    HashMap<String,Person> map = new HashMap<>();
    // 往集合中添加元素
    map.put("北京",new Person("张三",18));
    map.put("上海",new Person("李四",19));
    map.put("广州",new Person("王五",20));
    map.put("北京",new Person("赵六",18)); // key重复,新的value替换旧的
 
    // 使用keySet方法取出所有key放进set里,foreach遍历
    Set<String> set = map.keySet();
    for (String str:set) {
        System.out.println(str + "-->" + map.get(str).toString());
    }
}

.使用自定义类型作为键:

需要保证唯一性,重写hashCode方法和equals方法

private static void show02() {
    HashMap<Person,String> map = new HashMap<>();
    // 往集合中添加元素
    map.put(new Person("张三",18),"北京");
    map.put(new Person("李四",19),"上海");
    map.put(new Person("王五",20),"广州");
    // 姓名,年龄相同,视为key重复,应将value替换
    map.put(new Person("张三",18),"武汉");
 
    // 使用EntrySet方法获得由map所有键值对组成的set集合
    // 此处是内部类写法
    Set<Map.Entry<Person,String>> set = map.entrySet();
    // 迭代器遍历
    Iterator<Map.Entry<Person,String>> iterator = set.iterator();
    while(iterator.hasNext()){
        // 先获得entry对象,避免next超出范围
        Map.Entry<Person,String> entry = iterator.next();
        // 由entry对象的getKey方法和getValue方法获得键值
        System.out.println(entry.getKey().toString() + "-->" + entry.getValue());
    }
}
 

当我们重写了这个自定义类的hashCode方法和equals方法后:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Person person = (Person) o;
    return age == person.age &&
            Objects.equals(name, person.name);
}
 
@Override
public int hashCode() {
    return Objects.hash(name, age);
}

 

 

posted @ 2022-07-06 21:10  zj勇敢飞,xx永相随  阅读(26)  评论(0编辑  收藏  举报