How can I sort values of a Map in Java using its key

0 votes

I have a Map in java that has strings for key as well as value.

Data is like following: <"Ram", "1">, <"Johny", "1">, <"Humber", "4">

I want to sort the map based on its keys. So, In the end, I will have Johny, Humber, Ram

Eventually, I am trying to get two strings out of this Map. First String: in alphabetical order and Second String: Answers (in the same order as the question).

Right now I have the following:

Iterator itr = paramMap.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry pair = (Map.Entry)itr.next();
 questionAnswers += pair.getKey()+",";
}

This gets me the questions in a string but they are not in order.

May 16, 2018 in Java by sharth
• 3,370 points
1,194 views

2 answers to this question.

0 votes

Use a TreeMap. This is precisely what it is used for.

If this map is passed to you and you cannot determine the type, then you can do the following:

SortedSet<String> keys = new TreeSet<>(maps.keySet());
for (String key : keys) { 
   String value = maps.get(key);
   // do something
}

This will iterate across the map in natural order of the keys.

answered May 16, 2018 by Parth
• 4,640 points
0 votes

Assuming TreeMap is not good for you (and assuming you can't use generics):

List sortedKeys=new ArrayList(yourMap.keySet());
Collections.sort(sortedKeys);
// Do what you need with sortedKeys.
answered Oct 10, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,299 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,915 views
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
854 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,096 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,920 points
1,094 views
0 votes
1 answer

How can I get Key from their respective Values

public class NewClass1 { ...READ MORE

answered May 16, 2018 in Java by Parth
• 4,640 points
666 views
0 votes
1 answer

Sort a HashMap in Java

Convert hashmap to an ArrayList with a ...READ MORE

answered Jul 13, 2018 in Java by Daisy
• 8,140 points
656 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
5,278 views
0 votes
1 answer

How can I read a large text file line by line using Java?

// Open the file FileInputStream file = new ...READ MORE

answered May 2, 2018 in Java by Parth
• 4,640 points
890 views
0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by Parth
• 4,640 points
7,500 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP