How can I get Key from their respective Values

0 votes

If I have the value "Avengers", and a HashMap<String> hm for which hm.containsValue("Avengers") returns true.

How can I get the corresponding key? Do I have to loop through the hashmap? What is the best way to do this?

May 16, 2018 in Java by sharth
• 3,310 points
28 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
public class NewClass1 {

    public static void main(String[] args) {
       Map<Integer, String> testMaps = new HashMap<Integer, String>();
        testMaps.put(10, "a");
        testMaps.put(20, "b");
        testMaps.put(30, "c");
        testMaps.put(40, "d");
        for (Entry<Integer, String> entry : testMaps.entrySet()) {
            if (entry.getValue().equals("c")) {
                System.out.println(entry.getKey());
            }
        }
    }
}

Above method may not be good if your hashmap is really big. If your hashmap contain unique key to unique value mapping, you can maintain one more hashmap that contain mapping from Value to Key.

That is you have to maintain two hashmaps

1. Key to value 

2. Value to key

answered May 16, 2018 by parth295
• 4,600 points

Related Questions In Java

0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,840 points
582 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,520 points
3,958 views
0 votes
2 answers

How can I Copy files from one directory to another in Java

Java 8 Path sourcepath = Paths.get("C:\\data\\temp\\mydir"); ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,190 points
52 views
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
63 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
35 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
45 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
94 views
0 votes
1 answer

Sort a Map<Key, Value> by values

This might help solve your problem...!!!! public class ...READ MORE

answered Apr 18, 2018 in Java by Rishabh
• 3,520 points
38 views
0 votes
2 answers

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

Assuming TreeMap is not good for you ...READ MORE

answered Oct 10, 2018 in Java by Sushmita
• 6,840 points
51 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
112 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.