Convert HashMap to LinkedList in Java
Last Updated :
21 Feb, 2022
HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values.
LinkedList is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node.
Example:
Input:
l.put(2, 5);
l.put(3, 6);
l.put(4, 1);
l.put(8, 2);
Output:
LinkedList of key-> [2, 3, 4, 8]
LinkedList of values-> [5, 6, 1, 2]
respectively the output for key and values.
Syntax of keySet() Method
hash_map.keySet()
Parameters: The method does not take any parameters.
Return Value: The method returns a set having the keys of the hash map.
Syntax of values() Method
Hash_Map.values()
Parameters: The method does not accept any parameters.
Return Value: The method is used to return a collection view containing all the values of the map.
PseudoCode
List<Integer> list = new LinkedList<>(l.keySet());
List<Integer> listOfValue = new LinkedList<>(l.values());
Example 1:
Java
// Java program to Convert HashMap to LinkedList
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// create a hashmap instance
HashMap<Integer, Integer> l = new HashMap<>();
// add mappings
l.put(2, 5);
l.put(3, 6);
l.put(4, 1);
l.put(8, 2);
// list of keys
List<Integer> list = new LinkedList<>(l.keySet());
// list of values
List<Integer> listOfValue
= new LinkedList<>(l.values());
// print the list
System.out.println("LinkedList of key-> " + list);
System.out.println("LinkedList of values-> "
+ listOfValue);
}
}
OutputLinkedList of key-> [2, 3, 4, 8]
LinkedList of values-> [5, 6, 1, 2]
Example 2:
Java
// Java program to Convert HashMap to LinkedList
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// create a hashmap instance
HashMap<Integer, String> l = new HashMap<>();
// add mappings
l.put(1, "Geeks");
l.put(4, "For");
l.put(3, "Geeks");
// list of keys
List<Integer> list = new LinkedList<>(l.keySet());
// list of values
List<String> listOfValue
= new LinkedList<>(l.values());
// print the list
System.out.println("LinkedList of key-> " + list);
System.out.println("LinkedList of values-> "
+ listOfValue);
}
}
OutputLinkedList of key-> [1, 3, 4]
LinkedList of values-> [Geeks, Geeks, For]
Similar Reads
Convert a HashSet to a LinkedList in Java In Java, HashSet and LinkedList are linear data structures. These are majorly used in many cases. There may be some cases where we need to convert HashSet to LinkedList. So, in this article, we will see how to convert HashSet to LinkedList in Java. Java Program to Convert a HashSet to a LinkedList T
2 min read
Convert Array to LinkedList in Java Array is contiguous memory allocation while LinkedList is a block of elements randomly placed in the memory which are linked together where a block is holding the address of another block in memory. Sometimes as per requirement or because of space issues in memory where there are bigger chunks of co
3 min read
How to Convert LinkedHashMap to List in Java? LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayLis
2 min read
How to Implement Generic LinkedList in Java? Linked List is Linear Data Structures that store values in nodes. As we do know here each Node possesses two properties namely the value of the node and link to the next node if present so. Linked List can not only be of Integer data type but String, boolean, Float, Character, etc. We can implement
8 min read
Program to Convert HashMap to TreeMap in Java HashMap is a part of Javaâs collection since Java 1.2. It provides the basic implementation of Map interface of Java which stores the data in (Key, Value) pairs. To access a value in HashMap, one must know its key. HashMap is known as HashMap because it uses a technique Hashing for storage of data.
6 min read