Java - How to get an Integer associated with a String in a HashMap

jl00

I want to know how to get the Integer that is associated with the String in the HashMap people:

public HashMap<Integer, String> people;

public int getAssociatedInt(Person p){
return (The thing I do not know how to get);
}

I already tried people.get();, but I'm not exactly sure how to do that.

arshajii

You have a map that maps Integers to Strings, not the other way around. To find the (potentially multiple) Integer keys associated with a given String value, you can loop over the Maps entrySet():

for (Entry<Integer, String> e : people.entrySet())
    if (e.getValue().equals(value))
        return e.getKey();

However, the method above doesn't deal with multiple keys mapping to the same value. If you want to handle that, you can keep a list of target keys and add to the list instead of return in the body of the if-statement.

An alternate method would be to maintain a "reverse" map that maps the strings to the integers, and then simply query that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the two highest values in a HashMap<Integer,String>, making a ranking

From Java

How to sort a HashMap<String, Integer[]>?

From Dev

Sorting HashMap <Integer, List<String>> in Java

From Java

What is the equivalent of a Java HashMap<String,Integer> in Swift

From Java

how to easily sum two hashMap<String,Integer>?

From Dev

How do I get the key associated with the maximum value of a Rust HashMap?

From Java

How to convert String into Hashmap in java

From Java

Is Java method with a single argument to receive values of both HashMap<String, Integer> and HashMap<Long, Integer> possible?

From Dev

How to get ArrayList from Hashmap<String String>

From Dev

how to get last integer in a string?

From Java

Java HashMap<int[], Integer>

From Java

Java 8 Sort HashMap where map key is an object of <String, Integer>

From Dev

Cast HashMap<String, Integer> with HashMap<String, Double>

From Dev

get Java byte[] from HashMap<String , byte>

From Dev

how to convert String into Integer in java

From Java

How to reduce memory usage for a HashMap<String, Integer> like data structure

From Dev

How to split big HashMap<String, Integer> into smaller ones

From Dev

How to fill this HashMap Map<Integer, Map<String, Boolean>> pr

From Dev

How to get string and hashmap mixed value inside a hashmap

From Dev

Convert a HashMap<Integer, List<String>> to HashMap<String, HashSet<Integer>>

From Dev

How to get String key from HashMap?

From Dev

How to Get a specific string from HashMap

From Dev

How to get length of String[] array in a HashMap value

From Java

How to achieve a multi valued hashmap like HashMap<String,Hashmap<String,String>> in Java

From Java

Java get month string from integer

From Dev

Get Float OR integer value from the String in java

From Dev

Bash: How to search a string in file with Regex and get the associated value

From Dev

How to get a dictionary value in plan string associated with same key

From Dev

How to add increment values using a hashmap of <Integer,HashMap<Integer,Integer>

Related Related

  1. 1

    How to get the two highest values in a HashMap<Integer,String>, making a ranking

  2. 2

    How to sort a HashMap<String, Integer[]>?

  3. 3

    Sorting HashMap <Integer, List<String>> in Java

  4. 4

    What is the equivalent of a Java HashMap<String,Integer> in Swift

  5. 5

    how to easily sum two hashMap<String,Integer>?

  6. 6

    How do I get the key associated with the maximum value of a Rust HashMap?

  7. 7

    How to convert String into Hashmap in java

  8. 8

    Is Java method with a single argument to receive values of both HashMap<String, Integer> and HashMap<Long, Integer> possible?

  9. 9

    How to get ArrayList from Hashmap<String String>

  10. 10

    how to get last integer in a string?

  11. 11

    Java HashMap<int[], Integer>

  12. 12

    Java 8 Sort HashMap where map key is an object of <String, Integer>

  13. 13

    Cast HashMap<String, Integer> with HashMap<String, Double>

  14. 14

    get Java byte[] from HashMap<String , byte>

  15. 15

    how to convert String into Integer in java

  16. 16

    How to reduce memory usage for a HashMap<String, Integer> like data structure

  17. 17

    How to split big HashMap<String, Integer> into smaller ones

  18. 18

    How to fill this HashMap Map<Integer, Map<String, Boolean>> pr

  19. 19

    How to get string and hashmap mixed value inside a hashmap

  20. 20

    Convert a HashMap<Integer, List<String>> to HashMap<String, HashSet<Integer>>

  21. 21

    How to get String key from HashMap?

  22. 22

    How to Get a specific string from HashMap

  23. 23

    How to get length of String[] array in a HashMap value

  24. 24

    How to achieve a multi valued hashmap like HashMap<String,Hashmap<String,String>> in Java

  25. 25

    Java get month string from integer

  26. 26

    Get Float OR integer value from the String in java

  27. 27

    Bash: How to search a string in file with Regex and get the associated value

  28. 28

    How to get a dictionary value in plan string associated with same key

  29. 29

    How to add increment values using a hashmap of <Integer,HashMap<Integer,Integer>

HotTag

Archive