Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

How can I use sortedmap interface in Java? Can someone tell me an example

1 Answer

0 votes
by (13.1k points)

To implement a sortedmap you have to use TreeMap.

Here is an example for this:

Map<Integer, String> map = new TreeMap<Integer, String>(); 

 map.put(1, "Delhi");

 map.put(2, "Mumbai");

 map.put(3, "Bangalore");

for (Map.Entry<Integer, String> e : map.entrySet())

 {

 System.out.println(e.getKey() + " => " + e.getValue());

 }

Want to learn Java? Check out this core Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 20, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 16, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...