寒假打卡09-1月22日

Java 容器概览——List、Set、Map、Queue 概述

在 Java 中,集合框架(Java Collections Framework,简称 JCF)提供了一组用于存储和操作数据的容器类。集合框架包括接口、实现类和算法,能够满足各种数据结构和算法的需求。在本篇文章中,我们将概述 Java 容器中的四种主要类型:ListSetMapQueue,并介绍它们的常用实现类和应用场景。

List

List 接口表示一个有序的元素集合,允许重复元素。它提供了按索引访问元素的方法,并且可以动态调整大小。List 接口有多个实现类,其中最常用的是 ArrayListLinkedList

ArrayList

ArrayList 是一个基于动态数组的数据结构,具有快速的随机访问速度。它适合用于读多写少的场景。

示例代码

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

public class ArrayListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}

LinkedList

LinkedList 是一个基于双向链表的数据结构,具有快速的插入和删除速度。它适合用于频繁插入和删除操作的场景。

示例代码

import java.util.LinkedList;
import java.util.List;

public class LinkedListDemo {
    public static void main(String[] args) {
        List<String> list = new LinkedList<>();
        list.add("Dog");
        list.add("Cat");
        list.add("Horse");

        for (String animal : list) {
            System.out.println(animal);
        }
    }
}

Set

Set 接口表示一个不包含重复元素的集合。它不保证元素的顺序。Set 接口有多个实现类,其中最常用的是 HashSetTreeSet

HashSet

HashSet 是一个基于哈希表的数据结构,具有快速的查找速度。它适合用于需要快速查找和去重的场景。

示例代码

import java.util.HashSet;
import java.util.Set;

public class HashSetDemo {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("Red");
        set.add("Green");
        set.add("Blue");

        for (String color : set) {
            System.out.println(color);
        }
    }
}

TreeSet

TreeSet 是一个基于红黑树的数据结构,具有自动排序的功能。它适合用于需要有序存储和快速查找的场景。

示例代码

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        Set<String> set = new TreeSet<>();
        set.add("Zebra");
        set.add("Elephant");
        set.add("Lion");

        for (String animal : set) {
            System.out.println(animal);
        }
    }
}

Map

Map 接口表示一个键值对的集合,每个键最多只能映射到一个值。Map 接口有多个实现类,其中最常用的是 HashMapTreeMap

HashMap

HashMap 是一个基于哈希表的数据结构,具有快速的查找和插入速度。它适合用于需要快速查找和存储键值对的场景。

示例代码

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

public class HashMapDemo {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("One", 1);
        map.put("Two", 2);
        map.put("Three", 3);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

TreeMap

TreeMap 是一个基于红黑树的数据结构,具有自动排序的功能。它适合用于需要有序存储和快速查找键值对的场景。

示例代码

import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        Map<String, Integer> map = new TreeMap<>();
        map.put("Banana", 2);
        map.put("Apple", 1);
        map.put("Cherry", 3);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Queue

Queue 接口表示一个先进先出(FIFO)的数据结构。它常用于实现队列、缓冲区等。Queue 接口有多个实现类,其中最常用的是 LinkedListPriorityQueue

LinkedList

LinkedList 既可以作为 List 的实现类,也可以作为 Queue 的实现类。它适合用于需要快速插入和删除操作的队列场景。

示例代码

import java.util.LinkedList;
import java.util.Queue;

public class LinkedListQueueDemo {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.offer("First");
        queue.offer("Second");
        queue.offer("Third");

        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}

PriorityQueue

PriorityQueue 是一个基于堆的数据结构,具有自动排序的功能。它适合用于需要优先级调度的队列场景。

示例代码

import java.util.PriorityQueue;
import java.util.Queue;

public class PriorityQueueDemo {
    public static void main(String[] args) {
        Queue<Integer> queue = new PriorityQueue<>();
        queue.offer(3);
        queue.offer(1);
        queue.offer(2);

        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}

总结

Java 集合框架提供了一组强大而灵活的容器类,包括 ListSetMapQueue。每种容器都有多个实现类,适用于不同的应用场景。通过合理选择和使用这些容器类,我们可以编写出更加高效和健壮的代码。

希望通过本篇文章,大家对 Java 容器有了更深入的了解。在接下来的文章中,我们将继续探讨更多关于 Java 集合框架的知识点,敬请期待!

posted @   aallofitisst  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2024-01-22 寒假算法学习03
点击右上角即可分享
微信分享提示