Java 基础教学:高级特性与实战-集合框架

Java 集合框架提供了一套性能优良、使用方便的接口和类,用于存储和操作群组数据。最常用的集合接口有 ListSetMap

List

List 接口可以存储一系列有序的元素,并且可以包含重复的元素。List 的实现类常用的有 ArrayListLinkedList

ArrayList

ArrayListList 接口的大小可变数组的实现。它允许随机快速访问元素。

示例:使用 ArrayList
java 复制代码
import java.util.ArrayList;
import java.util.List;

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

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

LinkedList

LinkedListList 接口的链表实现。相对于 ArrayList,它在列表中间插入或删除元素时更高效。

示例:使用 LinkedList
java 复制代码
import java.util.LinkedList;
import java.util.List;

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

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

Set

Set 接口定义了一个不能包含重复元素的集合。Set 的实现类常用的有 HashSetTreeSet

HashSet

HashSet 是基于 HashMap 实现的,它不保证集合的迭代顺序;特别是,它不保证该顺序恒久不变。

示例:使用 HashSet
java 复制代码
import java.util.HashSet;
import java.util.Set;

public class SetExample {
    public static void main(String[] args) {
        Set<String> cities = new HashSet<>();
        cities.add("New York");
        cities.add("London");
        cities.add("Paris");
        cities.add("New York");

        for (String city : cities) {
            System.out.println(city);
        }
    }
}

TreeSet

TreeSet 是基于 TreeMap 实现的,元素被排序存储。

示例:使用 TreeSet
java 复制代码
import java.util.Set;
import java.util.TreeSet;

public class TreeSetExample {
    public static void main(String[] args) {
        Set<String> treeSet = new TreeSet<>();
        treeSet.add("Orange");
        treeSet.add("Apple");
        treeSet.add("Banana");

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

Map

Map 接口定义了一个从键映射到值的对象。Map 不能包含重复的键;每个键最多只能映射到一个值。Map 的实现类常用的有 HashMapTreeMap

HashMap

HashMap 是基于哈希表的 Map 接口实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。

示例:使用 HashMap
java 复制代码
import java.util.HashMap;
import java.util.Map;

public class MapExample {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Alice", 30);
        map.put("Bob", 25);
        map.put("Charlie", 35);

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

TreeMap

TreeMap 是基于红黑树的 NavigableMap 实现。此类保证映射按照键的升序排序。

示例:使用 TreeMap
java 复制代码
import java.util.Map;
import java.util.TreeMap;

public class TreeMapExample {
    public static void main(String[] args) {
        Map<String, Integer> treeMap = new TreeMap<>();
        treeMap.put("Alice", 30);
        treeMap.put("Charlie", 35);
        treeMap.put("Bob", 25);

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

总结

Java 集合框架提供了一组丰富的类和接口,用于在应用程序中高效地管理数据集合。ListSetMap 是最基本的集合类型,每种类型都有不同的特点和用途。了解何时使用哪种类型的集合以及如何正确地使用它们,对于编写高效、易于维护的 Java 程序至关重要。

相关推荐
科比不来it16 分钟前
Go语言数据竞争Data Race 问题怎么检测?怎么解决?
开发语言·c++·golang
biter down18 分钟前
c语言14:字符指针
c语言·开发语言
SimonKing26 分钟前
Mybatis-Plus的竞争对手来了,试试 MyBatis-Flex
java·后端·程序员
光军oi32 分钟前
JAVA全栈JVM篇————初识JVM
java·开发语言·jvm
我命由我1234537 分钟前
PDFBox - PDFBox 加载 PDF 异常清单(数据为 null、数据为空、数据异常、文件为 null、文件不存在、文件异常)
java·服务器·后端·java-ee·pdf·intellij-idea·intellij idea
7哥♡ۣۖᝰꫛꫀꪝۣℋ44 分钟前
Spring Boot
java·spring boot·后端
Moniane1 小时前
C++深度解析:从核心特性到现代编程实践
java·开发语言·jvm
uxiang_blog1 小时前
C++进阶:重载类型转换
linux·开发语言·c++
攻城狮CSU1 小时前
C# 数据加载专题 之泛型序列化
java·servlet·c#
浩泽学编程1 小时前
【源码深度 第1篇】LinkedList:双向链表的设计与实现
java·数据结构·后端·链表·jdk