第七天集合(Day 7 assemble)

今天主要学了Map集合直接实现或间接实现类有HashMap,LinkedMap,TreeMap,Hashtable,Properties其中最常用的是HashMap,里面存储的键值对是无序的,不可重复的。HashMap的存储结构为数组+链表+红黑树。LinkedMap是HashMap的子类,通过链表存储键值对,保证了数据的顺序。TreeMap也是有序的,可通过自然排序或自定义排序实现。Collections工具类是针对List,Map,Set集合的工具类,可以实现数据排序、翻转等操作。这些集合的底层实现还是等后期再来研究吧。

Map集合

  • Map集合概述
  • lnterface MapK:键的类型;V:值的类型
  • 将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值

创建Map集合的对象

  • 多态的方式
  • Map

    HashMap:作为Map的主要实现类,线程不安全,效率高,可以存储null的key和value

    LinkedHashMap:保证在遍历map元素时,可以按照添加的顺序实现遍历,在原有的HashMap底层基础上添加了一对指针,指向前一个和后一个元素,对于频繁的遍历其效率高于HashMap

    TreeMap:按照添加的key-value对进行排序,实现排序遍历,此时考虑key的自然排序或定义排序底层使用红黑树

    Hashtable:作为古老的实现类,线程安全,效率低,不能存储null的key和value

    Properties:常用来处理配置文件,key和value都是String类型

  • HashMap:作为Map的主要实现类,线程不安全,效率高,可以存储null的key和value

    LinkedHashMap:保证在遍历map元素时,可以按照添加的顺序实现遍历,在原有的HashMap底层基础上添加了一对指针,指向前一个和后一个元素,对于频繁的遍历其效率高于HashMap

  • LinkedHashMap:保证在遍历map元素时,可以按照添加的顺序实现遍历,在原有的HashMap底层基础上添加了一对指针,指向前一个和后一个元素,对于频繁的遍历其效率高于HashMap
  • TreeMap:按照添加的key-value对进行排序,实现排序遍历,此时考虑key的自然排序或定义排序底层使用红黑树
  • Hashtable:作为古老的实现类,线程安全,效率低,不能存储null的key和value

    Properties:常用来处理配置文件,key和value都是String类型

  • Properties:常用来处理配置文件,key和value都是String类型
  • Map结构的理解

    Map中key:无序的,不可重复的,使用Set存储所有的key—–>key所在的类要重写equals方法和hashCode方法 (以HashMap为例)

    Map中value:无序的,可重复的,使用Collection存储所有的value———>value所在的类要重写equals方法

    一个键值对key-value构成了一个Entry对象

    Map中的Entry:无序的、不可重复的使用Set存储所有的Entry

  • Map中key:无序的,不可重复的,使用Set存储所有的key—–>key所在的类要重写equals方法和hashCode方法 (以HashMap为例)
  • Map中value:无序的,可重复的,使用Collection存储所有的value———>value所在的类要重写equals方法
  • 一个键值对key-value构成了一个Entry对象
  • Map中的Entry:无序的、不可重复的使用Set存储所有的Entry

HashMap

        Map<String,String> map = new HashMap<>();        map.put("张无忌","赵敏");        map.put("郭靖","黄蓉");        map.put("杨过","小龙女");        System.out.println(map.remove("郭靖"));        System.out.println(map.containsValue("小龙女"));        System.out.println(map);        System.out.println(map.size());==========黄蓉true{杨过=小龙女, 张无忌=赵敏}2
        Map<String,String> map = new HashMap<>();        map.put("张无忌","赵敏");        map.put("郭靖","黄蓉");        map.put("杨过","小龙女");        //获取键对应的值        System.out.println(map.get("张无忌"));        System.out.println("===========");        //获取键的集合返回的是set集合        Set<String> set = map.keySet();        for (String s : set) {            System.out.println(s);        }        System.out.println("===========");        //获取值的集合返回的是Collection        Collection<String> cle = map.values();        for (String s : cle) {            System.out.println(s);        }

上面是遍历方法一

下面是遍历方法二通过entrySet方法

        Map map = new HashMap<>();        map.put("张无忌","赵敏");        map.put("郭靖","黄蓉");        map.put("杨过","小龙女");        //entrySet        Set entry = map.entrySet();        Iterator iterator = entry.iterator();        while (iterator.hasNext()){            Object obj = iterator.next();            //Entry中的元素都是entry            Map.Entry et = (Map.Entry) obj;            System.out.println(et.getKey()+","+et.getValue());        }

第三种遍历key取出对应的value

        Set set = map.keySet();        for (Object o : set) {            Object obj = map.get(o);            System.out.println(o+","+obj);        }

TreeMap

public static void main(String[] args) {    TreeMap map = new TreeMap();    Student s1 = new Student("Tom",23);    Student s2 = new Student("Bob",20);    Student s3 = new Student("Jack",18);    Student s4 = new Student("Jerry",17);    map.put(s1,95);    map.put(s2,96);    map.put(s3,94);    map.put(s4,97);    Set set = map.entrySet();    Iterator iterator = set.iterator();    while (iterator.hasNext()){        Object obj = iterator.next();        Map.Entry me = (Map.Entry) obj;        System.out.println(me.getKey()+","+me.getValue());    }            private String name;    private int age;    public Student(){}    public Student(String name, int age){        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public int compareTo(Object o) {        //按照年龄从小到大排序        Student student = (Student) o;        int num = this.age - student.age;        //如果年龄相同那么判断名字是否相同        int num2 = num == 0 ? this.name.compareTo(student.name) : num;        return num2;    }    @Override    public String toString() {        return "Student{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }=========Student{name='Jerry', age=17},97Student{name='Jack', age=18},94Student{name='Bob', age=20},96Student{name='Tom', age=23},95

Properties

  • Properties类是Hashtable的子类,该对象用于处理属性文件
  • 由于属性文件里的key、value都是字符串类型,所以Properties里的 key和l value都是字符串类型
  • 存取数据时,建议使用setProperty(String key,String value)方法和getProperty(String key)方法
/ /Properties:常用来处理配置文件。key和value都是string类型public static void main(String[] args) throws Exception {Properties pros = new Properties();FileInputStream fis = new FileInputstream( name:"jdbc.properties");pros. load(fis);//加载流对应的文件string name = pros.getProperty( "name" ) ;String password = pros.getProperty( "password" );system.out.println("name = " + name + ", password = " + password);

Collections工具类

  • Collections是一个操作Set、List和l Map 等集合的工具类
  • collections 中提供了一系列静态的万法对集合元系进行排序、查询和修改器等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法
  • 排序操作:(均为static方法)

reverse(List):反转List中元素的顺序 shuffle(List):对List集合元素进行随机排序 sort(List):根据元素的自然顺序对指定List集合元素按升序排序 sort(List,Comparator):根据指定的Comparator一生的o对-S六施东L11/>swap(List,int,int):将指定list集合中的i处元素和j处元素进行交换

reverse(List):反转List中元素的顺序 shuffle(List):对List集合元素进行随机排序 sort(List):根据元素的自然顺序对指定List集合元素按升序排序 sort(List,Comparator):根据指定的Comparator一生的o对-S六施东L11/>swap(List,int,int):将指定list集合中的i处元素和j处元素进行交换

        //copy方法的正确用法                List src = new ArrayList();        List list = Arrays.asList(new Object[src.size()]);        Collections.copy(list,src);        //返回的list1就是线程安全的list        List list1 = Collections.synchronizedList(list);
————————

Today, I mainly learned that the direct or indirect implementation classes of map collection include HashMap, linkedmap, treemap, hashtable and properties. The most commonly used is HashMap. The key value pairs stored in it are disordered and non repeatable. The storage structure of HashMap is array + linked list + red black tree. Linkedmap is a subclass of HashMap. It stores key value pairs through a linked list to ensure the order of data. Treemap is also ordered and can be realized by natural sorting or custom sorting. Collections tool class is a tool class for list, map and set collections, which can realize data sorting, flipping and other operations. The underlying implementation of these collections should be studied later.

Map集合

  • Map collection overview
  • lnterface MapK:键的类型;V:值的类型
  • Objects that map keys to values; Cannot contain duplicate keys; Each key can be mapped to a maximum of one value

Create an object for the map collection

  • Polymorphic way
  • Map
    HashMap: as the main implementation class of map, the thread is unsafe and efficient. It can store null keys and values
    LinkedHashMap: ensure that when traversing map elements, traversal can be realized in the order of addition. A pair of pointers are added on the bottom of the original HashMap to point to the previous and subsequent elements. For frequent traversal, its efficiency is higher than that of HashMap
    Treemap: sort according to the added key value pairs to realize sorting traversal. At this time, consider the natural sorting of keys or define sorting, and the red black tree is used at the bottom
    Hashtable: as an ancient implementation class, it is thread safe and inefficient. It cannot store null keys and values
    Properties: commonly used to handle configuration files. Both key and value are string types
  • HashMap: as the main implementation class of map, the thread is unsafe and efficient. It can store null keys and values
    LinkedHashMap: ensure that when traversing map elements, traversal can be realized in the order of addition. A pair of pointers are added on the bottom of the original HashMap to point to the previous and subsequent elements. For frequent traversal, its efficiency is higher than that of HashMap
  • LinkedHashMap: ensure that when traversing map elements, traversal can be realized in the order of addition. A pair of pointers are added on the bottom of the original HashMap to point to the previous and subsequent elements. For frequent traversal, its efficiency is higher than that of HashMap
  • Treemap: sort according to the added key value pairs to realize sorting traversal. At this time, consider the natural sorting of keys or define sorting, and the red black tree is used at the bottom
  • Hashtable:作为古老的实现类,线程安全,效率低,不能存储null的key和value

    Properties:常用来处理配置文件,key和value都是String类型

  • Properties:常用来处理配置文件,key和value都是String类型
  • Understanding of map structure
    Keys in map: unordered and non repeatable. Use set to store all keys —— > the class where the key is located should override the equals method and hashcode method (take HashMap as an example)
    Value in the map: unordered and repeatable. Use the collection to store all values ——— > the class where value is located should override the equals method
    A key value pair key value constitutes an entry object
    Entries in map: unordered and non repeatable. Use set to store all entries
  • Keys in map: unordered and non repeatable. Use set to store all keys —— > the class where the key is located should override the equals method and hashcode method (take HashMap as an example)
  • Value in the map: unordered and repeatable. Use the collection to store all values ——— > the class where value is located should override the equals method
  • A key value pair key value constitutes an entry object
  • Entries in map: unordered and non repeatable. Use set to store all entries

HashMap

        Map<String,String> map = new HashMap<>();        map.put("张无忌","赵敏");        map.put("郭靖","黄蓉");        map.put("杨过","小龙女");        System.out.println(map.remove("郭靖"));        System.out.println(map.containsValue("小龙女"));        System.out.println(map);        System.out.println(map.size());==========黄蓉true{杨过=小龙女, 张无忌=赵敏}2
        Map<String,String> map = new HashMap<>();        map.put("张无忌","赵敏");        map.put("郭靖","黄蓉");        map.put("杨过","小龙女");        //获取键对应的值        System.out.println(map.get("张无忌"));        System.out.println("===========");        //获取键的集合返回的是set集合        Set<String> set = map.keySet();        for (String s : set) {            System.out.println(s);        }        System.out.println("===========");        //获取值的集合返回的是Collection        Collection<String> cle = map.values();        for (String s : cle) {            System.out.println(s);        }

The above is traversal method 1

The following is the traversal method 2 through the entryset method

        Map map = new HashMap<>();        map.put("张无忌","赵敏");        map.put("郭靖","黄蓉");        map.put("杨过","小龙女");        //entrySet        Set entry = map.entrySet();        Iterator iterator = entry.iterator();        while (iterator.hasNext()){            Object obj = iterator.next();            //Entry中的元素都是entry            Map.Entry et = (Map.Entry) obj;            System.out.println(et.getKey()+","+et.getValue());        }

The third way is to traverse the key and get the corresponding value

        Set set = map.keySet();        for (Object o : set) {            Object obj = map.get(o);            System.out.println(o+","+obj);        }

TreeMap

public static void main(String[] args) {    TreeMap map = new TreeMap();    Student s1 = new Student("Tom",23);    Student s2 = new Student("Bob",20);    Student s3 = new Student("Jack",18);    Student s4 = new Student("Jerry",17);    map.put(s1,95);    map.put(s2,96);    map.put(s3,94);    map.put(s4,97);    Set set = map.entrySet();    Iterator iterator = set.iterator();    while (iterator.hasNext()){        Object obj = iterator.next();        Map.Entry me = (Map.Entry) obj;        System.out.println(me.getKey()+","+me.getValue());    }            private String name;    private int age;    public Student(){}    public Student(String name, int age){        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public int compareTo(Object o) {        //按照年龄从小到大排序        Student student = (Student) o;        int num = this.age - student.age;        //如果年龄相同那么判断名字是否相同        int num2 = num == 0 ? this.name.compareTo(student.name) : num;        return num2;    }    @Override    public String toString() {        return "Student{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }=========Student{name='Jerry', age=17},97Student{name='Jack', age=18},94Student{name='Bob', age=20},96Student{name='Tom', age=23},95

Properties

  • Properties类是Hashtable的子类,该对象用于处理属性文件
  • Since the key and value in the properties file are of string type, the key and L value in properties are of string type
  • 存取数据时,建议使用setProperty(String key,String value)方法和getProperty(String key)方法
/ /Properties:常用来处理配置文件。key和value都是string类型public static void main(String[] args) throws Exception {Properties pros = new Properties();FileInputStream fis = new FileInputstream( name:"jdbc.properties");pros. load(fis);//加载流对应的文件string name = pros.getProperty( "name" ) ;String password = pros.getProperty( "password" );system.out.println("name = " + name + ", password = " + password);

Collections工具类

  • Collections是一个操作Set、List和l Map 等集合的工具类
  • Collections provides a series of static ten thousand methods to sort, query and modify the collection element system. It also provides methods to set immutable collection objects and realize synchronous control of collection objects
  • Sorting operations: (all static methods)

Reverse (list): reverse the order of the elements in the list shuffle (list): randomly sort the elements of the list set sort (list): sort the elements of the specified list set in ascending order according to the natural order of the elements sort (list, comparator): apply – s six times L11 / & gt; Swap (list, int, int): exchange the elements at I and j in the specified list set

Reverse (list): reverse the order of the elements in the list shuffle (list): randomly sort the elements of the list set sort (list): sort the elements of the specified list set in ascending order according to the natural order of the elements sort (list, comparator): apply – s six times L11 / & gt; Swap (list, int, int): exchange the elements at I and j in the specified list set

        //copy方法的正确用法                List src = new ArrayList();        List list = Arrays.asList(new Object[src.size()]);        Collections.copy(list,src);        //返回的list1就是线程安全的list        List list1 = Collections.synchronizedList(list);