Retrieve LinkedHashMap elements

We know that LinkedHashMap is a key-value pair based data structure.

Sometimes, we need to retrieve all the keys of LinkedHashMap

Sometimes we need to retrieve value for the specified key from the LinkedHashMap

Sometimes we need to retrieve all the values from a LinkedHashMap

Let’s see how we can achieve all these retrievals

Case1:

Retrieve all the keys from LinkedHashMap

We can use keyset() method of LinkedHashMap to get all the keys in that Map.

Example

  1. import java.util.*;
  2. public class LinkedHashMapRetrieveKeysExample {
  3. public static void main(String args[]) {
  4.  
  5. LinkedHashMap<String,Integer> lhm = new LinkedHashMap<>();
  6. lhm.put(1, “One”);
  7. lhm.put(2, “Two”);
  8. lhm.put(3, “Three”);
  9. Set keys = lhm.keySet();
  10. System.out.println(“All the keys of LinkedHashMap….”);
  11. System.out.println(keys);
  12.  
  13. }
  14. }
import java.util.*;
public class LinkedHashMapRetrieveKeysExample {
public static void main(String args[]) {

LinkedHashMap<String,Integer> lhm = new LinkedHashMap<>();
lhm.put(1, “One”);
lhm.put(2, “Two”);
lhm.put(3, “Three”);
Set keys = lhm.keySet();
System.out.println(“All the keys of LinkedHashMap….”);
System.out.println(keys);

}
}


Case2 :

Retrieve all the values from LinkedHashMap

We can use values() method to retrieve all the values from the LinkedHashMap

Example

  1. import java.util.*;
  2. public class LinkedHashMapRetrieveAllValuesExample {
  3. public static void main(String args[]) {
  4.  
  5. LinkedHashMap<String,Integer>lhm = new LinkedHashMap<>();
  6. lhm.put(1, “One”);
  7. lhm.put(2, “Two”);
  8. lhm.put(3, “Three”);
  9. Collection values = lhm.values();
  10. System.out.println(“All the values of LinkedHashMap….”);
  11. System.out.println(values);
  12.  
  13. }
  14. }
import java.util.*;
public class LinkedHashMapRetrieveAllValuesExample {
public static void main(String args[]) {

LinkedHashMap<String,Integer>lhm = new LinkedHashMap<>();
lhm.put(1, “One”);
lhm.put(2, “Two”);
lhm.put(3, “Three”);
Collection values = lhm.values();
System.out.println(“All the values of LinkedHashMap….”);
System.out.println(values);

}
}


Case3 :

Retrieve value for the specified key from LinkedHashMap

We can also retrieve value for a specific key from the LinkedHashMap using get() method by passing a specific key.

Example

  1. import java.util.*;
  2. public class LinkedHashMapRetrieveValueForKeyExample {
  3. public static void main(String args[]) {
  4.  
  5. LinkedHashMap<String,Integer> lhm = new LinkedHashMap<>();
  6. lhm.put(1, “One”);
  7. lhm.put(2, “Two”);
  8. lhm.put(3, “Three”);
  9. String value = lhm.get(1);
  10. System.out.println(“Value for key 1….”);
  11. System.out.println(value);
  12.  
  13. }
  14. }
import java.util.*;
public class LinkedHashMapRetrieveValueForKeyExample {
public static void main(String args[]) {

LinkedHashMap<String,Integer> lhm = new LinkedHashMap<>();
lhm.put(1, “One”);
lhm.put(2, “Two”);
lhm.put(3, “Three”);
String value = lhm.get(1);
System.out.println(“Value for key 1….”);
System.out.println(value);

}
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on