java: collection

Introduction

A collection is a group of data, a data structure that stores multiple elements.
The storage structure of the collection is divided into two types:

  • Sequence structure
  • Chain structure

Sequential storage

Putting the elements in the collection in a certain area in turn is called a sequential structure, and the space allocated in the memory is continuous.
Features: High access efficiency, low insertion or deletion efficiency.
Insert picture description here

Chain storage

The space allocated in the memory can be discontinuous, divided into:

  • One-way chain storage
  • Two-way chain storage

Features: High insert and delete efficiency, low access efficiency
Insert picture description here
Insert picture description here

Collection classes and interfaces

Collection related APIs are in the java.util package
Insert picture description here

Collection

It is an interface, and the main ones inherited from other interfaces are List and Set

List

ArrayList

import java.util.ArrayList;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        // 访问元素
        sites.get(1); // 访问第二个元素
        // 修改元素
        sites.set(2, "Wiki"); // 第一个参数为索引位置,第二个为要修改的值
        // 删除元素
        sites.remove(3); // 删除第四个元素
        // 计算大小
        sites.size();
        // 迭代数组列表
        for (int i = 0; i < sites.size(); i++) {
    
    
            System.out.println(sites.get(i));
        }
        for (String i : sites) {
    
    
            System.out.println(i);
        }
        ...
        ...
        ...
    }
}
// [Google, Runoob, Taobao, Weibo]

See for details

Vector

It is old and not recommended.
Advantages: thread safety

LinkedList

When you need to insert or delete frequently, use LinkedList, two-way chain storage

// 引入 LinkedList 类
import java.util.LinkedList; 

LinkedList<E> list = new LinkedList<E>();   // 普通创建方法
或者
LinkedList<E> list = new LinkedList(Collection<? extends E> c); // 使用集合创建链表
import java.util.LinkedList;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        // 使用 addFirst() 在头部添加元素
        sites.addFirst("Wiki");
        // 使用 addLast() 在尾部添加元素
        sites.addLast("Wiki");
        // 使用 removeFirst() 移除头部元素
        sites.removeFirst();
        // 使用 removeLast() 移除尾部元素
        sites.removeLast();
        // 使用 getFirst() 获取头部元素
        sites.getFirst()
        // 使用 getLast() 获取尾部元素
        sites.getLast()
        // 迭代元素
        for (int size = sites.size(), i = 0; i < size; i++) {
    
    
            System.out.println(sites.get(i));
        }
        for (String i : sites) {
    
    
            System.out.println(i);
        }
        System.out.println(sites);
    }
}

Set

  • Unordered (the order of the elements has nothing to do with the order in which they are placed), and the elements cannot be accessed by index.
  • Non-repeatable (repetitive elements cannot appear in the set).

HashSet

// 引入 HashSet 类      
import java.util.HashSet;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    
    HashSet<String> sites = new HashSet<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");
        sites.add("Runoob");  // 重复的元素不会被添加
        // 判断元素是否存在
        sites.contains("Taobao")
        sites.remove("Taobao");  // 删除元素,删除成功返回 true,否则为 false
        // 计算大小
        sites.size()
        // 迭代
        for (String i : sites) {
    
    
            System.out.println(i);
        }
        System.out.println(sites);
    }
}

TreeSet

The elements will be sorted, such as from small to large

// 引入 TreeSet 类      
import java.util.TreeSet;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    
    TreeSet<String> sites = new TreeSet<String>();
        sites.add(2);
        sites.add(5);
        sites.add(1);
        sites.add(3);
        sites.add(4);
        System.out.println(sites);
    }
}
// [1, 2, 3, 4, 5]

Sort by:
1. Natural order
2. Provide a comparator

Map

What it stores is a key-value mapping.

HashMap

// 引入 HashMap 类      
import java.util.HashMap;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    
        // 创建 HashMap 对象 Sites
        HashMap<Integer, String> Sites = new HashMap<Integer, String>();
        // 添加键值对
        Sites.put(1, "Google");
        Sites.put(2, "Runoob");
        Sites.put(3, "Taobao");
        Sites.put(4, "Zhihu");
        // 访问元素
        Sites.get(3);
        // 删除元素
        Sites.remove(4);
        // 删除所有键值对
        Sites.clear();
        // 计算大小
        Sites.size();
        // 输出 key 和 value
        for (Integer i : Sites.keySet()) {
    
    
            System.out.println("key: " + i + " value: " + Sites.get(i));
        }
        // 返回所有 value 值
        for(String value: Sites.values()) {
    
    
          // 输出每一个value
          System.out.print(value + ", ");
        }
        System.out.println(Sites);
    }
}

Hashtable

Synchronous, thread safe (not commonly used)

Iterator

Java Iterator (iterator) is not a collection, it is a method for accessing a collection, it can be used to iterate collections such as ArrayList and HashSet.

The two basic operations of iterator it are next, hasNext, and remove.

  • Calling it.next() will return the next element of the iterator and update the state of the iterator.
  • Call it.hasNext() to check if there are any elements in the collection.
  • Call it.remove() to delete the elements returned by the iterator.
// 引入 ArrayList 和 Iterator 类
import java.util.ArrayList;
import java.util.Iterator;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    

        // 创建集合
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");

        // 获取迭代器
        Iterator<String> it = sites.iterator();

        // 输出集合中的第一个元素
        System.out.println(it.next());
    }
}

Output all elements in the collection sites:

// 引入 ArrayList 和 Iterator 类
import java.util.ArrayList;
import java.util.Iterator;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    

        // 创建集合
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");

        // 获取迭代器
        Iterator<String> it = sites.iterator();

        // 输出集合中的所有元素
        while(it.hasNext()) {
    
    
            System.out.println(it.next());
        }
    }
}

Delete element

// 引入 ArrayList 和 Iterator 类
import java.util.ArrayList;
import java.util.Iterator;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        numbers.add(12);
        numbers.add(8);
        numbers.add(2);
        numbers.add(23);
        Iterator<Integer> it = numbers.iterator();
        while(it.hasNext()) {
    
    
            Integer i = it.next();
            if(i < 10) {
    
      
                it.remove();  // 删除小于 10 的元素
            }
        }
        System.out.println(numbers);
    }
}

Collections tools

Tools for operating collections

import java.util.ArrayList;

public class RunoobTest {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        // 访问元素
        Collections.addAll(sites, "123", "哈哈哈")
        // 最大值
        Collections.max(sites)
        // 翻转
        // 替换
        // 交换
        // 初始化
    }
}
// [Google, Runoob, Taobao, Weibo]

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/114487947
Recommended