JAVA--高级基础开发

day05_【Map集合】

第一章

1.1 概述

  1. Map集合位于java.util.Map包中。
  2. Map集合的特色:
  • 简单的来讲,Map集合存放的数据结构是键值对,也就是说,Key=value.
  • 经过Api文档,Map接口跟Collection接口没有任何的关系。
  • Collection接口的子接口,每次只能存储一个元素,叫作单列集合。
  • Map接口存储的元素是键值对,key=value,也叫作双列集合。
  • Map集合中的键是不能重复的,值能够重复,而且每一个键只能对应一个值。
  • Map集合可使用null 做为键和值。
  • Map集合中的key键,是根据Set 集合来存放的。
  • Map集合在输出数据时,不保证顺序。

1.2 Map集合经常使用的实现类

  1. Ma集合经常使用的实现类有两种:HashMap、LinkedHashMap
  2. HashMap:存储的数据采用的Hash表结构,不保证元素的存储顺序,因为要保证键的惟一性,因此必须重写HashCode方法和equals方法。【无序不重复】
  3. LinkedHashMap是HashMap的子类,存储的顺序是采用哈希表+链表结构,经过链表结构能够保证元素的顺序是一致的。【有序不重复】

1.3 Map集合经常使用的方法

  1. public V put(K key ,V value):把指定的键与值添加到 Map 集合中
  2. public V remove(Object key);返回被移除的元素,在集合中根据指定的键,删除指的的值。
  3. public V get(Object key):根据指定的键,在 Map 集合中获取对应的值
  4. public Set<K> keySet():获取 Map 集合中全部的键,存储到 Set 集合中
  5. public boolean containKey(Object key):判断集合中是否有此键。
  6. public Set<Map.Entry<K,V>> entrySet():获取到 Map 集合中全部的键值对对象的集合

1.3.1Map集合经常使用方法的示例java

 

//Map集合的应用:存放的都是键值对数据结构

public class Test01 {spa

    public static void main(String[] args) {code

        Map<String,String>map=new HashMap<>();对象

        map.put("a","liwenjie");接口

        map.put("b","sangfengjiao");ci

        map.put("c","wangzhiya");rem

        map.put("d","mashitian");文档

        //V get(Object key)返回到指定键所映射的值.字符串

        System.out.println(map.get("a"));

        //int hashCode();返回此地图的哈希码值。

        System.out.println(map.hashCode());

        //boolean isEmpty()若是此集合为空,则返回 true 。

        System.out.println(map.isEmpty());

        //Set<K> keySet();返回此集合中全部的键

        System.out.println(map.keySet());

 

        //void putAll(Map<? extends K,? extends V> m)

        //将指定的map集合复制到另一个map集合中(可选操做)。

        Map<String,String>map2=new HashMap<>();

        map2.putAll(map);

        System.out.println("==="+map2);

        //V remove(Object key);返回被移除的元素

        // 在集合中根据指定的键,删除指的的值。

        System.out.println(map.remove("a"));

        System.out.println(map);

        //map集合 的长度

        System.out.println(map2.size());

        //boolean containsKey(Object key)

        //若是此映射包含指定键的映射,则返回 true 。

        System.out.println(map2.containsKey("b"));

 

        //利用for循环 迭代输出键和值

        Set<Map.Entry<String,String>>mpp=map2.entrySet();

        for(Map.Entry<String,String>ss:mpp){

            System.out.println("键:"+ss.getKey()+" "+"值:"+ss.getValue());

        }

    }

}

1.4 Entry 键值对对象

  1. 即经过集合中每一个键值对(Entry)对象,获取键值对对象中的键与值。
  2. Map集合中存放是两种对象:一种是键,一种是值,它们在 Map 中是一一
  3. 对应的,这一对对象又称做 Map 中的一个 Entry(项).Entry 将键值对的关系封装成了对象,即键值对对象。
  4. 获取了 Entry 对象,表示获取了一对键值对,那么一样 Entry 中,分别提供了获取键和值的方法。
  5. Public  K  getKey():获取 Entry 对象中的键key
  6. Public  V  getValue():获取 Entry 对象中的值 value

 //利用for循环 迭代输出键和值

Entry 键值对对象 示例:

        Set<Map.Entry<String,String>>mpp=map2.entrySet();

        for(Map.Entry<String,String>ss:mpp){

            System.out.println("键:"+ss.getKey()+" "+"值:"+ss.getValue());

        }

1.5 Map 集合中存储自定义的对象

  1. 存放自定义对象的时候,必须重写Hashcode方法和equals方法,才能去掉重复的值

示例:

//自定义对象的使用

        Map<Student,String>map=new HashMap<>();

        map.put(new Student("liwenjie",23),"天水市");

        map.put(new Student("sangfengjiao",22),"泰安市");

        map.put(new Student("wanzhiya",22),"山西市");

        map.put(new Student("zhangsan",23),"北京市");

        map.put(new Student("zhangsan",23),"北京市");

        map.put(new Student("zhangsan",23),"北京市");

       //存放自定义对象的时候,必须重写Hashcode方法和equals方法,才能去掉重重的

        Set<Map.Entry<Student,String>>srf=map.entrySet();

        for(Map.Entry<Student,String>ss:srf){

            System.out.println("键:"+ss.getKey()+"值:"+ss.getValue());

        }

1.6 HashMap集合

  1. HashMap 集合的特色:
  • 存储的数据无序,不重复。键不能重复,值能够重复。

HashMap<String,Integer>map=new HashMap<>();

        map.put("a",100);

        map.put("b",200);

        map.put("c",300);

        map.put("d",400);

        map.put("d",400);

        Set<Map.Entry<String,Integer>>mpp=map.entrySet();

        for(Map.Entry<String,Integer> ss:mpp){

            System.out.println(ss.getKey()+"=="+ss.getValue());

        }

1.7  LinkedHashMap集合

  1. LinkedHashMap 是HashMap的子类,它是链表和哈希表组合的一个集合,
  2. LinkedHashMap集合的特色:元素是有顺序的,而且不重复。

  LinkedHashMap<String,Integer>map=new LinkedHashMap<>();

        map.put("a",500);

        map.put("c",200);

        map.put("c",200);

        map.put("d",300);

        map.put("d",300);

        map.put("e",400);

        map.put("f",400);

        Set<Map.Entry<String,Integer>>mmm=map.entrySet();

        for(Map.Entry<String,Integer>  ss:mmm){

            System.out.println(ss.getKey()+"==="+ss.getValue());

        }

1.8 练习题

  1. 计算一个字符串中每一个字符出现的次数,用 Map 集合来存储结果,键(key)表明字符,值(value)表明次数。

public class Exercise01 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("输入字符串:");

        String s = sc.nextLine();

        Method(s);

    }

    public static void  Method(String s){

        HashMap<Character,Integer> map =new HashMap<>();

         for(int i = 0; i <s.length() ; i++) {

             //若是不包含的话就添加到集合

            if(!map.containsKey(s.charAt(i))){

                map.put(s.charAt(i),1);

            }else{ //包含的话 ,就计入次数

                int count = map.get(s.charAt(i));

                count++;

                map.put(s.charAt(i),count);

            }

         }

        System.out.println(map);

    }

}

相关文章
相关标签/搜索