HashMap中获取键和值的两种方法

HashMap中获取键和值的两种方法

 

package cn.itcast.Day10;

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

import static cn.itcast.Day10.ChangeParase.show2;

public class HashMap_one {
    public static void main(String[] args) {
show1();

    }

    private static void show2() {
        //如果键值对相同则会发生替换
        HashMap map=new HashMap<>();
        map.put(new Person("黄晓明",18),"英国");
        map.put(new Person("吴亦凡",35),"法国");
        map.put(new Person("意大利炮王",78),"德国");
        map.put(new Person("黄晓明",18),"意大利");



        Set> ent = map.entrySet();
        for (Map.Entry entry : ent) {
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"--->"+value);
        }

    }

    private static void show1() {

        //如果键值对相同则会发生替换
        HashMap map=new HashMap<>();
        map.put("英国",new Person("黄晓明",18));
        map.put("法国",new Person("吴亦凡",35));
        map.put("德国",new Person("意大利炮王",78));
        map.put("英国",new Person("文章",38));

        Set set = map.keySet();
        for (String key:set){
            Person person = map.get(key);
            System.out.println(person);
        }


    }
}

 

你可能感兴趣的:(JAVA知识)