Java-List collection, Set collection, Map collection

1. List collection

ArrayList is a dynamic array

LinkedList storage structure based on linked list

Features:

1.ArrayList query is fast, addition and deletion are slow

2.LinkedList query is slow, addition and deletion are fast

List collections are ordered and elements can be repeated

    List<String> list = new ArrayList<>();
        list.add("a");
        list.add("d");
        list.add("y");
        list.add("m");
        System.out.println(list);
        System.out.println(list.get(2));
        System.out.println("list集合的遍历");
        for(int i = 0; i< list.size(); i++){
            String s = list.get(i);
            System.out.println(s);
        }
        for(String s : list){
            System.out.println(s);
        }

 2. Set collection

The Set collection cannot directly obtain an element, but can only traverse the elements through an iterator.

Popularly understood as a primary school student's schoolbag, elements are thrown directly into it without any order, so the elements cannot be repeated.

The elements stored in Set are non-repeatable and the elements are unordered.

Set<String> set = new HashSet<>();
        set.add("z");
        set.add("t");
        set.add("y");
        set.add("s");
        System.out.println(set);
        System.out.println("set集合的遍历");
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            String a = iterator.next();
            System.out.println(a);
        }

 3. Map collection

Data structure based on key-value pair storage

In Map, keys are not allowed to be repeated, but values ​​can be repeated.

Map will store all keys in a set collection

 

 Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",18);
        map.put("sex","男");
        map.put("address","海南");
        System.out.println(map);
        System.out.println("Map的遍历");
        Set<String> keySet = map.keySet();//获取map中所有Key值的set集合
        Iterator<String> ite = keySet.iterator();
        while (ite.hasNext()){
            String key = ite.next();//获取key的值
            Object value = map.get(key);
            System.out.println(key+"--->"+value);
        }

Guess you like

Origin blog.csdn.net/weixin_69036336/article/details/128154932