JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to get entry set from TreeMap?


Description:

Below example shows how to get all key-value pair as Entry objects. Entry class provides getter methods to access key-value details. The method entrySet() provides all entries as set object.


Code:
package com.java2novice.treemap;

import java.util.Set;
import java.util.Map.Entry;
import java.util.TreeMap;

public class MyTreeMapEntrySet {
	
	public static void main(String a[]){
		TreeMap<String, String> hm = new TreeMap<String, String>();
		//add key-value pair to TreeMap
		hm.put("first", "FIRST INSERTED");
		hm.put("second", "SECOND INSERTED");
		hm.put("third","THIRD INSERTED");
		System.out.println(hm);
		//getting value for the given key from TreeMap
		Set<Entry<String, String>> entires = hm.entrySet();
		for(Entry<String,String> ent:entires){
			System.out.println(ent.getKey()+" ==> "+ent.getValue());
		}
	}
}

Output:
{first=FIRST INSERTED, second=SECOND INSERTED, third=THIRD INSERTED}
first ==> FIRST INSERTED
second ==> SECOND INSERTED
third ==> THIRD INSERTED
<< Previous Program | Next Program >>

List Of All TreeMap Sample Programs:

  1. Basic TreeMap Operations.
  2. How to iterate through TreeMap?
  3. How to copy Map content to another TreeMap?
  4. How to search a key in TreeMap?
  5. How to search a value in TreeMap?
  6. How to get all keys from TreeMap?
  7. How to get entry set from TreeMap?
  8. How to delete all elements from TreeMap?
  9. How to sort keys in TreeMap by using Comparator?
  10. How to sort keys in TreeMap by using Comparator with user define objects?
  11. How to get sorted sub-map from TreeMap?
  12. How to get first key element from TreeMap (Sorted Map)?
  13. How to get last key element from TreeMap (Sorted Map)?
  14. How to reverse sorted keys in a TreeMap?
Java2Novice - YouTube Channel
  1. Python Tutorial for Beginners
Knowledge Centre
What is adapter class?
An adapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2019 by Nataraja Gootooru. All Rights Reserved.