字典的put()方法用于在字典中插入映射。这意味着可以将特定键及其值映射到特定字典中。

用法:

DICTIONARY.put(key, value)

参数:该方法有两个参数,都属于Dictionary的Object类型。

key:这是指需要插入字典以进行映射的关键元素。

value:这是指以上键将映射到的值。

返回值:该方法返回键所映射到的值。如果键未映射到任何值,则返回NULL。

以下程序用于说明java.util.Dictionary.put()方法的用法:

示例1:

// Java code to illustrate the put() method

import java.util.*;

public class Dictionary_Demo {

public static void main(String[] args)

{

// Creating an empty Dictionary

Dictionary dict

= new Hashtable();

// Inserting values into the Dictionary

dict.put(10, "Geeks");

dict.put(15, "4");

dict.put(20, "Geeks");

dict.put(25, "Welcomes");

dict.put(30, "You");

// Displaying the Dictionary

System.out.println("Initial Dictionary is: " + dict);

// Inserting existing key along with new value

String returned_value = (String)dict.put(20, "All");

// Verifying the returned value

System.out.println("Returned value is: " + returned_value);

// Displaying the new table

System.out.println("New Dictionary is: " + dict);

}

}

输出:

Initial Dictionary is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}

Returned value is: Geeks

New Dictionary is: {10=Geeks, 20=All, 30=You, 15=4, 25=Welcomes}

示例2:

// Java code to illustrate the put() method

import java.util.*;

public class Dictionary_Demo {

public static void main(String[] args)

{

// Creating an empty Dictionary

Dictionary dict

= new Hashtable();

// Inserting values into the Dictionary

dict.put(10, "Geeks");

dict.put(15, "4");

dict.put(20, "Geeks");

dict.put(25, "Welcomes");

dict.put(30, "You");

// Displaying the Dictionary

System.out.println("Initial Dictionary is: " + dict);

// Inserting existing key along with new value

String returned_value = (String)dict.put(50, "All");

// Verifying the returned value

System.out.println("Returned value is: " + returned_value);

// Displaying the new table

System.out.println("New Dictionary is: " + dict);

}

}

输出:

Initial Dictionary is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}

Returned value is: null

New Dictionary is: {10=Geeks, 20=Geeks, 30=You, 50=All, 15=4, 25=Welcomes}

java dictionary 实例化_Java Dictionary put()用法及代码示例相关推荐

  1. java math.sin()_Java Math sin()用法及代码示例

    java.lang.Math.sin()返回介于0.0和pi之间的角度的三角正弦.如果参数为NaN或无穷大,则结果为NaN.如果自变量为零,则结果为零,其符号与自变量相同.返回的值将在-1和1之间. ...

  2. java range对象_Java LocalTime range()用法及代码示例

    LocalTime类的range()方法用于获取最小值和最大值形式的字段范围,并将该字段作为参数传递给此方法.此方法的返回值是该字段的ValueRange对象,并且该方法仅对LocalTime对象支持 ...

  3. java short字段_Java Field setShort()用法及代码示例

    java.lang.reflect.Field的setShort()方法用于将字段的值设置为指定对象的缩写.当您需要将对象的字段值设置为短时,可以使用此方法在对象上设置值. 用法: public vo ...

  4. java range(10)_Java Year range()用法及代码示例

    Year类的range()方法用于获取最大值和最小值的字段范围,并将该字段作为参数传递给此方法.此方法的返回值是该字段的ValueRange对象,并且该方法仅对Year对象支持的那些字段返回Value ...

  5. java short 写法_Java Short longValue()用法及代码示例

    Short类的java.lang.Short.longValue()方法是Java中的内置方法,用于将Short对象的值返回为long. 用法 ShortObject.longValue() 返回值: ...

  6. java scanner字符串_Java Scanner toString()用法及代码示例

    java.util.Scanner类的toString()方法返回此Scanner的字符串表示形式.未指定确切的格式. 用法: public String toString() 返回值:此函数返回此扫 ...

  7. java compareandset 包_Java AtomicReferenceArray compareAndSet()用法及代码示例

    如果AtomicReferenceArray对象的索引i处的当前值等于期望值,则使用AtomicReferenceArray类的compareAndSet()方法原子地将AtomicReference ...

  8. java boolean例子_Java Field setBoolean()用法及代码示例

    java.lang.reflect.Field的setBoolean()方法用于将字段的值设置为指定对象上的布尔值.当需要将对象的字段的值设置为布尔值时,可以使用此方法在对象上设置值. 用法: pub ...

  9. java 中negate()_Java BigInteger negate()用法及代码示例

    先决条件:BigInteger基础 的java.math.BigInteger.negate()方法返回一个BigInteger,其值为(-this). negate()方法将更改BigInteger ...

  10. java numberformat 方法_Java NumberFormat parse()用法及代码示例

    parse(str)方法是java.text.NumberFormat的内置方法,该方法从给定字符串的开头解析文本以产生数字.该方法可能不使用给定字符串的整个文本 用法: public Number ...

最新文章

  1. Linux那些事儿 之 戏说USB(24)设备的生命线(七)
  2. RouteHttpMap要添加的引用
  3. ue4材质节点大全_转载于知乎:UE4 多象限(UDIM)贴图在UE4中的应用
  4. 乐高泰坦机器人视频解说_“安防”机器人将亮相服贸会
  5. 是圆的问题呢(洛谷P1652题题解,Java语言描述)
  6. 选项不属于HTML语言特点,Web前端开发初级模拟测试卷一(2)
  7. 数位 dp 最低位最高位之差绝对值大于2_面试必备——手撕代码(2)“买卖股票的最佳时机”...
  8. linux unzip命令
  9. 毕业设计系列1--基于ESP8266设计智能插座--材料清单
  10. Win10 远程桌面黑屏问题
  11. springboot定时器 + Javamail + 心知天气API (通过邮件每天给女朋友发送一句情话+当日天气信息+当日生活指数)
  12. dolphinscheduler 2.0.4 PIGEON任务使用的一次尝试
  13. 剑指Offer 46.把数字翻译成字符串(Python)
  14. Scrape Center爬虫平台之spa8案例
  15. Unity3D之调用WinRT组件
  16. 永恒之蓝漏洞复现测试
  17. oracle数据库中spool的作用,Oracle中Spool命令如何使用 Oracle中Spool命令使用方法
  18. brew安装报错:fatal: not in a git directory Error: Command failed with exit 128: git
  19. jmeter+csv+ant接口自动化测试--设计jmeter脚本(一)
  20. OCR(Optical Character Recognition,光学字符识别)技术详解

热门文章

  1. ubuntu source
  2. 最优控制理论 一、变分法和泛函极值问题
  3. windows下安装使用couchdb
  4. 按键精灵文字识别插件_按键精灵——如何实现办公自由(二)
  5. aspx ttf文件加载不出来_加载页面信息,刷不出来心态都崩了
  6. 扎心!“我学了六个月 Python,怎么还是会找不到工作”
  7. 【iOS-cocos2d-X 游戏开发之十三】cocos2dx通过Jni调用Android的Java层代码(下)
  8. 版本控制 Git RPM打包
  9. ASP实现记住密码的功能
  10. 地图相关应用系统部署到现场报错原因汇总