Home  >  Article  >  Java  >  How to use Java ArrayList, LinkedList and HashMap

How to use Java ArrayList, LinkedList and HashMap

王林
王林forward
2023-04-27 15:58:071604browse

    1. ArrayList

    1. Principle

    How to use Java ArrayList, LinkedList and HashMap

    2. Usage

    Open a new project in Idea, right-click src to create a new package com.my.Container, and then create Container.class. Enter the following code

    package com.my.Container;
    import java.util.ArrayList;
    import java.util.Random;
    public class Container {
        public static void main(String[] args) {
            ArrayList<String> names = new ArrayList<>();//创建一个ArrayList对象,指定这个对象存储String对象元素。
            names.add("我是一号");
            names.add("我是二号");//向容器中添加两个元素
            System.out.println(names);//查看容器有多少个元素
            names.remove(0); //删除下标位置是0的元素
            System.out.println(names);//查看容器有多少个元素
            names.set(0,"我是三号"); //修改位置是0 的元素,改为 明天
            System.out.println(names);//查看容器有多少元素
            names.clear();//清空容器的所有元素
            //循环添加10个元素
            Random random = new Random();
            for (int i = 0 ; i < 10 ;i++){
                names.add("我是第"+random.nextInt(50)+"名");
            }
            for(int i=0; i<names.size(); i++){                            //遍历容器中的元素,把所有元素都访问一遍
                System.out.println("第"+(i+1)+"个元素为:" + names.get(i));  // get方法,根据下标获取元素.
            }
        }
    }

    to run the code and experience the implementation of the add, delete, modify, and check operations of the container ArrayList.

    2. LinkedList

    1. Principle

    How to use Java ArrayList, LinkedList and HashMap

    2. Usage

    Create a new linkedlist.class and enter the following code

    package com.my.Container;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Random;
    public class linkedlist {
        public static void main(String[] args) {
            LinkedList<String> linkedNames = new LinkedList<>();//创建一个 LinkedList 对象 linkedNames,是用链表结构存储元素数据的
            linkedNames.add("我是1");
            linkedNames.add("我是2");
            linkedNames.add("我是3");//增加3个元素
            linkedNames.remove(1);//删除下标为1的元素
            linkedNames.set(1,"我是4");//修改下标为1的元素
            System.out.println(linkedNames);
            //linkedNames.clear();//清空容器的所有元素
            Random random2 = new Random();//循环添加10个元素
            for (int i = 0 ; i < 10 ;i++){
                linkedNames.add("我是"+random2.nextInt(100));
            }
            for(int i=0; i<linkedNames.size(); i++){ //遍历容器中的元素,把所有元素都访问一遍
                System.out.println("第"+(i+1)+"个元素为:" + linkedNames.get(i));  // get方法,根据下标获取元素
            }
        }
    }

    Run the code and experience the implementation of the add, delete, modify and query operations of the container ArrayList (almost the same as ArrayList).

    3. HashMap

    1. Principle

    How to use Java ArrayList, LinkedList and HashMap

    2. Usage

    Create a new hashmap.class and enter the following code

    package com.my.Container;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.Random;
    public class hashmap {
        public static void main(String[] args) {
            HashMap<String,String> mapNames = new HashMap<>();//创建一个 HashMap 对象 mapNames,是用链表结构存储元素数据的
            mapNames.put("NO1","我是1号");
            mapNames.put("NO2","我是2号");
            mapNames.put("NO3","我是3号"); //增加3个元素
            mapNames.remove("NO2");   //删除第二个元素
            mapNames.put("NO4","我是5号");  //修改第2个元素
            System.out.println(mapNames); //打印mapNames中所有元素
            mapNames.clear();//清空mapNames元素
            Random random3 = new Random();  //循环添加10个元素
            for (int i = 0 ; i < 10 ;i++){
                mapNames.put("NO"+i,"嫦娥"+random3.nextInt(100)+"号");
            }
            int i = 0;  //遍历容器中的元素,把所有元素都访问一遍
            for(String key :mapNames.keySet()){ //先获取key的集合,然后遍历每个key,
                i++; //定义i用来计数
                System.out.println("第"+(i+1)+"个元素为:"+ mapNames.get(key));  // get方法,根据key 获取元素
            }
        }
    }

    Run the above code to experience the addition, deletion, modification and query operations of HashMap.

    The running results are as follows:

    How to use Java ArrayList, LinkedList and HashMap

    4. Comparing the advantages, disadvantages and differences

    Here we mainly grasp the advantages and disadvantages of ArrayList and LinkedList, namely The difference between them

    1. The bottom layer of ArrayList is an array structure, and the bottom layer of LinkList is a linked list structure.

    2.LinkedList uses linked lists to store elements, which has high insertion and deletion efficiency, but low random reading efficiency.

    3.ArrayList uses arrays to store elements. Insertion and deletion efficiency is low, but random reading efficiency is high.

    It can be said: When the operation is to add data after a column of data instead of in the front or middle, and you need to randomly access the elements, using ArrayList will provide better performance; when your operation When you add or delete data in front or in the middle of a column of data, and access the elements in order, you should use LinkedList.

    ArrayList and LinkedList each have their own advantages and disadvantages in terms of performance, and each has its own application. Here are some performance comparisons summarized in the following table

    How to use Java ArrayList, LinkedList and HashMap

    The above is the detailed content of How to use Java ArrayList, LinkedList and HashMap. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete