zoukankan      html  css  js  c++  java
  • Collections工具类、Map集合、HashMap、Hashtable(十八)

    1.Map集合概述和特点

    * A:Map接口概述
      * 去重复,
      * 查看API可以知道,
      * 将键映射到值的对象,
      * 一个映射不能包含重复的键,
      * 每个键最多只能映射到一个值。
    * B:Map接口和Collection接口的不同
      * Map是双列的,Collection是单列的。
      * Map的键唯一,Collection的子体系Set是唯一的,即不重复。
      * Map集合的数据结构值针对键有效,跟值无关;Collection集合的数据结构是针对元素有效。

    2.Map集合的功能概述

    * a:添加功能
      * V put(K key,V value):添加元素。
        * 如果键是第一次存储,就直接存储元素,返回null
        * 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
    * b:删除功能
       * void clear():移除所有的键值对元素
      * V remove(Object key):根据键删除键值对元素,并把值返回
    * c:判断功能
      * boolean containsKey(Object key):判断集合是否包含指定的键
      * boolean containsValue(Object value):判断集合是否包含指定的值
      * boolean isEmpty():判断集合是否为空
    * d:获取功能
      * Set<Map.Entry<K,V>> entrySet():???
      * Entry 是Map 接口中的内的接口,内接口,
      * V ---- get(Object key):根据键获取值
      * Set<K> -- keySet():获取集合中所有键的集合
      * Collection<V> values():获取集合中所有值的集合
    * e:长度功能
      * int size():返回集合中的键值对的个数。

    3.Map集合的遍历之键找值

    * 键找值思路:
      * 获取所有键的集合keySet()
      * 遍历键的集合,获取到每一个键get()
      * 根据键找值

    Map集合的遍历之键找值
            HashMap<String, Integer> hm = new HashMap<>();
            hm.put("张三", 23);
            hm.put("张三", 23);
            hm.put("李四", 24);
            hm.put("王五", 25);
            hm.put("王五", 25);
            hm.put("王五", 25);
            hm.put("赵六", 26);
            1.keySet(),get(),增强for循环
              for (String key : hm.keySet()) {
                Integer value = hm.get(key);
                System.out.println(key+","+value);
            }

    4.Map集合的遍历之键值对对象找键和值

    * :键值对对象找键和值思路:
      * 获取所有键值对对象的集合
      * 遍历键值对对象的集合,获取到每一个键值对对象
      * 根据键值对对象找键和值

    Map集合的遍历之键值对对象找键和值
            2.entrySet(),Entry<>,getValue(),getKey(),增强for循环
              for (Entry<String,Integer> entry : hm.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(key+","+value);
            }
    HashMap集合键是Stirng值是String的案例
            HashMap<String, String> hm = new HashMap<>();
            hm.put("张三", "北京");
            hm.put("李四", "大连");
            hm.put("王五", "上海");
            hm.put("赵六", "南京");
            System.out.println(hm);
            //{赵六=南京, 张三=北京, 李四=大连, 王五=上海}
            }    
    TreeMap集合键是String值是String的案例
            TreeMap<String, String> ts = new TreeMap<>();
            ts.put("1", "北京");
            ts.put("5", "南京");
            ts.put("2", "天京");
            ts.put("4", "东京");
            ts.put("3", "西京");
            ts.put("6", "中都");
            System.out.println(ts);
            //{1=北京, 2=天京, 3=西京, 4=东京, 5=南京, 6=中都}
    TreeMap集合键是Student值是String的案例
            TreeMap<Student, String> ts = new TreeMap<>(new Comparator<Student>() {
                @Override
                public int compare(Student s1, Student s2) {
                    int num = s1.getName().compareTo(s2.getName());
                    return num ==0 ? s1.getAge() - s2.getAge() : num;
                }
            });
            ts.put(new Student("张三",23), "北京");
            ts.put(new Student("李四",24), "东京");
            ts.put(new Student("李四",25), "东京");
            ts.put(new Student("李四",24), "东京");
            ts.put(new Student("王五",25), "南京");
            ts.put(new Student("王五",25), "南京");
            System.out.println(ts);
    需求:统计字符串中每个字符出现的次数
            String str = "aabbbccc";
            char[] arr = str.toCharArray();//把字符串转换成字符数组
            HashMap<Character , Integer> hm = new HashMap<>();//创建HashMap对象
            for(char c : arr){//增强for循环
                if(!hm.contaionsKey(c)){//若hm中没有值就添加,key 为单个字符,值 最初为1,以后每有相同的就加1。
                    hm.put(c,1);
                }else{
                    hm.put(c,hm.get(c)+1);
                }
            }
    集合嵌套之HashMap嵌套HashMap
            HashMap<String, HashMap<Student, String>> hm = new HashMap<>();
            HashMap<Student, String> hm1 = new HashMap<>();
            hm1.put(new Student("张三",23) ,"北京" );
            hm1.put(new Student("李四",24) ,"上海" );
            hm1.put(new Student("张三",23) ,"南京" );
            hm1.put(new Student("王五",25) ,"东京" );
            hm1.put(new Student("王五",25) ,"西京" );
            hm1.put(new Student("赵六",26) ,"中都" );
            HashMap<Student, String> hm2 = new HashMap<>();
            hm2.put(new Student("张3",23) ,"北京" );
            hm2.put(new Student("李4",24) ,"上海" );
            hm2.put(new Student("张3",23) ,"南京" );
            hm2.put(new Student("王5",25) ,"东京" );
            hm2.put(new Student("王5",25) ,"西京" );
            hm2.put(new Student("赵6",26) ,"中都" );
            hm.put("第一组人", hm1);
            hm.put("第二组人", hm2);
            for (String key : hm.keySet()) {
                HashMap<Student, String> value = hm.get(key);
                for (Student val : value.keySet()) {
                    System.out.println(key +","+ val +","+ value.get(val));
                }
            }
        }
        /*
           第一组人,Student [name=张三, age=23],南京
                  第一组人,Student [name=李四, age=24],上海
                  第一组人,Student [name=王五, age=25],西京
                  第一组人,Student [name=赵六, age=26],中都
                  第二组人,Student [name=张3, age=23],南京
                  第二组人,Student [name=王5, age=25],西京
                  第二组人,Student [name=赵6, age=26],中都
                  第二组人,Student [name=李4, age=24],上海
         */

    5.HashMap和Hashtable的区别

    * HashMap和Hashtable的区别
        * 底层都是hash表实现的,
            * HashMap: JDK2.0、不同步(线程不安全的),效率高、能存储null值和null键
            * Hashtable:JDK1.0、同步的(线程安全的),效率低、不能存储null值和null键 
            * Vector--JDK1.0 

    6.Collections工具类的概述和常见方法讲解

    * A:Collections类概述
      * collection ----集合层次的根接口
      * collections----操作集合的工具类
        * 针对集合操作 的工具类  
    * B:Collections成员方法
      public static <T> void sort(List<T> list)//排序
      public static <T> int binarySearch(List<?> list,T key)//二分查找
      public static <T> T max(Collection<?> coll)//最大值
      public static void reverse(List<?> list)//反转
      public static void shuffle(List<?> list)无序

    7.泛型固定下边界

      * ? super E ---- E 是子类
      * ? extends E ---- E是父类

  • 相关阅读:
    cinder支持nfs快照
    浏览器输入URL到返回页面的全过程
    按需制作最小的本地yum源
    创建可执行bin安装文件
    RPCVersionCapError: Requested message version, 4.17 is incompatible. It needs to be equal in major version and less than or equal in minor version as the specified version cap 4.11.
    惠普IPMI登陆不上
    Linux进程状态——top,ps中看到进程状态D,S,Z的含义
    openstack-neutron基本的网络类型以及分析
    openstack octavia的实现与分析(二)原理,架构与基本流程
    flask上下文流程图
  • 原文地址:https://www.cnblogs.com/wanghui1316/p/5455998.html
Copyright © 2011-2022 走看看