Home >Java >javaTutorial >Convert ArrayList to LinkedHashMap in Java

Convert ArrayList to LinkedHashMap in Java

Linda Hamilton
Linda HamiltonOriginal
2025-02-07 11:16:10194browse

Convert ArrayList to LinkedHashMap in Java

A LinkedHashMap in Java maintains the insertion order of elements, unlike a regular HashMap. Converting an ArrayList to a LinkedHashMap requires assigning keys to each ArrayList element. The simplest approach uses the ArrayList index as the key. Iteration and sorting behavior are similar between ArrayLists and LinkedHashMaps.

Here's a breakdown of the conversion process, along with Java code examples illustrating different approaches:

Algorithm:

  1. Initialization: Create an empty LinkedHashMap.
  2. Iteration: Iterate through the ArrayList.
  3. Key-Value Pair Creation: For each element in the ArrayList, use its index as the key and the element itself as the value.
  4. Insertion: Add the key-value pair to the LinkedHashMap.
  5. Return: Return the populated LinkedHashMap.

Java Code Examples:

Approach 1: Using a loop

This is the most straightforward approach.

<code class="language-java">import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ArrayListToLinkedHashMap {

    public static <t> LinkedHashMap<integer t> convert(List<t> arrayList) {
        LinkedHashMap<integer t> linkedHashMap = new LinkedHashMap<>();
        for (int i = 0; i < arrayList.size(); i++) {
            linkedHashMap.put(i + 1, arrayList.get(i)); //Index +1 as key
        }
        return linkedHashMap;
    }

    public static void main(String[] args) {
        List<integer> arrayList = new ArrayList<>(List.of(5, 16, 7, 1997, 2001));
        LinkedHashMap<integer integer> linkedHashMap = convert(arrayList);
        System.out.println("LinkedHashMap: " + linkedHashMap);
    }
}</integer></integer></integer></t></integer></t></code>

Approach 2: Using Java Streams

This approach leverages Java Streams for a more concise solution.

<code class="language-java">import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ArrayListToLinkedHashMapStreams {

    public static <t> LinkedHashMap<integer t> convert(List<t> arrayList) {
        return IntStream.range(0, arrayList.size())
                .boxed()
                .collect(Collectors.toMap(i -> i + 1, arrayList::get, (a, b) -> a, LinkedHashMap::new));
    }

    public static void main(String[] args) {
        List<integer> arrayList = new ArrayList<>(List.of(5, 16, 7, 1997, 2001));
        LinkedHashMap<integer integer> linkedHashMap = convert(arrayList);
        System.out.println("LinkedHashMap: " + linkedHashMap);
    }
}</integer></integer></t></integer></t></code>

Both approaches achieve the same result, converting an ArrayList to a LinkedHashMap where the keys are the indices (starting from 1) and the values are the elements from the ArrayList. Choose the approach that best suits your coding style and project requirements. The Streams approach is generally considered more elegant and potentially more efficient for larger lists. Remember to handle potential exceptions (like NullPointerException) if your ArrayList might contain null values.

The above is the detailed content of Convert ArrayList to LinkedHashMap in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn