Map集合。

Map集合:

java.util,Map<k,v> 

特点:1、键值对 2、key-value一一对应 3、key不允许重复。

Map常用实现类:

java.util.HashMap<k,v> 集合  implement Man<k,v> 接口。

HashMap的特点:1、HashMap集合,底层是哈希表,查询速度快。

        2、jdk8之前, 数组+单向链表。 之后:数组+单项链表/红黑树

        3、无序集合。

java.util.LinkedHashMap<k,v> 集合  extends HashMap<k,v>

LinkedHashMap 特点:1、底层-- 哈希表+链表,  所以是有序集合。

Map常用方法:

 public static void main(String[] args) {
        //public V put(K key,V value)
        //k不重复,返回值V是null
        //k重复,覆盖value,返回被替换的值。
        Map<String,String> hashMap=new HashMap<>();
        hashMap.put("张玉昊","胡云钰");
        hashMap.put("天","云");
        System.out.println(hashMap);
        // public V remove (Object key):删除指定键对应的元素。
        //返回值 V:
        // key 存在, 返回被删除的值。 否则返回null。
        hashMap.remove("天");
        System.out.println(hashMap);
        // public V get(Object key) 根据键获取值
        //key存在,返回对应的值,否则null
        System.out.println(hashMap.get("张玉昊"));
        //bool containskey(Object key) 是否包含指定键
        System.out.println(hashMap.containsKey("张玉昊"));
    }

遍历Map集合:

第一种方式:

  public static void main(String[] args) {
        Map<String,String> hashMap=new HashMap<>();
        hashMap.put("张玉昊","胡云钰");
        hashMap.put("天","云");
        //Map集合的第一种方式:通过键找值
        //Map集合中的方法:Set<K> keySet()
        //实现步骤:
        //1、keySet()把Map集合的所有key取出来,存到一个Set集合中
        //2、遍历set集合,找到key,通过key找value.

        //Set<String> set= hashMap.keySet();
        for(String key : hashMap.keySet()){
            System.out.println(hashMap.get(key));
        }
    }

第二种方式:

 public static void main(String[] args) {
        Map<String,String> hashMap=new HashMap<>();
        hashMap.put("张玉昊","胡云钰");
        hashMap.put("天","云");
        //Map集合的第二种方式:通过Entry对象遍历(键值对对象)
        //Map集合中的方法:
        //Set<Map.Entry<K,V> entrySet() 返回映射中包含的
        //映射关系的Set视图。
        //实现步骤;
        //1、entrySet()方法,把Map中的多个Entry对象存入Set集合。
        //2、遍历Set集合,获取每个Entry对象
        //3、使用getKey() and  getValue() 获取键值。
        for (Map.Entry<String,String> entry:hashMap.entrySet()){
            System.out.println(entry.getKey()+"--"+entry.getValue());
        }
    }

Hash Map存储自定义类型:

  //HashMap存储自定义类型的键值
    //Map集合保证key唯一:
    //所以,作为key的元素,必须重写hashCode()和equals()
    public static void main(String[] args) {
        show2();
    }

    public static void show1(){
        //Key: String类型
            //String类重写了hashCode()and equals(),所以key唯一
        //Value:Person类型
            //value可以重复。
        Map<String,Person> hashMap=new HashMap<>();
        hashMap.put("四川",new Person("Sam",16));
        hashMap.put("上海",new Person("Penny",19));
        hashMap.put("四川",new Person("instead",17));
        for (String set:hashMap.keySet()){
            System.out.println(set+"--"+
                    hashMap.get(set));
        }
        //因为四川重复了,所以第一个被覆盖。
    }
    public static void show2(){
        //Key: Person类型
            //必须重写hashCode 和 equals
        //value:String
            //可重复。
        HashMap<Person,String> hashMap=new HashMap<>();
        hashMap.put(new Person("天",21),"川农");
        hashMap.put(new Person("云",22),"宜院");
        hashMap.put(new Person("天",21),"川农1");
        for (Map.Entry<Person,String> entry:hashMap.entrySet()){
            System.out.println(entry.getKey()+"--"+entry.getValue());
        }
        //重写前:
        //Person{name='天', age=21}--川农
       //Person{name='天', age=21}--川农1
        //Person{name='云', age=22}--宜院
        //重写后:
        //Person{name='天', age=21}--川农1
       // Person{name='云', age=22}--宜院
    }

LinkedHashMap:

java.util.LinkedHashMap<K,V> extend HashMap<K,V>

底层:哈希表+链表  有序。

public static void main(String[] args) {
       LinkedHashMap<String,String> linkedHashMap=new LinkedHashMap<>();
       linkedHashMap.put("a","a");
       linkedHashMap.put("c","c");
       linkedHashMap.put("b","b");
        System.out.println(linkedHashMap);
        //{a=a, c=c, b=b}
    }

HashTable集合:

java.util.Hashtable<K,V> implements Map<K,V>

  Hashtable: 底层哈希表,是线程安全的集合,单线程,速度慢。

  HashMap:底层哈希表,是线程不安全的集合,多线程,速度快。

  -----------

  HashMap:可null键,null值

  Hashtable:不可以。

  ------

  Hashtable 和 Vector ,jdk1.2后,被 HashMap和ArrayList取代

  但,Hashtable的子类,Properties,还在用。

  Properties集合是唯一和IO结合的集合。

Map集合练习:

判断字符串每个字符出现个数:

public static void main(String[] args) {
       LinkedHashMap<String,String> linkedHashMap=new LinkedHashMap<>();
       linkedHashMap.put("a","a");
       linkedHashMap.put("c","c");
       linkedHashMap.put("b","b");
        System.out.println(linkedHashMap);
        //{a=a, c=c, b=b}
    }

JDK9 对集合的优化  of 方法:

public static void main(String[] args) {
        //List,Set,Map 接口,添加了一个静态方法,可一次性添加多个元素。
        //static <E> List<E> of (E...elements)
        //使用前提:
            //集合元素固定,不会再改变时。
        //注意:
            //1、of方法,只用于,List,Set,Map接口,不适用于实现类。
            //2、of 的返回值是一个不能改变的集合,不能再用put  add
            //3、Set 和Map接口在调用of时,不能有重复的元素。
        List<String> list=List.of("a","b","c");
        System.out.println(list);
    }

猜你喜欢

转载自www.cnblogs.com/zhangyuhao/p/10761516.html
今日推荐