Copy Code code as follows:
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;
public class Mapvaluegetkey {
public static void Main (string[] args) {
Map map = new hashmap<> ();
Map.put (1, "A");
Map.put (2, "A");
Map.put (3, "A");
Map.put (4, "A");
Map.put (5, "A");
String value = "A";
ArrayList arr = valuegetkey (map, value);
if (!arr.isempty ()) {
for (int i=0; i<arr.size (); i++) {
System.out.println (Arr.get (i));
}
}
}
private static ArrayList Valuegetkey (Map map,string value) {
Set set = Map.entryset ();
ArrayList arr = new arraylist<> ();
Iterator it = Set.iterator ();
while (It.hasnext ()) {
Map.entry Entry = (map.entry) it.next ();
if (Entry.getvalue (). Equals (value)) {
int s = (int) entry.getkey ();
Arr.add (s);
}
}
return arr;
}
}
The results are as follows:
Copy Code code as follows: