请问hashmap中想用值找键该怎么找?
请问hashmap中想用值找键该怎么找?
------解决方案--------------------
Set<Entry> entries = map.entry();
for(Entry entry : entries) {
if(youObject.equals(entry.getValue())) {
entry.getKey();//可能会找到多个
}
}
------解决方案--------------------
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashTest {
/**
* @param args
*/
public static void main(String[] args) {
String val = String.valueOf(1000);
Map<Integer, String> map = new HashMap<Integer, String>();
for (int i = 0; i < 100; i++) {
map.put(Integer.valueOf(i), String.valueOf(i * 100));
}
Set<Entry<Integer, String>> entries = map.entrySet();
for (Entry entry : entries) {
if (val.equals(entry.getValue())) {
System.out.println(entry.getKey());
}
}
}
}