杰瑞科技汇

Java8 List转List如何高效实现?

Of course! The phrase "Java 8 List to List" can mean a few different things. I'll cover the most common and useful interpretations, from simple copying to powerful transformations using Streams.

Java8 List转List如何高效实现?-图1
(图片来源网络,侵删)

Here are the key ways to convert or work with lists in Java 8:

  1. Simple Copying: Creating a new, independent list from an existing one.
  2. Filtering: Creating a new list containing only elements that match a certain condition.
  3. Mapping (Transformation): Creating a new list by transforming each element of the original list.
  4. Chaining Operations: Combining filtering and mapping in a single, expressive operation.

Simple Copying (Creating a New List)

This is the most straightforward "list to list" operation. You want a new list that contains all the same elements as the original, but changes to the new list won't affect the original.

a) Using the Constructor (Simple & Direct)

This is the most common and readable way to create a shallow copy.

import java.util.Arrays;
import java.util.List;
public class ListCopyExample {
    public static void main(String[] args) {
        List<String> originalList = Arrays.asList("Apple", "Banana", "Cherry");
        // Create a new list containing all elements from the original list
        List<String> copiedList = new ArrayList<>(originalList);
        System.out.println("Original List: " + originalList);
        System.out.println("Copied List:  " + copiedList);
        // Prove they are independent
        copiedList.add("Date");
        System.out.println("\nAfter adding 'Date' to copied list:");
        System.out.println("Original List: " + originalList); // Unchanged
        System.out.println("Copied List:  " + copiedList);   // Changed
    }
}

Output:

Java8 List转List如何高效实现?-图2
(图片来源网络,侵删)
Original List: [Apple, Banana, Cherry]
Copied List:  [Apple, Banana, Cherry]
After adding 'Date' to copied list:
Original List: [Apple, Banana, Cherry]
Copied List:  [Apple, Banana, Cherry, Date]

b) Using Stream.collect() (More Verbose for Simple Copy)

While overkill for a simple copy, this is the foundation for more complex transformations.

import java.util.List;
import java.util.stream.Collectors;
public class StreamCopyExample {
    public static void main(String[] args) {
        List<Integer> originalList = List.of(1, 2, 3, 4, 5);
        // Stream the original list and collect it into a new list
        List<Integer> copiedList = originalList.stream()
                                              .collect(Collectors.toList());
        System.out.println("Original List: " + originalList);
        System.out.println("Copied List:  " + copiedList);
    }
}

Filtering a List

This is one of the most powerful features of Java 8 Streams. You create a new list containing only the elements that satisfy a given predicate (a condition).

import java.util.List;
import java.util.stream.Collectors;
public class ListFilterExample {
    public static void main(String[] args) {
        List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        // Filter the list to keep only even numbers
        List<Integer> evenNumbers = numbers.stream()
                                           .filter(number -> number % 2 == 0)
                                           .collect(Collectors.toList());
        System.out.println("Original Numbers: " + numbers);
        System.out.println("Even Numbers:    " + evenNumbers);
    }
}

Output:

Original Numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Even Numbers:    [2, 4, 6, 8, 10]

Explanation:

Java8 List转List如何高效实现?-图3
(图片来源网络,侵删)
  • numbers.stream(): Creates a stream of integers from the list.
  • .filter(number -> number % 2 == 0): This is an intermediate operation. It keeps only the elements for which the lambda expression number % 2 == 0 returns true.
  • .collect(Collectors.toList()): This is a terminal operation. It gathers the elements from the stream into a new List.

Mapping (Transforming) a List

Here, you create a new list by applying a function to each element of the original list. The elements in the new list can be of a different type.

import java.util.List;
import java.util.stream.Collectors;
public class ListMapExample {
    public static void main(String[] args) {
        List<String> fruitNames = List.of("apple", "banana", "cherry");
        // Map each string to its uppercase version
        List<String> upperCaseFruits = fruitNames.stream()
                                                 .map(String::toUpperCase) // Method reference
                                                 .collect(Collectors.toList());
        // Map each string to its length
        List<Integer> fruitLengths = fruitNames.stream()
                                                .map(String::length)
                                                .collect(Collectors.toList());
        System.out.println("Original Fruit Names: " + fruitNames);
        System.out.println("Uppercase Fruit Names: " + upperCaseFruits);
        System.out.println("Fruit Name Lengths: " + fruitLengths);
    }
}

Output:

Original Fruit Names: [apple, banana, cherry]
Uppercase Fruit Names: [APPLE, BANANA, CHERRY]
Fruit Name Lengths: [5, 6, 6]

Explanation:

  • .map(String::toUpperCase): This is an intermediate operation. The map function applies the given function (in this case, the toUpperCase method of the String class) to each element in the stream.
  • String::toUpperCase is a "method reference," which is a shorthand for the lambda fruit -> fruit.toUpperCase(). It makes the code cleaner.

Chaining Operations (Filter then Map)

This is where the real power of the Stream API shines. You can chain multiple operations together in a single, readable line of code.

Scenario: Get the names of all employees in the "Engineering" department, but only return their names in uppercase.

import java.util.List;
import java.util.stream.Collectors;
class Employee {
    private String name;
    private String department;
    public Employee(String name, String department) {
        this.name = name;
        this.department = department;
    }
    public String getName() { return name; }
    public String getDepartment() { return department; }
    @Override
    public String toString() {
        return "Employee{name='" + name + "', department='" + department + "'}";
    }
}
public class ListChainExample {
    public static void main(String[] args) {
        List<Employee> employees = List.of(
            new Employee("Alice", "Engineering"),
            new Employee("Bob", "HR"),
            new Employee("Charlie", "Engineering"),
            new Employee("David", "Marketing")
        );
        // 1. Filter for Engineering department
        // 2. Map each employee to their name
        // 3. Convert the name to uppercase
        // 4. Collect into a new list
        List<String> engineeringNamesInUpperCase = employees.stream()
            .filter(employee -> "Engineering".equals(employee.getDepartment())) // Filter
            .map(Employee::getName)                                         // Map
            .map(String::toUpperCase)                                       // Map again
            .collect(Collectors.toList());                                   // Collect
        System.out.println("All Employees: " + employees);
        System.out.println("Engineering Dept (Uppercase): " + engineeringNamesInUpperCase);
    }
}

Output:

All Employees: [Employee{name='Alice', department='Engineering'}, Employee{name='Bob', department='HR'}, Employee{name='Charlie', department='Engineering'}, Employee{name='David', department='Marketing'}]
Engineering Dept (Uppercase): [ALICE, CHARLIE]

Summary Table

Goal Method / Code Snippet Key Stream API Method
Simple Copy List<String> newList = new ArrayList<>(oldList); Constructor
Filter (Create a subset) list.stream().filter(e -> e > 10).collect(Collectors.toList()); filter()
Map (Transform elements) list.stream().map(String::length).collect(Collectors.toList()); map()
Chain (Filter & Map) list.stream().filter(...).map(...).collect(Collectors.toList()); filter(), map()
Modify Original List (Not a copy) list.removeIf(e -> e < 5); removeIf()

By mastering these Stream operations, you can handle almost any "list to list" transformation in a clean, functional, and highly readable way.

分享:
扫描分享到社交APP
上一篇
下一篇