当前位置:网站首页>Differences between list and set access elements

Differences between list and set access elements

2022-06-11 16:18:00 zhangsan3333

 Insert picture description here

  • Collection aggregate ( Interface ) Under the interface of 2 A direct sub interface :

    • List aggregate ( Interface ): You can save duplicate elements , Have subscript , Storage order , Can store multiple null Elements .

      • ​ArrayList class : The bottom layer is a variable array , Operate according to the subscript , Fast query efficiency , The efficiency of addition and deletion is low .

      • LinkedList class : At the bottom is the list , Operate according to the head and tail of the linked list , Fast addition and deletion efficiency , Query efficiency is low .

    • Set aggregate ( Interface ): Cannot save duplicate elements , No subscript . Can be stored null But only one . And the order of access is not guaranteed , That is, for sets set There is no order when accessing , There is no law .

package SetTest;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Set3 {
    
    public static void main(String[] args) {
    
        Set<String> set = new HashSet();
        set.add("c");
        set.add("b");
        set.add("a");
        set.add("a");
        set.add(null);
        set.add(null);
        for (String s1 : set) {
    
            System.out.println("s1 = " + s1);
        }
        System.out.println();

        List<String> list = new ArrayList<>();
        list.add("c");
        list.add("b");
        list.add("a");
        list.add("a");
        list.add(null);
        list.add(null);
        for (String s2 : list) {
    
            System.out.println("s2 = " + s2);
        }
    }
}

 Insert picture description here

原网站

版权声明
本文为[zhangsan3333]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111600221258.html