Skip to content
Related Articles

Related Articles

Convert ArrayList to LinkedHashMap in Java
  • Last Updated : 28 Dec, 2020

LinkedHashMap is a predefined class in Java that is similar to HashMap, contain key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved.

We need to convert ArrayList to LinkedHashMap the Key value of LinkedHashMap will be an index of ArrayList, basically, LinkedHashMap will also be the same as ArrayList in terms of iteration and storing data.

Example :

Input ArrayList :  { 5, 6, 7, 8, 4 } 

Output LinkedHashMap :

Index  |  Value
  1    |    5
  2    |    6
  3    |    7
  4    |    8
  5    |    4

We can convert ArrayList to LinkedHashMap in 2 ways:

  1. Using the for-each loop by traversing through the whole ArrayList and push each element to LinkedHashMap.
  2. Using streams in java 8 version.

Approach: Using the for-each loop



  • Taking input in ArrayList.
  • Using a For/While loop to push value in LinkedHashMap(Key of LinkedHashMap will be an index of ArrayList assuming that the first index is 1)

Pseudocode :
 

while (i < l1.size()) {

            l.put(i + 1, l1.get(i));
            i++; 
}

Here, "l1" is ArrayList and "l" is LinkedHashMap.

Java




// Java program to convert ArrayList
// to LinkedHashMap
  
import java.util.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        LinkedHashMap<Integer, Integer> l = new LinkedHashMap<>();
  
        ArrayList<Integer> l1 = new ArrayList<>();
        l1.add(5);
        l1.add(6);
        l1.add(7);
        l1.add(8);
        l1.add(4);
  
        int i = 0;
       
        // Adding one by one the elements 
        // of ArrayList to LinkedHashMap
        while (i < l1.size()) 
        {
  
            l.put(i + 1, l1.get(i));
            i++;
        }
        
        System.out.println("Key  |  Value");
        
        // .entrySet() method gives us the list of all 
        // mappings reference of the map
        for (Map.Entry<Integer, Integer> it :l.entrySet()) {
            
            System.out.println(" " + it.getKey() + "   |  "
                               + it.getValue());
            
        }
    }
}
Output
Key  |  Value
 1   |  5
 2   |  6
 3   |  7
 4   |  8
 5   |  4

Time Complexity: O(n)

Approach 2: Using streams

If you are using Java 8 or above there is another method, you can use the stream to convert List to LinkedHashMap object. The Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods that can be pipelined to produce the desired result.

Example:

Java




// Java program to convert ArrayList to LinkedHashMap
  
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
class integer {
  
    private Integer id;
    private String name;
  
    public integer(Integer id, String name)
    {
        this.id = id;
        this.name = name;
    }
  
    public Integer getId() { return this.id; }
    
    // the .toString method of Object class
    // will be called by default when the object
    // of integer class will be made
    public String toString()
    {
        return "[" + this.id + "=>" + this.name + "]";
    }
}
  
class GFG {
    public static void main(String[] args)
    {
  
        List<integer> List = new ArrayList<integer>();
  
        List.add(new integer(1, "will"));
        List.add(new integer(2, "mike"));       
        List.add(new integer(3, "luke"));
        List.add(new integer(4, "dustin"));
  
        System.out.println("ArrayList contains: " + List);
  
        // stream Api is used here to convert the 
        // List to LinkedHashMap
        Map<Integer, integer> HashMap = List.stream().collect(Collectors.toMap(
                       integer::getId, integer -> integer));
  
        System.out.println("Map contains: " + HashMap); 
    }
}
Output
ArrayList contains: [[1=>will], [2=>mike], [3=>luke], [4=>dustin]]
Map contains: {1=[1=>will], 2=[2=>mike], 3=[3=>luke], 4=[4=>dustin]}

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :