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,320 points
32 views

1 answer to this question.

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,850 points
590 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,540 points
5,378 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
65 views
0 votes
1 answer

How do I get distinct element from list in java?

import java.util.*; public class SetE { ...READ MORE

answered Aug 7 in Java by sampriti
• 1,100 points
17 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,850 points
42 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
52 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
129 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,540 points
48 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,850 points
56 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
258 views