Java Dictionary put()方法与示例

字典类的put()方法 (Dictionary Class put() method)

  • put() method is available in java.util package.

    put()方法在java.util包中可用。

  • put() method is used to put the given key to the given value in this dictionary.

    put()方法用于将给定键放入此字典中的给定值。

  • put() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.

    put()方法是一个非静态方法,可通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • put() method may throw an exception at the time of returning value associated with the key.

    put()方法在返回与键关联的值时可能会引发异常。

    NullPointerException: This exception may throw when the given any of the parameters is null exists.

    NullPointerException :当给定的任何参数为null时,可能引发此异常。

Syntax:

句法:

    public abstract Value put(Key k, Value val);

Parameter(s):

参数:

  • Key k – represents the key value(k).

    密钥k –代表密钥值(k)。

  • Value val – represents the value(val).

    值val –表示值(值)。

Return value:

返回值:

The return type of this method is Value, it gets value associated with the given key(k) otherwise it returns null when the key did not hold any value.

此方法的返回类型为Value ,它获取与给定键(k)关联的值 ,否则当键不包含任何值时返回null。

Example:

例:

// Java program is to demonstrate the example of
// put(Key k, Value val) method of Dictionary

import java.util.*;

public class PutOfDictionary {
    public static void main(String[] args) {
        // Instantiate a Hashtable object
        Dictionary dictionary = new Hashtable();

        // By using put() method is to
        // add an elements in Hashtable 
        dictionary.put(1, "C");
        dictionary.put(2, "C++");
        dictionary.put(3, "JAVA");
        dictionary.put(4, "PHP");
        dictionary.put(5, "SFDC");

        //Display dictionary
        System.out.println("dictionary: " + dictionary);

        // Here we are replacing the value exists
        // on the given key element 3
        dictionary.put(3, "Microservices");

        // Display Modified Dictionary
        System.out.println("dictionary: " + dictionary);
    }
}

Output

输出量

dictionary: {5=SFDC, 4=PHP, 3=JAVA, 2=C++, 1=C}
dictionary: {5=SFDC, 4=PHP, 3=Microservices, 2=C++, 1=C}


翻译自: https://www.includehelp.com/java/dictionary-put-method-with-example.aspx

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值