• Tags
  •         
  • www.breakyizhan.com
  •    

    Java中的Java.util.EnumMap.containsValue(value)方法用于确定映射的一个或多个键是否与给定值相关联。它将值作为参数,如果该值由EnumMap中的任何键映射,则返回True。

    句法:

    boolean containsValue(Object value)

    参数:该方法接受一个参数,该值指的是要通过任何键检查其映射的值。

    返回值:当一个或多个键映射到相同值时返回true。

    下面的程序说明了Java.util.EnumMap.containsValue()方法

    Java.util.EnumMap.containsValue(value)程序1:

    // Java program to demonsrate containsValue() method
    import java.util.*;
    
    // An enum of gfg ranking is created
    public enum rank_countries {
        India,
        United_States,
        China,
        Japan,
        Canada,
        Russia
    };
    
    class Enum_map {
        public static void main(String[] args)
        {
    
            EnumMap<rank_countries, Integer> mp = new
            EnumMap<rank_countries,Integer>(rank_countries.class);
    
            // values are associated in mp
            mp.put(rank_countries.India, 72);
            mp.put(rank_countries.United_States, 1083);
            mp.put(rank_countries.China, 4632);
            mp.put(rank_countries.Japan, 6797);
            mp.put(rank_countries.Canada, 1820);
    
            // check if map contains mapping at specified key
            boolean ans = mp.containsValue(72);
    
            // prints the result
            System.out.println("Map contains 72: " + ans);
        }
    }
    

    输出:

    Map contains 72: true

    Java.util.EnumMap.containsValue(value)程序2:

    // Java program to demonsrate containsValue() method
    import java.util.*;
    
    // An enum of gfg ranking is created
    public enum rank_countries {
        India,
        United_States,
        China,
        Japan,
        Canada,
        Russia
    };
    
    class Enum_map {
        public static void main(String[] args)
        {
    
            EnumMap<rank_countries, Integer> mp = new
            EnumMap<rank_countries,Integer>(rank_countries.class);
    
            // values are associated in mp
            mp.put(rank_countries.India, 72);
            mp.put(rank_countries.United_States, 1083);
            mp.put(rank_countries.China, 4632);
            mp.put(rank_countries.Japan, 6797);
            mp.put(rank_countries.Canada, 1820);
    
            // check if map contains mapping at specified key
            boolean ans = mp.containsValue(2000);
    
            // prints the result
            System.out.println("Map contains 2000: " + ans);
        }
    }
    

    输出:

    Map contains 2000: false

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5454.html