博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Dictionary put()方法与示例
阅读量:2545 次
发布时间:2019-05-11

本文共 2065 字,大约阅读时间需要 6 分钟。

字典类的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 Dictionaryimport 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}

翻译自:

转载地址:http://ljvzd.baihongyu.com/

你可能感兴趣的文章
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
交换机划分Vlan配置
查看>>
yum安装Elasticsearch5.x
查看>>
正则表达式
查看>>
Python模块_json & pickle模块
查看>>
Python 模块之_os模块_os是与操作系统交互的接口
查看>>
通通玩blend美工(1)——荧光Button
查看>>
[UWP]了解模板化控件(8):ItemsControl
查看>>