Java中EnumMap的put(key,value)方法: put(key,value)

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Java中的Java.util.EnumMap.put(key,value)方法用于关联指定的键值对。在这种情况下,如果重复这些值,则替换旧值。

句法:

Enum_Map.put(key, value)

使用的参数:该方法有两个参数。

  • key - 与值关联的指定键。
  • value - 它是与指定键关联的值。

返回值:该函数返回与指定键关联的旧值。

下面的程序说明了put(key,value)方法的工作原理

Java.util.EnumMap.put(key,value)程序1:

// Java program to demonstrate keySet()
import java.util.*;

// An enum of geeksforgeeks
public enum gfg {
    Global_today,
    India_today,
    China
}
;

class Enum_demo {
    public static void main(String[] args)
    {

        EnumMap<gfg, Integer> mp = new
                 EnumMap<gfg, Integer>(gfg.class);

        // Values are associated
        mp.put(gfg.Global_today, 799);
        mp.put(gfg.India_today, 69);

       // Display the initial map
       System.out.println("The map is: " + mp);

        // Stores the old value associated with the key
        int prev_value = mp.put(gfg.India_today, 72);

        // Prints the old value
        System.out.println("Previous value: " + prev_value);

       // Display the final map
       System.out.println("The final map is: " + mp);
    }
}

输出:

The map is: {Global_today=799, India_today=69}
Previous value: 69
The final map is: {Global_today=799, India_today=72}

Java.util.EnumMap.put(key,value)程序2:

// Java program to demonstrate the working of keySet()
import java.util.*;

// an enum of geeksforgeeks
// ranking globally and in india
public enum gfg {
    Global_today,
    India_today,
    China_today
}
;

class Enum_demo {
    public static void main(String[] args)
    {

        EnumMap<gfg, Integer> mp = new EnumMap<gfg, Integer>(gfg.class);

        // Values are associated
        mp.put(gfg.Global_today, 799);
        mp.put(gfg.India_today, 69);

       // Display the initial map
       System.out.println("The map is: " + mp);

        // Stores the old value associated with the key
        int prev_value = mp.put(gfg.Global_today, 800);

        // Prints the old value
        System.out.println("Previous value: " + prev_value);

       // Display the final map
       System.out.println("The final map is: " + mp);
    }
}

输出:

The map is: {Global_today=799, India_today=69}
Previous value: 799
The final map is: {Global_today=800, India_today=69}
  •   本文标题:Java中EnumMap的put(key,value)方法: put(key,value) - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/5457.html
      微信返利机器人
      免费:淘宝,京东,拼多多优惠券
      腾讯,爱奇艺,优酷的VIP视频免费解析,免费看
      即刻扫描二维码,添加微信机器人!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注