Retrieving TreeMap elements

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

Sometimes, We need to retrieve all the keys of TreeMap

Sometimes, We need to retrieve value for the specified key from the TreeMap

Sometimes we need to retrieve all the values from a TreeMap

Let’s see how we can achieve all these retrievals

Case 1

Retrieve all the keys from TreeMap

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

Example

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

TreeMap<Integer,String> tm = new TreeMap<>();
tm.put(3, “Three”);
tm.put(1, “One”);
tm.put(2, “Two”);

Set<Integer> keys = tm.keySet();
System.out.println(“All the keys of TreeMap....”);
System.out.println(keys);

}
}


Case 2

Retrieve all the values from TreeMap

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

Example

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

TreeMap<Integer,String> tm = new TreeMap<>();
tm.put(3, “Three”);
tm.put(1, “One”);
tm.put(2, “Two”);

Collection values = tm.values();
System.out.println(“All the values of TreeMap....”);
System.out.println(values);

}
}


Case 3

Retrieve value for the specified key from TreeMap

We can use get() method by passing a specific key to retrieve value for a specific key from the TreeMap using .

Example

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

TreeMap<Integer,String> tm  = new TreeMap<>();
tm.put(3, “Three”);
tm.put(1, “One”);
tm.put(2, “Two”);

String value = tm.get(1);
System.out.println(“Value for key 1….”);
System.out.println(value);

}
}

About the Author

Karibasappa G C (KB)
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