Write a Java Program to Sort map by keys

Java Program to Sort map by keys

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Sort map by keys
Java Program to Sort map by keys

Write a Java Program to Sort map by keys

PROGRAM: Java Program to Sort map by keys

/*Java Program to Sort map by keys*/

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

class Main {
public static void main(String[] args) {

// create a hashmap
Map<String, String> languages = new HashMap<>();
languages.put(“pos3”, “JS”);
languages.put(“pos1”, “Java”);
languages.put(“pos2”, “Python”);
System.out.println(“Map: ” + languages);

// create a tree map from the map
TreeMap<String, String> sortedNumbers = new TreeMap<>(languages);
System.out.println(“Map with sorted Key” + sortedNumbers);

}
}



Leave a Reply

Your email address will not be published. Required fields are marked *