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

Convert ArrayList to LinkedHashMap in Java

Linda Hamilton
Release: 2025-02-07 11:16:10
Original
225 people have browsed it

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.

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);
    }
}
Copy after login

Approach 2: Using Java Streams

This approach leverages Java Streams for a more concise solution.

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);
    }
}
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Articles by Author
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template