Java——List、Ste、Map

List:

Concept: List is an interface inherited from Collection , that is, List is a kind of collection. List is an ordered queue, and each element in the List has an index; the index value of the first element is 0, and the index value of subsequent elements is +1 in turn. Unlike Set, List allows duplicate elements.

ArrayList:

ArrayList is a dynamic, ordered and repeatable array of elements. The following is the data storage structure of ArrayList:

When adding data, the program will automatically open up a space at the end to store the newly added data. If the location is specified, a new space will be opened at the specified location and the original data will be automatically moved back one grid. When deleting an element, the element at the specified index will be deleted, and the rear elements will move forward one space in turn.

Features: fast query, slow addition and deletion

Use of ArrayList:

First instantiate the ArrayList

List<String> list = new ArrayList<>();

add() adds elements: 

Format: variable name.add(added element);

list.add("蔡徐坤");
list.add("真爱粉");
list.add("小黑子");

 

Add an element to the specified position:

Format: variable name.add(add element position index, added element);

list.add(1,"绿尸寒警告");

set() replaces the element at the specified position in this list with the specified element 

Format: variable name.set(index of element to be replaced, replacement value);

list.set(1,"唱,跳,rap,篮球");

 

 get() returns the element at the specified position in the list

Format: variable name.get(index of the element to be returned);

System.out.println(list.get(1));

 

 size() returns the number of elements in this table

Format: variable name.size();

System.out.println(list.size());

remove() removes the element at the specified position in this list 

Format: variable name.remove (the index of the element to be removed);

list.remove(1);

 clear() removes all elements from the collection

Format: variable name.clear();

list.clear()

 

 LinkedList

LinkedList, which also implements the List interface , is different from ArrayList. ArrayList is a dynamic array, while LinkedList is a doubly linked list. The data structure is as follows:

 Features: slow query, adding and deleting blocks

 LinkedList method:

 Set

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

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

Set data structure:

 How to use HashSet

First instantiate the HashSet

Set<String> set = new HashSet<>();

Traversing elements method: 

 //增强for循环
        for (String s : set) {
            System.out.println(s);
        }
        //迭代器
        Iterator<String> aa = set.iterator();
        while (aa.hasNext()){
            String b = aa.next();
            System.out.println(b);
        }

add() If the specified element is not already contained in this set, add the specified element

set.add("树叶");
set.add("风源树叶");
set.add("我是冒险家协会的");
set.add("八神虫子");
set.add("原神的形状");
set.add("你适合去往生堂上班");
set.add("下班!原神,启动");
set.add("你只会变成个丘丘人");

remove() If the specified element exists in the Set collection, remove the element from the Set collection 

set.remove("树叶"):

size() returns the number of elements on this list

System.out.println(set.size());

 clear() removes all elements from the Set collection

set.clear()

 contains() determines whether the Set collection contains the specified element. Contains returns true. does not contain return false

System.out.println(set.contains("风源树叶"));

Map

Map is different from the List and Set interfaces. It is a collection composed of a series of key-value pairs and provides a mapping from key to Value. In Map, it guarantees a one-to-one correspondence between key and value.

In the Map, the key is not allowed to be repeated, and the value of the value can be repeated.

Map will store all keys in a set collection.

data structure:

Instructions: 

Map<String,Object> map = new HashMap<>();

Add element:

map.put("name","蔡徐坤");
map.put("age",2.5);
map.put("sex","男");

 Iterate over elements:

Set<String> strings = map.keySet();
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            String i = iterator.next();
            Object o = map.get(i);
            System.out.println(i + "----" + o);

Guess you like

Origin blog.csdn.net/weixin_69420643/article/details/128192322