• Java 集合Map(二)


    Map

           1,Map集合存储的键值对元素,键是唯一的,而值可以重复。

            2,Map集合的功能:

                              添加:

                                   V put(K key,V value)

                              判断:

                                  V containsKey(K key)  判断集合是否有指定的键。

                                  K containsValue(V value)  判断集合是否有指定的值。

                              删除:

                                     remove(K key)

                              获取:

                                 V get(K key)  根据指定的键,找到对应的值。

                                 Set<K> keySet() 返回键的Set集合。

                                 Collection<V>values() 返回值的Collection集合。

                                 Set<Map.Entry<K,V>> entrySet()  返回的是键值对对象的集合。

                              长度:

                                 size()

             3,Map的数据结构问题

                           Map集合的数据结构对键有效,跟值无关。

                           底层数据结构跟Set一致。

                                   如果是哈希表结构,就需要重写hashCode()和equals()方法。

                                   如果是二叉树结构,就有两种方式:Comparable,Comparator。

            4,Map的遍历方式:

                            假设有一个HashMap集合对象存储的都是String类型,名称叫hm。

                           1,根据键找值:

                                      a ,获取所有集合。

                                      b ,遍历所有集合,获取到每一个键。

                                      c ,根据键找值。

    //示例
    Set<String> set=hm.keySet();
    for(String key : set){
        String value=hm.get(key);
        System.out.println(value+"-"+key)
    }

                         2,根据键值对对象找键和值

                                   a ,获取所有键值对对象。

                                   b ,遍历所有键值对对象集合,获取每一个键值对对象。

                                   c ,根据键值对对象,找到键和值 。

    //示例
    Set<Map.Entry<String,String>> set=hm.keySet();
    for(Map.Entry<String,String> me:set){
        String key=me.getKey();
        String value=me.getValue();
        System.out.println(key+""+value);
    }

                5,Map集合的体系

                           Map

                             |-----HashMap

                                         |-----LinkedHashMap

                             |-----Hashtable

                             |-----TreeMap

                           1,HaspMap和Hashtable的区别?

                                    HaspMap线程不安全(不同步),效率高,允许null键和值。JDK1.2

                                    Hashtable线程安全(同步),效率低, 不允许null键和值。JDK1.0

                           2,LinkedHashMapr的键的特点?

                                     底层数据结构是链表和哈希表

                                     键有序,唯一。

                6,Map和Collection的区别?

                            Map是双列集合,存储的是键值对,键唯一,值可以重复。

                            Collection是单列集合,存储的元素是单一的,键唯一,值可以重复。

                7,注意:

                            如果使用了一个JDK提供了类的名称,那么在使用JDK的类的时候,需要加入包的前缀

    //示例
    java.util.HashMap<String,String> hm=new java.util.HashMap<String,String>();

                 

                8,案例:

                    

                             需求:

                          1,"cbxzbvavdvgd"获取字符串中,每一个字母出现次数:"a(1)b(2)c(1)d(2)g(1)v(3)x(1)z(1)"。

                             

                          思路:

                               1,定义一个字符串。

                               2,将字符串转成字符数组。

                               3,定TtreeMap集合。

                               4,遍历字符数组,获取每一个字符。

                               5,拿每一个第一次出现的字符作为键,对TreeMap集合中查找值。

                                          返回null,说明不存在,存储一次,并且记为1。

                                          不为null,说明已经存在过,把值+1,然后重新存储。

                               6,把TreeMap集合的元素遍历并拼接成满足需求的字符串。

                               7,输出字符串。

    //示例
    String str="cbxzbvavdvgd";
    char[] chs=srt.toCharArray();
    TreeMap<Character,Ingeter> tm=new TreeMap<Character,Integer>();
    for(char ch:chs){
        Ingeter i=tm.get(ch);
        if(i==null){
            tm.put(ch,1);
        }else{
            i++;
            tm.put(ch,i);
        }
    }
    StringBuilder sb=new StringBuiler();
    Set<Character> set=tm.keySet();
    for(Character key:set){
        Integer value=tm.get(key);
        sb.append(key).append("(").append(value).append(")");
    }
    String result=sb.toString();
    System.out.println(result);

                         2,Map集合的嵌套及遍历

                         school

                             |--team1

                                    |--t101 tom

                                    |--t102 june

                             |--team2

                                    |--t201 mary

                                    |--t202 joden

                         思路:

                                  1,创建集合对象

                                  2,创建元素对象

                                  3,元素对象添加到集合对象中

                                  4,遍历

    //示例
    HashMap<String,HashMap<String,String>> school=new HashMap<String,HashMap<String,String>>();
    
    HashMap<String,String> team1=new HashMap<String,String>();
    team1.put("t101","tom");
    team1.put("t102","june");
    
    HashMap<String,String> team2=new HashMap<String,String>();
    team2.put("t201","mary");
    team2.put("t202","joden");
    
    school.put("team1",team1);
    school.put("team2",team2);
    
    Set<String>  set=school.keySet();
    for(String key:set){
        HashMap<String,String> hmkey=school.get(key);
        Set<String> hm=hmkey.keySet();
        for(String hmkey: hm){
            String value=hm.get(hmkey);
            System.out.println(”	“+hmkey+"-"+value);
        }
        
    }

    Collections

                    1,是针对集合操作的工具类。

                    2,常见功能:

                             排序

                                      sort()

                             二分查找

                                      binarySearch()

                             最大值 

                                      max()

                             反转

                                     reverse()

                             随机置换

                                    shuffle()

                    3,Collection和Collections的区别?

                              Collection是单列集合的顶层接口

                              Collections是针对集合操作的工具类。

                   4,如果造一个线程安全的集合?

    //示例
    ArrayList<String> list=synchronizedList(new ArrayList<String>());
                

                       

    ------------------------------------------------------------               

    集合总结:

               应用场景:

                        是否键值对

                            |---是

                                  |---Map

                                          |---是否排序

                                                    |---是

                                                           |---TreeMap                                             

                                                    |---否

                                                           |---HashMap

                                                      

                            |---否

                                  |---Collection

                                             |---是否唯一

                                                      |---是

                                                            |---Set

                                                                    |---是否排序

                                                                              |---是

                                                                                     |---TreeSet

                                                                              |---否 

                                                                                     |---HashSet

                                                      |---否

                                                             |---增删多

                                                                       |---LinkedList

                                                             |---查询多

                                                                       |---ArrayList

    ---------------------------------------------------------------------------

    集合体系总结:

                     集合

                        |---Collection

                                    |---List

                                           |---ArrayList

                                                    |---底层数据结构是数组

                                                    |---线程不同步,不安全,效率高。查询快,增删慢。

                                           |---Vector

                                                    |---底层数据结构是数组

                                                    |---线程同步,安全,效率低。查询快,增删慢。

                                           |---LinkedList

                                                    |---底层数据结构是双向循环链表

                                                    |---线程不同步,不安全,效率高。查询慢,增删快。

                                    |---Set

                                           |---HashSet

                                                     |---底层数据结构是哈希表

                                                     |---如何保证唯一性?

                                                                        |---依赖hashCode()与equals()

                                                                                       |---执行顺序

                                                                                                 |---先判断hashCode()是否相同?

                                                                                                                        |---是

                                                                                                                               |---继续走equals(),返回值

                                                                                                                                             |---true

                                                                                                                                                    |---元素重复,不添加

                                                                                                                                             |---false

                                                                                                                                                    |---元素不重复,添加

                                                                                                                        |---否

                                                                                                                               |---直接添加

                                           |---TreeSet

                                                    |---底层数据结构是二叉树

                                                    |---如何保证唯一性?

                                                               |---根据返回值是否为0

                                                    |---如何排序

                                                               |---自然排序:Comparable

                                                               |---比较器排序:Comparator

                        |---Map

                                |---HashMap

                                           |---LinkedHashMap

                                |---Hashtable

                                |---TreeMap

  • 相关阅读:
    计数
    表单验证
    获取位置
    jq事件
    jq选择器
    PHP超文本预处理器(通用开源脚本语言)
    文字超出两行显示省略号
    Vim配置:在win10下用vim编译运行C/C++(异步插件管理,一键运行)
    实验楼HTML基础入门学习
    博客园美化:添加目录,标题设置,代码高亮,主题设置
  • 原文地址:https://www.cnblogs.com/zhanfuxing/p/3643724.html
Copyright © 2020-2023  润新知