39Map集合

110 阅读5分钟
   Map<K,V>双列集合键值对接口,Collection是单列集合的根接口
	1.内部定义的方法,子接口/实现类都有
	2.键唯一,值可以重复  一个键对应一个值,叫一一对应关系(映射关系/键值对关系)
      键要唯一: 键所属的类覆盖重写hashCode和equals方法
      允许存储null键和null值
      键无索引: 不能通过索引的方式获取键 
    3.依靠键维护映射关系(可以通过键获取到值,但是不能通过值获取键)
  Map接口常用实现类:
 HashMap<K,V>集合特点   键无序: 不保证存入和取出的顺序是一致的 
 LinkedHashMap<K,V>集合特点  链表特性保证键有序: 保证存入和取出的顺序是一致的   
常用方法   
     public V put(K key, V value) : 把指定的键与指定的值添加到Map集合中。
    	如果键key,是第一次存储,返回null
    	如果键key已经存在,返回是被替换掉的值
    	键相同,值被替换

	public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
    	键不存在: 返回null
	public V remove(Object key) : 
		把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
		
	public boolean containsKey(Object key) :判断该集合中是否有此键。
	public boolean containsValue(Object value) :判断该集合中是否有此值
    public Set<K> keySet() : 获取Map集合中所有的键,存储到Set集合中。
	public Collection<V> values() :返回Map集合中的所有值到Collection集合。        
    public class Demo02Map {
    public static void main(String[] args) {
        //多态创建Map集合对象
        Map<String,String> map = new HashMap<>();

        //public V put(K key, V value) : 把指定的键与指定的值添加到Map集合中。
        String value = map.put("文章","马伊琍");
        System.out.println(value);

        value = map.put("文章","姚笛");
        System.out.println(value);

        map.put("黄晓明","杨颖");

        map.put("谢霆锋","王菲"); 
        System.out.println(map); 
        //获取值"姚笛"
        value = map.get("文章");
        System.out.println(value); 
        //没有键
        value = map.get("宝强");
        System.out.println(value);

        //判断是否包含键"黄晓明"
        boolean result = map.containsKey("黄晓明");
        System.out.println("是否包含键黄晓明:"+result);//true

        //判断是否包含键"宝强"
        result = map.containsKey("宝强");
        System.out.println("是否包含键宝强:"+result);//false

        //判断是否包含值"王菲"
        System.out.println("是否包含值王菲: "+map.containsValue("王菲"));//true

        //判断是否包含值"马伊琍"
        System.out.println("是否包含值马伊琍: "+map.containsValue("马伊琍"));//false

        //删除键"文章"
        value = map.remove("文章");
        System.out.println(value);//姚笛
        System.out.println(map); 
        System.out.println("键值对数量: "+map.size());//2 
        System.out.println("是否为空: "+map.isEmpty());//false 
        //清空
        map.clear(); 
        System.out.println("键值对数量: "+map.size());//0 
        System.out.println("是否为空: "+map.isEmpty());//true 
    }
}

Map集合的获取所有的键和值的功能

 public class Demo03Map {
    public static void main(String[] args) {
        //多态创建Map集合对象
        Map<String,Integer> map = new HashMap<>();

        //put: 添加键值对
        map.put("b",98);
        map.put("d",100);
        map.put("a",97);
        map.put("c",99);
        System.out.println(map);

        //keySet方法:获取所有的键对应的Set集合
        Set<String> set = map.keySet();

        //迭代器遍历
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        System.out.println("------------");

        //values方法: 获取所有的值对应的Collection集合
        Collection<Integer> coll = map.values(); 
        //增强for遍历
        for (Integer num : coll) {
            System.out.println(num);
        }
    }
}

###Map集合的遍历_通过键找值

/* 遍历(迭代器/增强for)所有的键对应的Set集合
            4.1获取到当前的键
            4.2Map集合对象调用get方法,传递当前的键,获取对应的值
            4.3打印键和值
 */
public class Demo02_01MapEach {
    public static void main(String[] args) {        
        //1.创建Map集合对象,指定键和值的类型
        Map<String,String> map = new HashMap<>();
        //2.Map集合对象调用put方法,添加键值对
        map.put("邓超","孙俪");
        map.put("李晨","范冰冰");
        map.put("刘德华","柳岩");		
        //keySet方法+增强for
        //3.Map集合对象调用keySet方法,获取所有的键对应的Set集合
        Set<String> set = map.keySet();
        //4.遍历(增强for)所有的键对应的Set集合
        for (String key : set) {
            //4.1获取到当前的键: key
            //4.2Map集合对象调用get方法,传递当前的键,获取对应的值
            String value = map.get(key);
            //4.3打印键和值
            System.out.println(key+"----"+value);

        } 
    }
}

public class Demo02_02MapEach {
    public static void main(String[] args) {
        //1.创建Map集合对象,指定键和值的类型
        Map<String,String> map = new HashMap<>();
        //2.Map集合对象调用put方法,添加键值对
        map.put("邓超","孙俪");
        map.put("李晨","范冰冰");
        map.put("刘德华","柳岩");        
		//keySet方法+迭代器
        //3.Map集合对象调用keySet方法,获取所有的键对应的Set集合
        Set<String> set = map.keySet();
        //4.遍历(迭代器)所有的键对应的Set集合
        Iterator<String> it = set.iterator();
        while(it.hasNext()) {
            //4.1获取到当前的键
            String key = it.next();
            //4.2Map集合对象调用get方法,传递当前的键,获取对应的值
            String value = map.get(key);
            //4.3打印键和值
            System.out.println(key+"::::"+value);
        }
    }
}

键值对方式

    Map抽象方法:
        getKey(): 获取键 getValue(): 获取值  setValue(V newValue): 修改值,返回的被修改的值  
        entrySet(): 用来获取所有的结婚证(键值对/映射关系)
        Map集合对象调用entrySet方法,获取所有的键值对对应的Set集合 
        4.遍历(迭代器/增强for)所有的键值对对应的Set集合
            4.1获取到当前的键值对对象
            4.2当前的键值对对象调用getKey方法,获取键
            4.3当前的键值对对象调用getValue方法,获取值
            4.4打印键和值
      public class Demo04_01MapEach {
    public static void main(String[] args) {
        //1.创建Map集合对象,指定键和值的类型
        Map<String,String> map = new HashMap<>();
        //2.Map集合对象调用put方法,添加键值对
        map.put("邓超","孙俪");
        map.put("李晨","范冰冰");
        map.put("刘德华","柳岩");
        //entrySet方法 + 增强for
        //3.Map集合对象调用entrySet方法,获取所有的键值对对应的Set集合
        Set<Map.Entry<String,String>> set = map.entrySet();
        //4.遍历(增强for)所有的键值对对应的Set集合
        for (Map.Entry<String, String> entry : set) {
            //4.1获取到当前的键值对对象: entry
            //4.2当前的键值对对象调用getKey方法,获取键
            //4.3当前的键值对对象调用getValue方法,获取值
            //4.4打印键和值
            System.out.println(entry.getKey()+"::::"+entry.getValue());
        }
        System.out.println("--------------");
        //最简单的增强for
        //快捷键: map.entrySet().for
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey()+"::::"+entry.getValue());
        }
    }
}
public class Demo04_01MapEach {
    public static void main(String[] args) {
        //1.创建Map集合对象,指定键和值的类型
        Map<String,String> map = new HashMap<>();
        //2.Map集合对象调用put方法,添加键值对
        map.put("邓超","孙俪");
        map.put("李晨","范冰冰");
        map.put("刘德华","柳岩");
        //3.Map集合对象调用entrySet方法,获取所有的键值对对应的Set集合
        Set<Map.Entry<String, String>> set = map.entrySet();
        //4.遍历(迭代器)所有的键值对对应的Set集合
        Iterator<Map.Entry<String, String>> it = set.iterator();
        while(it.hasNext()) {
            //4.1获取到当前的键值对对象
            Map.Entry<String, String> entry = it.next();
            //4.2当前的键值对对象调用getKey方法,获取键
            String key = entry.getKey();
            //4.3当前的键值对对象调用getValue方法,获取值
            String value = entry.getValue();
            //4.4打印键和值
            System.out.println(key+"::::"+value);
        }
    }
}  

HashMap存储自定义对象并遍历

public class Student {
    private String name;
    private int age;
    //覆盖重写hashCode和equals方法
    //生成空参/满参,set/get/toString方法
}