zoukankan      html  css  js  c++  java
  • day18 Map 的用法

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

    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():
    * V get(Object key):根据键获取值
    * Set<K> keySet():获取集合中所有键的集合
    * Collection<V> values():获取集合中所有值的集合
    * e:长度功能
    * int size():返回集合中的键值对的个数

    迭代方法

    第一种 用keySet

    Map<String, Integer> map = new HashMap<>();
    map.put("张三", 23);
    map.put("李四", 24);
    map.put("王五", 25);
    map.put("赵六", 26);
    Set<String> s = map.keySet();
    Iterator<String> i = s.iterator();
    while (i.hasNext()) {
    String key = i.next();
    Integer value = map.get(key);
    System.out.println(key + " " + value);
    }
    }

    /*

    //使用增强for循环遍历
    for(String key : map.keySet()) { //map.keySet()是所有键的集合
    System.out.println(key + "=" + map.get(key));
    }

    */

    第二种:entrySet

    /**
    * Map集合的第二种迭代,根据键值对对象,获取键和值
    * A:键值对对象找键和值思路:
    * 获取所有键值对对象的集合
    * 遍历键值对对象的集合,获取到每一个键值对对象
    * 根据键值对对象找键和值
    */ 

    Map<String, Integer> map = new HashMap<>();
    map.put("张三", 23);
    map.put("李四", 24);
    map.put("王五", 25);
    map.put("赵六", 26);

    /*
    * Set<Entry<String,Integer>> s=map.entrySet();
    * Iterator<Entry<String,Integer>> i=s.iterator();
    * while(i.hasNext()){
    * Entry<String,Integer> ss=i.next();
    * String key= ss.getKey();
    * Integer value=ss.getValue();
    * System.out.println(key+" "+value);
    */

    for (Entry<String, Integer> en : map.entrySet()) {
    System.out.println(en.getKey() + " " + en.getValue());
    }
    }

    /*
    * * A:案例演示
    * HashMap集合键是Student值是String的案例
    * 键是学生对象,代表每一个学生
    * 值是字符串对象,代表学生归属地
    */
    public static void main(String[] args) {
    HashMap<Student, String> hm = new HashMap<>();
    hm.put(new Student("张三", 23), "北京");
    hm.put(new Student("张三", 23), "上海");
    hm.put(new Student("李四", 24), "广州");
    hm.put(new Student("王五", 25), "深圳");

    System.out.println(hm);
    }

    集合框架(LinkedHashMap的概述和使用)
    * A:案例演示
    * LinkedHashMap的特点
    * 底层是链表实现的可以保证怎么存就怎么取

    TreeMap

    /**
    * * A:案例演示
    * TreeMap集合键是Student值是String的案例
    */

    //比较器,按照姓名比较,使用特定的比较器对关键字进行排序 
    public static void main(String[] args) {
    TreeMap<Student, String> tm = 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;
    }
    });
    tm.put(new Student("张三", 23), "北京");
    tm.put(new Student("李四", 13), "上海");
    tm.put(new Student("赵六", 43), "深圳");
    tm.put(new Student("王五", 33), "广州");

    System.out.println(tm);
    demo1();
    }

    例题统计字符串中每个字符出现的次数
    * 需求:统计字符串中每个字符出现的次数
    String str = "aaaabbbcccccccccc";
    char[] arr = str.toCharArray(); //将字符串转换成字符数组
    HashMap<Character, Integer> hm = new HashMap<>(); //创建双列集合存储键和值

    for(char c: arr) {
    /*if(!hm.containsKey(c)) { //如果不包含这个键
    hm.put(c, 1);
    }else {
    hm.put(c, hm.get(c) + 1);
    }*/
    hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);
    }
    //6,打印双列集合获取字符出现的次数
    for (Character key : hm.keySet()) { //hm.keySet()代表所有键的集合
    System.out.println(key + "=" + hm.get(key));//hm.get(key)根据键获取值
    }


    }

    18.09_集合框架(集合嵌套之HashMap嵌套HashMap)
    * A:案例演示
    * 集合嵌套之HashMap嵌套HashMap

    /**
    * * A:案例演示
    * 集合嵌套之HashMap嵌套HashMap
    *
    * 需求:
    * 双元课堂有很多基础班
    * 第88期基础班定义成一个双列集合,键是学生对象,值是学生的归属地
    * 第99期基础班定义成一个双列集合,键是学生对象,值是学生的归属地
    *
    * 无论88期还是99期都是班级对象,所以为了便于统一管理,把这些班级对象添加到双元课堂集合中
    */
    public static void main(String[] args) {
    //定义88期基础班
    HashMap<Student, String> hm88 = new HashMap<>();
    hm88.put(new Student("张三", 23), "北京");
    hm88.put(new Student("李四", 24), "北京");
    hm88.put(new Student("王五", 25), "上海");
    hm88.put(new Student("赵六", 26), "广州");

    //定义99期基础班
    HashMap<Student, String> hm99 = new HashMap<>();
    hm99.put(new Student("唐僧", 1023), "北京");
    hm99.put(new Student("孙悟空",1024), "北京");
    hm99.put(new Student("猪八戒",1025), "上海");
    hm99.put(new Student("沙和尚",1026), "广州");

    //定义双元课堂
    HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
    hm.put(hm88, "第88期基础班");
    hm.put(hm99, "第99期基础班");

    //遍历双列集合
    for(HashMap<Student, String> h : hm.keySet()) { //hm.keySet()代表的是双列集合中键的集合
    String value = hm.get(h); //get(h)根据键对象获取值对象
    //遍历键的双列集合对象
    for(Student key : h.keySet()) { //h.keySet()获取集合总所有的学生键对象
    String value2 = h.get(key);

    System.out.println(key + "=" + value2 + "=" + value);
    }
    }

    18.10_集合框架(HashMap和Hashtable的区别)
    * A:面试题
    * HashMap和Hashtable的区别
    * Hashtable是JDK1.0版本出现的,是线程安全的,效率低,HashMap是JDK1.2版本出现的,是线程不安全的,效率高
    * Hashtable不可以存储null键和null值,HashMap可以存储null键和null值
    * B:案例演示
    * HashMap和Hashtable的区别

    Collection 的用法

    /**
    * Collecitons中的常见方法
    * 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)
    */

    ArrayList<String> list = new ArrayList<>();
    list.add("a");
    list.add("c");
    list.add("d");
    list.add("f");
    list.add("g");

    System.out.println(Collections.binarySearch(list, "c"));
    System.out.println(Collections.binarySearch(list, "b"));

    }

    总结一下哈:


    /**
    * Collection
    * List(存取有序,有索引,可以重复)
    * ArrayList
    * 底层是数组实现的,线程不安全,查找和修改快,增和删比较慢
    * LinkedList
    * 底层是链表实现的,线程不安全,增和删比较快,查找和修改比较慢
    * Vector
    * 底层是数组实现的,线程安全的,无论增删改查都慢
    * 如果查找和修改多,用ArrayList
    * 如果增和删多,用LinkedList
    * 如果都多,用ArrayList
    * Set(存取无序,无索引,不可以重复)
    * HashSet
    * 底层是哈希算法实现
    * LinkedHashSet
    * 底层是链表实现,但是也是可以保证元素唯一,和HashSet原理一样
    * TreeSet
    * 底层是二叉树算法实现
    * 一般在开发的时候不需要对存储的元素排序,所以在开发的时候大多用HashSet,HashSet的效率比较高
    * TreeSet在面试的时候比较多,问你有几种排序方式,和几种排序方式的区别
    * Map
    * HashMap
    * 底层是哈希算法,针对键
    * LinkedHashMap
    * 底层是链表,针对键
    * TreeMap
    * 底层是二叉树算法,针对键
    * 开发中用HashMap比较多
    */

  • 相关阅读:
    中国国家气象局天气预报信息接口
    windows文件同步到linux
    页面元素自适应
    js对浮点数计算的bug
    ajax跨域报错
    html5使用js确定用户坐标位置
    linux下rsync服务的搭建
    linux基本指令
    js控制页面刷新大全
    pdf在浏览器的显示问题
  • 原文地址:https://www.cnblogs.com/lixiaowei395659729/p/7028864.html
Copyright © 2011-2022 走看看