Basic use of List collection and Map

List and Map are two very common and important data structures when it comes to Java programming. They provide different ways to organize and manipulate collections of data. In this blog, we will explain the concept, characteristics and usage of List and Map in detail, and provide some sample codes.

List (list)
List is an ordered collection that allows the storage of repeated elements. It is one of the most commonly used interfaces in the Java collection framework and is generally used to store and manipulate a set of elements. The following are some important features and commonly used operations of List:

  • Orderedness: The elements in the List are stored in the order of insertion, and the elements can be accessed and manipulated according to the index.
  • Repeatability: List allows storing repeated elements, and the same element can appear multiple times.
  • Dynamic size: The size of the List can be dynamically adjusted as needed, and elements can be added or deleted.

In Java, List is an interface, and there are many implementation classes to choose from, such as ArrayList, LinkedList, and Vector. Here is some sample code showing the basic usage of List:

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

public class ListExample {
    
    
    public static void main(String[] args) {
    
    
        // 创建一个ArrayList对象
        List<String> fruits = new ArrayList<>();

        // 添加元素到列表
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // 获取列表大小
        int size = fruits.size();
        System.out.println("列表大小:" + size);

        // 遍历列表
        for (String fruit : fruits) {
    
    
            System.out.println(fruit);
        }

        // 根据索引获取元素
        String firstFruit = fruits.get(0);
        System.out.println("第一个水果:" + firstFruit);

        // 删除元素
        fruits.remove(1);
        System.out.println("删除后的列表:" + fruits);
    }
}

Map (mapping)
Map is a collection of key-value pairs, which maps keys to corresponding values. It provides a quick way to find and access data. The following are some important features and commonly used operations of Map:

  • Key-value pairs: The elements in the Map are stored in the form of key-value pairs, and each key corresponds to a unique value.
  • Quick lookup: You can quickly look up the corresponding value through the key without traversing the entire collection.
  • Unordered: The elements in the Map have no fixed order, and different implementation classes may iterate in different orders.

In Java, Map is also an interface, and there are many implementation classes to choose from, such as HashMap, TreeMap, and LinkedHashMap. Here is some sample code showing the basic usage of Map:

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

public class MapExample {
    
    
    public static void main(String[] args) {
    
    
        // 创建一个HashMap对象
        Map<String, Integer> students = new HashMap<>();

        // 添加键值对到Map
        students.put("Alice", 18);
        students.put("Bob", 20);
        students.put("Charlie", 19);

        // 获取Map的大小
        int size = students.size();
        System.out.println("Map的大小:" + size);

        // 遍历Map的键值对
        for (Map.Entry<String, Integer> entry : students.entrySet()) {
    
    
            String name = entry.getKey();
            int age = entry.getValue();
            System.out.println(name + " 的年龄是:" + age);
        }

        // 根据键获取值
        int aliceAge = students.get("Alice");
        System.out.println("Alice 的年龄是:" + aliceAge);

        // 判断键是否存在
        boolean hasKey = students.containsKey("Bob");
        System.out.println("是否包含 Bob:" + hasKey);

        // 删除键值对
        students.remove("Charlie");
        System.out.println("删除后的Map:" + students);
    }
}

Through the above code, we can see the basic usage of List and Map. List is used for storing and manipulating elements in an orderly manner, while Map is used for finding and accessing values ​​​​by keys.

Hope this blog is helpful to you. If you have additional questions, please feel free to ask.

Guess you like

Origin blog.csdn.net/weixin_65837469/article/details/131460805