[Set framework] List, Set and Map

1, Basic points

1. A collection is similar to an array, but the number of elements in the collection can change dynamically
Note: a collection can only store user-defined data types, not basic data types

2. Sets are divided into three categories

  • List: the stored data can be repeated and in order
  • Set: the stored data is non repeatable and unordered (TreeSet is ordered under specific circumstances)
  • Map: double column type. The stored data key cannot be repeated, and the value can be repeated

1, List interface

It has two implementation classes ArrayList and LinkedList

list.clear() -- Method to delete all elements

1,ArrayList

ArrayList is implemented based on array. The bottom layer is a dynamic Object array, and its capacity can grow automatically
Because it is an array structure with subscripts, the addition and deletion is very slow and the query is very fast

1) Single query: list Get (subscript);

2) Loop traversal:

  • For loop: for (int i = 0; I < list. Size(); i++)
  • foreach loop: for(Object temp: list)
  • lambda expression: list foreach(r -> {})
    Note: lambda expressions cannot directly traverse arrays. They need to be converted into sets before they can be traversed by lambda expressions

3) Generic: used to specify the specific storage data type for the collection. It can only store user-defined data types, not basic data types
For example, list alit = new arraylist();

4) Add data: list add()

5) Delete operation
list.remove() can be deleted by subscript or content (if the content is also of Integer type, it may be confused with subscript, so the function of generics will be displayed at this time. When deleted by this method, the collection will use generics)
list.clear() clears all elements

6) Update operation: list Set (index, object) updates the element of the specified subscript

package com.decade.list;

import java.util.ArrayList;
import java.util.List;

public class ArrayListDemo01 {

    public static void main(String[] args) {
        //Create an ArrayList set with a default length of 10
        ArrayList arrayList = new ArrayList();

        //increase
        arrayList.add("one");
        arrayList.add(true);
        arrayList.add(3.14F);

        //foreach loop 
        for (Object temp: arrayList) {
            System.out.println(temp);
        }

        System.out.println("===============");

        //Delete
        arrayList.remove(1);

        //foreach loop 
        for (Object temp: arrayList) {
            System.out.println(temp);
        }

        System.out.println("===============");

        //change
        arrayList.set(0, "test");

        //foreach loop 
        for (Object temp: arrayList) {
            System.out.println(temp);
        }

        System.out.println("===============");

        //generic paradigm
        List<String> bList = new ArrayList<String>();
        bList.add("a");

        bList.forEach(System.out::println);
    }
}

2,LinkedList

1) LinkedList is a linked list at the bottom, which is very fast to add and delete, but the query is relatively slow
The methods of LinkedList and ArrayList are the same

2) Convert collection to array
Object[] array = list.toArray();

package com.decade.list;

import java.util.LinkedList;
import java.util.List;

public class LinkedListDemo02 {

    public static void main(String[] args) {
        //Declare a LinkedList
        List<String> aList = new LinkedList<String>();

        aList.add("aaa");
        aList.add("bbb");

        //Convert collection to array
        Object[] array = aList.toArray();
    }
}

2, Set interface

It has two implementation classes, HashSet and TreeSet
set.clear() -- Method to delete all elements

1,HashSet

1) The data stored in the HashSet set is out of order (not in order, not out of order) and non repeatable. If you store duplicate data, the set will automatically help you to redo it

The default initial capacity is 16 and the loading factor is 0.75, which means 16 * 0.75 = 12
When the number of elements in the set exceeds 12, the set capacity is doubled to 32

HashSet has a hash table at the bottom and no subscript, so it cannot be deleted by subscript
Because of this, there is no modification method in the set set, so you can only delete it first and then add it
There is no get(index) method to get a single element

package com.decade.list;

import java.util.HashSet;
import java.util.Set;

public class HashSetDemo03 {

    public static void main(String[] args) {
        Set<String> aSet = new HashSet<>();
        //increase
        aSet.add("dasdas");
        aSet.add("zasda");
        aSet.add("aasda");
        aSet.add("casdasd");

        aSet.forEach(System.out::println);
        System.out.println("==========");

        //Delete
        aSet.remove("aasda");

        aSet.forEach(System.out::println);
        System.out.println("==========");

        //Change, delete first and then add
        aSet.remove("casdasd");
        aSet.add("bcasdasd");

        aSet.forEach(System.out::println);
        System.out.println("==========");

        boolean flag = aSet.contains("bcasdasd");

        System.out.println("flag = " + flag);
    }
}

2,TreeSet

The bottom layer of TreeSet is binary tree
The stored data cannot be repeated, but it is orderly (for types that implement Comparable and Comparator interfaces)

The method is the same as that in HashSet

package com.decade.list;

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo04 {

    public static void main(String[] args) {
        Set<String> bSet = new TreeSet<>();

        bSet.add("zasda");
        bSet.add("casd");
        bSet.add("aadsas");

        bSet.forEach(System.out::println);
    }
}

3, Map interface

Double column type. The stored data is out of order. The key cannot be repeated and the value can be repeated

1,HashMap

1) The bottom layer is a hash table. key cannot be repeated, value can be repeated and unordered

2) Common methods

  • Insert and modify operations
    map.put(key,value); (the return value of the put method is the old value before this key. If not, null will be returned.)
  • Query map get(key);
  • Traversal operation map Foreach ((k, v) - > {}) note that the double column structure of K, v should also be used when traversing with lambda expressions
    HashMap can be printed directly because its source code covers toString()
  • Delete all methods, HashMap clear()
  • Delete method, HashMap Remove (key). Delete according to the key
package com.decade.list;

import java.util.HashMap;
import java.util.Map;

public class HashMapDemo05 {

    public static void main(String[] args) {

        Map<String, String> hashMap = new HashMap<>();

        //insert
        hashMap.put("asa", "ok");
        hashMap.put("add", "test");
        hashMap.put("bsd", "java");
        hashMap.put("dc", "check");

        //HashMap can be printed directly because its source code covers toString()
        System.out.println(hashMap);

        //Traversal operation map Foreach ((k, v) - > {}) note that the double column structure of K, v should also be used when traversing with lambda expressions
        hashMap.forEach((k,v) -> {
            System.out.println(k + "=" + v);
        } );

        //modify
        hashMap.put("dc", "newValue");
        System.out.println(hashMap);

        //delete
        hashMap.remove("dc");
        System.out.println(hashMap);

        //query
        String value = hashMap.get("bsd");
        System.out.println(value);
    }
}

2,TreeMap

The bottom layer is a binary tree. The key cannot be repeated, and the value can be repeated. It can be sorted according to the key (the type of key needs to implement the Comparable or Comparator interface)

TreeMap has the same method as HashMap

Tags: Java Back-end set

Posted by my800stuff on Tue, 17 May 2022 01:13:38 +0300