hashmap如何根据value值查找对应的key值

  • Post author:
  • Post category:其他


import java.util.HashMap;
import java.util.Map;
public class Main{
    public static void main(String[] args) {
        HashMap<Integer,Integer> map=new HashMap<>();
        //key---value
        map.put(1,2);
        map.put(2,3);
        map.put(3,4);
        map.put(4,5);
        for (Map.Entry<Integer,Integer> entry:map.entrySet()){
            if (entry.getValue().equals(3)){
                //查找value等于3所对应的key值
                System.out.println("value:"+ 3 + "所对应的key值为:"+entry.getKey());
            }
            else if (entry.getValue().equals(2)) {
                //查找value等于2所对应的key值
                System.out.println("value:"+ 2 + "所对应的key值为:" + entry.getKey());
            }

        }
    }
}

在这里插入图片描述