杰瑞科技汇

Java中如何获取Class对象的列表?

Of course! Let's dive deep into the List interface in Java. It's one of the most fundamental and widely used data structures in the Java Collections Framework.

Java中如何获取Class对象的列表?-图1
(图片来源网络,侵删)

What is a List?

In simple terms, a List is an ordered collection (also known as a sequence). It allows you to store a group of elements where:

  1. Duplicates are allowed: You can have multiple elements that are equal.
  2. Order is preserved: Elements are stored in the order they are added. You can access elements by their integer position (index) in the list, starting from 0.

Think of a List like a dynamic array that can grow and shrink, or a shopping list where you can have multiple "milk" items and the order you wrote them down matters.


The List Interface Hierarchy

List is an interface in the java.util package. It extends the Collection interface, which in turn extends the Iterable interface.

java.lang.Object
  ↳ java.util.Collection<E>
    ↳ java.util.List<E>

Because List is an interface, you cannot create an instance of it directly. You must use one of its concrete implementations.

Java中如何获取Class对象的列表?-图2
(图片来源网络,侵删)

Common List Implementations

Java provides three primary implementations of the List interface, each with different performance characteristics and use cases.

Implementation Description Key Characteristics When to Use
ArrayList A resizable-array implementation of the List interface. - Fast Random Access: get(index) is very fast (O(1) time complexity).
- Slow Insertion/Deletion: Adding or removing elements in the middle of the list is slow (O(n) time complexity) because it may require shifting all subsequent elements.
- Generally more memory efficient than LinkedList.
Default choice. Use when you need fast access to elements by index and don't frequently insert or remove from the middle of the list.
LinkedList Doubly-linked list implementation of the List interface. - Fast Insertion/Deletion: Adding or removing elements at the beginning or middle is very fast (O(1) time complexity).
- Slow Random Access: get(index) is slow (O(n) time complexity) because it has to traverse the list from the head or tail.
- Implements Deque, Queue, and List interfaces.
Use when you need frequent additions or removals from anywhere in the list and don't need fast random access.
Vector A legacy class, similar to ArrayList but with synchronized methods. - Thread-Safe: All public methods are synchronized, making it safe for use in multi-threaded environments.
- Slower: The synchronization overhead makes it slower than ArrayList in single-threaded scenarios.
- Its growth is managed by a "capacity increment" factor.
Avoid in new code. If you need a thread-safe list, use CopyOnWriteArrayList or use Collections.synchronizedList(new ArrayList<>()). Vector is considered a legacy class.

How to Use List: Code Examples

Here’s a practical guide to using Lists with ArrayList, which is the most common implementation.

a. Creating a List

You typically declare the variable with the List interface type and instantiate it with a concrete class (ArrayList).

import java.util.ArrayList;
import java.util.List;
// The best practice is to program to the interface
List<String> fruits = new ArrayList<>();
// You can also specify an initial capacity (optional)
List<Integer> numbers = new ArrayList<>(20);

b. Adding Elements

Use the add() method. By default, elements are appended to the end of the list.

Java中如何获取Class对象的列表?-图3
(图片来源网络,侵删)
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// To add an element at a specific position
fruits.add(1, "Mango"); // Inserts "Mango" at index 1
System.out.println(fruits); // Output: [Apple, Mango, Banana, Orange]

c. Accessing Elements

Use get(index) to retrieve an element by its position.

String firstFruit = fruits.get(0); // "Apple"
String secondFruit = fruits.get(1); // "Mango"
System.out.println("The first fruit is: " + firstFruit);

d. Updating Elements

Use set(index, element) to replace the element at a specific position.

// Replace the element at index 2
fruits.set(2, "Strawberry");
System.out.println(fruits); // Output: [Apple, Mango, Strawberry, Orange]

e. Removing Elements

You can remove an element by its value or by its index.

// Remove by value (removes the first occurrence)
fruits.remove("Apple");
System.out.println(fruits); // Output: [Mango, Strawberry, Orange]
// Remove by index
String removedFruit = fruits.remove(1); // Removes "Strawberry"
System.out.println("Removed fruit: " + removedFruit); // Output: Removed fruit: Strawberry
System.out.println(fruits); // Output: [Mango, Orange]

f. Getting the Size of the List

Use the size() method. Do not use size() in a for-loop condition if you are modifying the list!

int numberOfFruits = fruits.size();
System.out.println("There are " + numberOfFruits + " fruits."); // Output: There are 2 fruits.

g. Iterating Over a List

There are several ways to loop through a List.

// 1. Enhanced For-Loop (most common)
System.out.println("--- Using For-Loop ---");
for (String fruit : fruits) {
    System.out.println(fruit);
}
// 2. Using an Iterator (safe for modification)
System.out.println("\n--- Using Iterator ---");
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext()) {
    String fruit = iterator.next();
    if ("Mango".equals(fruit)) {
        iterator.remove(); // Safely remove the current element
    }
}
System.out.println(fruits); // Output: [Orange]
// 3. Using Java 8 Streams (modern, functional approach)
System.out.println("\n--- Using Java 8 Streams ---");
fruits.add("Grape");
fruits.add("Kiwi");
fruits.stream().forEach(System.out::println);
// 4. Using a classic for-loop with index (useful if you need the index)
System.out.println("\n--- Using Index For-Loop ---");
for (int i = 0; i < fruits.size(); i++) {
    System.out.println("Fruit at index " + i + " is: " + fruits.get(i));
}

h. Checking for an Element

Use the contains() method.

boolean hasOrange = fruits.contains("Orange");
boolean hasApple = fruits.contains("Apple");
System.out.println("Does the list contain Orange? " + hasOrange); // true
System.out.println("Does the list contain Apple? " + hasApple);   // false

Key Methods of the List Interface

Method Description
add(E e) Appends the specified element to the end of this list.
add(int index, E element) Inserts the specified element at the specified position in this list.
get(int index) Returns the element at the specified position in this list.
set(int index, E element) Replaces the element at the specified position with the specified element.
remove(Object o) Removes the first occurrence of the specified element from this list.
remove(int index) Removes the element at the specified position in this list.
size() Returns the number of elements in this list.
isEmpty() Returns true if this list contains no elements.
contains(Object o) Returns true if this list contains the specified element.
clear() Removes all of the elements from this list.
indexOf(Object o) Returns the index of the first occurrence of the specified element.
lastIndexOf(Object o) Returns the index of the last occurrence of the specified element.
subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

List vs. Set vs. Queue

It's crucial to understand the difference between the main collection interfaces.

Feature List Set Queue
Order Ordered (by insertion) Unordered (usually, but can be sorted) FIFO (First-In, First-Out)
Duplicates Allowed Not Allowed Allowed
Purpose A sequence of elements where order and position matter. A collection of unique elements, like a mathematical set. A collection designed for holding elements prior to processing.
Common Implementations ArrayList, LinkedList HashSet, LinkedHashSet, TreeSet LinkedList, PriorityQueue
Example Use Case A list of students in a class, a history of commands. A list of unique user IDs, a deck of cards (no duplicates). A line of people at a ticket counter, a task scheduler.

Summary

  • List is an ordered, duplicate-friendly collection.
  • Use the ArrayList implementation for most general-purpose cases where fast random access is needed.
  • Use LinkedList when you need frequent insertions and deletions.
  • Always declare your list variable with the List interface type (e.g., List<String>) for better flexibility and code reusability.
分享:
扫描分享到社交APP
上一篇
下一篇