Java advanced feature learning (super detailed) one (collection)

1. Introduction to collection framework

1.1 Why use the collection framework

Reason: Since the length of the array is defined by default when the array is created, and the length of the array is unchanged, there will be memory waste in the assignment and creation of the array. Therefore, the collection can be used as an array whose length can be changed, which can solve the memory problem. waste phenomenon

 

1.2 Architecture Diagram of Java Collection Framework

Standard statement: 

  1. The collection has two parent interfaces Collection, Map
  2. Collection has two subinterfaces List and Set
  3. List has two common implementation classes ArrayList and LinkedList
  4. Set has two common implementation classes HashSet and TreeSet
  5. Map has two common implementation classes HashMap and TreeMap

Remarks: The ones marked in red are the common ones and the focus of our study in this chapter, and the other two are not very common

1.3 Features of Java collection framework

(1) List features: orderly, repeatable

(2) Set features: disordered, non-repeatable

(3) Collection features: unordered, repeatable

(4) Map features: store a pair of values ​​[key-value pair] (key key, value value)

2. List interface

2.1 The implementation class of the List interface: (the difference between ArrayList and LinkedList)

        (1) ArrayList features: high query efficiency, low efficiency of addition, deletion and modification [stored in the form of an array]

        (2) LinkedList features: low query efficiency, high efficiency of addition, deletion and modification [stored in the form of a linked list]

2.2 ArrayList collection class

(1) Instantiate the ArrayList object

        //1、实例化集合对象
        ArrayList arrayList = new ArrayList();

(2) Add data add()

        The first: add data in the default order

        //2、给集合添加数组
        arrayList.add(1);
        arrayList.add(true);
        arrayList.add("jojo");
        arrayList.add(23.4);

        The second method: add data through corner mark 

        //第二种给集合添加数据
        arrayList.add(0,1);
        arrayList.add(1,true);
        arrayList.add(2,"jojo");

Remarks : What happens if there is already an element in the corner mark here, the corner mark will be occupied, and the rest of the elements will be moved back

(3) Delete data remove()

The first method: delete data through the corner mark (1 represents the corner mark)

            arrayList.remove(1);

The second type: delete data by value ("1" means that the value is not a subscript)

            arrayList.remove("1");

Remarks : Some students here may have doubts. Isn’t the collection here can store any elements? How can the parameter in remove determine whether the input integer is a parameter or a subscript? For example, if there is 12 here, there is no subtitle in the collection. It is marked with 12 and has the number 12. Will the element with the number 12 be deleted here? You can’t answer here, remove() recognizes the parameters first with the subscript and then the data, and the numbers are all subscripts by default.

(4) Query array

        The first type: traditional for loop

        //3、查询数组中的集合信息【for循环】
        for (int i = 0 ; i < arrayList.size() ; i++){
            System.out.println(arrayList.get(i));
        }

         The second: enhanced for loop

        //第二种获取集合中数据【foreach循环】
        for (Object o : arrayList){
            System.out.println(o);
        }

(5) Expansion: 

        arrayList.size() is the method to get the length of the array

        arrayList.get(i) is to get the value of each element of the array

 

2.3 ArrayList collection class (generic)

        Generic: <> use the specified collection to store the data type, which must be a wrapper type, for example, the basic type int cannot be put in, and the compilation will fail, and the wrapper type Integer of int must be placed

(1) ArrayList stores String type

        ArrayList<String>arrayList = new ArrayList<String>();
        arrayList.add("i'm ");
        arrayList.add("a ");
        arrayList.add("jojo");
        arrayList.add(2,"one");//通常不用这个添加方式

(2) ArrayList stores Integer type

      //泛型中如果存放基本数据类型必须是包装类型,int的包装类型是Integer类型
       ArrayList<Integer>arrayList1 = new ArrayList<Integer>();

(3) ArrayList stores object type data

public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
}
    public static void main(String[] args) {
        User user1 = new User(100,"jojo","jojo123");
        User user2 = new User(101,"小杰","xiaojie123");
        User user3 = new User(102,"奇呀","qiya123");
        User user4 = new User(103,"库洛洛","kuluoluo123");

        ArrayList<User> arrayList = new ArrayList<>();
        arrayList.add(user1);
        arrayList.add(user2);
        arrayList.add(user3);
        arrayList.add(user4);

        for (int i = 0 ; i < arrayList.size() ; i++){
            System.out.println("id:"+arrayList.get(i).getId()+"\t用户名:"+arrayList.get(i).getName()+"\t密码:"+arrayList.get(i).getPwd());
        }
    }

2.4 LinkedList collection class

(1) LinkedList stores Object type data

    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();

        linkedList.add("11");
        linkedList.add("jojo");
        linkedList.add("奇呀");

        for (int i = 0 ; i < linkedList.size() ; i++){
            System.out.println(linkedList.get(i));
        }
    }

 (2) LinkedList stores object type data

        User user1 = new User(100,"jojo","jojo123");
        User user2 = new User(101,"小杰","xiaojie123");
        User user3 = new User(102,"奇呀","qiya123");
        User user4 = new User(103,"库洛洛","kuluoluo123");

        LinkedList<User> linkedList = new LinkedList<>();
        linkedList.add(user1);
        linkedList.add(user2);
        linkedList.add(user3);
        linkedList.add(user4);

(3) Expansion

The generic type of LinkedList has the same meaning as the generic type of ArrayList, but it has some unique methods

        //如果使用LinkedList中独有的方法addFirst(),linkedList.addLast(),removeFirst()等等
        //则不可以使用List<User> linkedList = new LinkedList();只能使用
        //LinkedList<User> linkedList = new LinkedList<>();
        linkedList.addFirst(user2);
        linkedList.addLast(user2);

 

3. Set interface

3.1 Adding data to Set and traversing output

    public static void main(String[] args) {
        Set set = new HashSet() ;
        set.add(1);
        set.add("hh");
        set.add(true);
        set.add(123);
        set.add("小杰");
        set.add("hh");

        //iterator迭代器
        Iterator its = set.iterator();
        while ((its.hasNext())){
            Object o = (Object) its.next();
            System.out.println();
        }
    }

Core point 1: iterator() gets the iterator object

Core point 2: hasnext() determines whether there is a next value

Core point 3: next() gets the next value

Core point 4: The set collection is out of order

Core point 5: sets cannot store duplicate data (deduplication)

3.2 Set Generic -String

    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("小杰");
        set.add("库洛洛");
        set.add("西索");
        set.add("杰洛");

        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Object o = (Object) iterator.next();
            System.out.println(o);
        }
    }

3.3 Set Generic - Object Type

    public static void main(String[] args) {
        User user1 = new User(100,"jojo","jojo123");
        User user2 = new User(101,"小杰","xiaojie123");
        User user3 = new User(102,"奇呀","qiya123");
        User user4 = new User(103,"库洛洛","kuluoluo123");

        Set<User> sets = new HashSet<>();
        sets.add(user1);
        sets.add(user2);
        sets.add(user3);
        sets.add(user4);

        Iterator<User> iterator = sets.iterator();
        while (iterator.hasNext()){
            User user = iterator.next();
            System.out.println("id:"+user.getId()+"\t用户名:"+user.getName()+"\t密码:"+user.getPwd());
        }
    }

4. Map interface

4.1 Use of HashMap

(1) Instantiate HashMap

        //实例化HashMap对象
        Map map = new HashMap();

(2) Assign value to HashMap

        //给HashMap对象添加值
        map.put("a", 1);
        map.put(true,231.1);
        map.put(99, "ok");

(3) traverse HashMap

        a) The first type

        Set set = map.keySet();//获取map集合中所有的key值
        System.out.println(set);

        //1、通过foreach-增强for循环获取所有key对应的value值
        for (Object o : set){
            System.out.printf(map.get(o)+"\t");//获取key对应的value值
        }

        b) The second type

        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Object o = iterator.next();
            System.out.printf(o+"\t");
            System.out.println(map.get(o)+"\t");
        }

4.2 map - Generics

public class Demo10 {
    public static void main(String[] args) {
        //实例化HashMap对象
        Map<String ,Object> map = new HashMap<>();

        //给HashMap对象添加值
        map.put("奇呀", 1);
        map.put("西索",231.1);
        map.put("小杰", true);

        Set<String> set = map.keySet();

        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String st = iterator.next();
            System.out.printf("key:"+st+"\t");
            System.out.println("value:"+map.get(st));
        }

    }
}

5. Comprehensive training

1. Traverse List<Map<String,Object>> (difficulty: medium)

public class Demo12 {
    public static void main(String[] args) {
        Map<String ,Object> map = new HashMap<>();
        map.put("奇呀", 1);
        map.put("西索",231.1);
        map.put("小杰", true);

        Map<String,Object> map0 = new HashMap<>();
        map0.put("团长", 234);
        map0.put("侠客",1.13);
        map0.put("武藏", false);

        List<Map<String ,Object>> list = new ArrayList<>();

        list.add(map);
        list.add(map0);
        for (Map<String ,Object> map1 : list){
            Set<String> set = map1.keySet();
            Iterator<String> iterator = set.iterator();
            while (iterator.hasNext()){
                String st = iterator.next();
                System.out.print("Key:"+st+"\t\t");
                System.out.println("Value:"+map1.get(st));
            }
            System.out.println("************************");
        }
    }

}

2. Traverse List<List<String>> (difficulty: easy)

public class Demo13 {
    public static void main(String[] args) {
        List<String>list = new ArrayList<>();
        list.add("jojo");
        list.add("小杰");
        List<String>list1 = new ArrayList<>();
        list1.add("jojo1");
        list1.add("奇呀");
        List<String>list2 = new ArrayList<>();
        list2.add("jojo2");
        list2.add("库洛洛");

        List<List<String>> lists = new ArrayList<>();
        lists.add(list);
        lists.add(list1);
        lists.add(list2);

        for (List<String> l1 : lists){
            for (String st : l1){
                System.out.println(st);
            }
            System.out.println("**************");
        }

    }
}

3. Traverse Map<String,Map<String,Object>> (difficulty: above average)

public class Demo14 {
    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
        map.put("奇呀", 1);
        map.put("西索",231.1);
        map.put("小杰", true);
        Map<String,Object> map0 = new HashMap<>();
        map0.put("团长", 234);
        map0.put("侠客",1.13);
        map0.put("武藏", false);

        Map<String,Map<String,Object>> map1 = new HashMap<>();
        map1.put("ok",map);
        map1.put("jojo",map0);

        Set<String> set = map1.keySet();
        Iterator<String>iterator = set.iterator();
        while (iterator.hasNext()){
            String st = iterator.next();
            System.out.println("Key:"+st);
            System.out.println("Value:");

            Map<String,Object> mapOne = map1.get(st);
            Set<String> set1 = mapOne.keySet();
            Iterator<String>iterator1 = set1.iterator();
            while (iterator1.hasNext()){
                String st1 = iterator1.next();
                System.out.print("Key:"+st1+"\t");
                System.out.println("Value:"+mapOne.get(st1));
            }
            System.out.println("*********************");
        }
    }
}

Guess you like

Origin blog.csdn.net/jojo_oulaoula/article/details/131236551