java collection List, Map, Set, Queue use

Collection. A sequence of independent elements that all obey one or more rules. Lists must hold elements in the order they were inserted, while Sets cannot have duplicate elements.
Map. A set of paired key-value pair objects, allowing keys to be used to look up values.

There are 2 types of Lists:
Basic ArrayList, longer than random access to elements, but slower when inserting and removing elements in the middle of the List.
LinkedList, which provides optimized sequential access by inserting and deleting operations in the middle of the List at low cost. LinkedList is relatively slow for random access.


Iterator Iterator
1. Use the method iterator() to ask the container to return an Iterator. Iterator will be ready to return the first element of the sequence.
2. Use next() to get the next element in the sequence.
3. Use hashNext() to check if there are more elements in the sequence.
4. Use remove() to remove the newly returned element of the iterator.


Set: Do not save duplicate elements.
HashSet: No sorting, use a hash function. Provide the fastest query speed
TreeSet: Stores elements in a red-black tree data structure, sorted


Map: key-value pair
HashMap for quick access,
TreeMap keeps keys always sorted.


Stack stack usually refers to the last-in-first-out container (LIFO), and LinkedList can implement all the functions of the stack.

Queue: queue (first in, first out)

    public static void testList(){
        List<Integer> list = new ArrayList<>();
        list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);list.add(6);list.add(7);
        System.out.println(list.toString());
        Iterator it = list.iterator();
        while (it.hasNext()){
           int i = (Integer) it.next();
            System.out.println(i);
        }
        System.out.println(list.toString());
        it = list.iterator();
        for (int i = 0; i < 3; i++) {
            it.next();
            it.remove();
        }
        System.out.println(list.toString());
    }

    public static void testLinkedList(){
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);list.add(6);list.add(7);
        System.out.println(list.toString());
        System.out.println("getFirst():"+list.getFirst());
        System.out.println("getLast():"+list.getLast());
        System.out.println("element():"+list.element());
        System.out.println("peek():"+list.peek());
        System.out.println("remove():"+list.remove());
        System.out.println("removeFirst():"+list.removeFirst());
        System.out.println("poll():"+list.poll());
        System.out.println(list.toString());
        System.out.println(list.pop());
        System.out.println(list.toString());
        list.addFirst(8);
        list.offer(9);
        list.addLast(10);
        System.out.println(list.toString());
        list.push(11);
        System.out.println(list.toString());
        list.pop();
        System.out.println(list.toString());
    }

    public static void testMap(){
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"10");map.put(2,"20");map.put(3,"30");map.put(4,"40");
        System.out.println(map.keySet()); // get key
        System.out.println(map.values()); // get the values

        // keySet
        for (Integer i : map.keySet()){ // get value by key
            System.out.println(i+"  "+map.get(i));
        }
        // entrySet
        Iterator iterator = map.entrySet().iterator(); // Get Set of key and value
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
        // keySet
        Iterator it = map.keySet().iterator();
        while (it.hasNext()){
            int key = (int) it.next();
            String value = map.get(key);
            System.out.println(key+"   "+value);
        }
    }


    /**
     * Queue: first in first out
     * offer(Object o)
     * poll()
     */
    public static void testQueue(){
        Queue queue = new LinkedList();
        queue.offer(1);queue.offer(2);queue.offer(3);queue.offer(4);
        System.out.println(queue.toString());
        System.out.println(queue.poll());
        System.out.println(queue.toString());
    }
    /**
     * Stack: first in, last out
     * push(Object 0)
     * pop()
     */
    public static void testStack(){
        LinkedList stack = new LinkedList();
        stack.push(1 );stack.push(2 );stack.push(3 );stack.push(4 );
        System.out.println(stack.toString());
        System.out.println(stack.pop());
        System.out.println(stack.toString());
    }

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325378795&siteId=291194637