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,310 points
50 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 parth295
• 4,600 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,840 points

Related Questions In Java

0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,310 points
57 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,190 points
50 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,310 points
44 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,040 points
204 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,840 points
31 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 parth295
• 4,600 points
28 views
0 votes
1 answer

Sort a HashMap in Java

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

answered Jul 12, 2018 in Java by Daisy
• 8,040 points
44 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,190 points
90 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 parth295
• 4,600 points
26 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 parth295
• 4,600 points
106 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.