java dictionary 实例化_Java Dictionary put()用法及代码示例
字典的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()用法及代码示例相关推荐
- java math.sin()_Java Math sin()用法及代码示例
java.lang.Math.sin()返回介于0.0和pi之间的角度的三角正弦.如果参数为NaN或无穷大,则结果为NaN.如果自变量为零,则结果为零,其符号与自变量相同.返回的值将在-1和1之间. ...
- java range对象_Java LocalTime range()用法及代码示例
LocalTime类的range()方法用于获取最小值和最大值形式的字段范围,并将该字段作为参数传递给此方法.此方法的返回值是该字段的ValueRange对象,并且该方法仅对LocalTime对象支持 ...
- java short字段_Java Field setShort()用法及代码示例
java.lang.reflect.Field的setShort()方法用于将字段的值设置为指定对象的缩写.当您需要将对象的字段值设置为短时,可以使用此方法在对象上设置值. 用法: public vo ...
- java range(10)_Java Year range()用法及代码示例
Year类的range()方法用于获取最大值和最小值的字段范围,并将该字段作为参数传递给此方法.此方法的返回值是该字段的ValueRange对象,并且该方法仅对Year对象支持的那些字段返回Value ...
- java short 写法_Java Short longValue()用法及代码示例
Short类的java.lang.Short.longValue()方法是Java中的内置方法,用于将Short对象的值返回为long. 用法 ShortObject.longValue() 返回值: ...
- java scanner字符串_Java Scanner toString()用法及代码示例
java.util.Scanner类的toString()方法返回此Scanner的字符串表示形式.未指定确切的格式. 用法: public String toString() 返回值:此函数返回此扫 ...
- java compareandset 包_Java AtomicReferenceArray compareAndSet()用法及代码示例
如果AtomicReferenceArray对象的索引i处的当前值等于期望值,则使用AtomicReferenceArray类的compareAndSet()方法原子地将AtomicReference ...
- java boolean例子_Java Field setBoolean()用法及代码示例
java.lang.reflect.Field的setBoolean()方法用于将字段的值设置为指定对象上的布尔值.当需要将对象的字段的值设置为布尔值时,可以使用此方法在对象上设置值. 用法: pub ...
- java 中negate()_Java BigInteger negate()用法及代码示例
先决条件:BigInteger基础 的java.math.BigInteger.negate()方法返回一个BigInteger,其值为(-this). negate()方法将更改BigInteger ...
- java numberformat 方法_Java NumberFormat parse()用法及代码示例
parse(str)方法是java.text.NumberFormat的内置方法,该方法从给定字符串的开头解析文本以产生数字.该方法可能不使用给定字符串的整个文本 用法: public Number ...
最新文章
- Linux那些事儿 之 戏说USB(24)设备的生命线(七)
- RouteHttpMap要添加的引用
- ue4材质节点大全_转载于知乎:UE4 多象限(UDIM)贴图在UE4中的应用
- 乐高泰坦机器人视频解说_“安防”机器人将亮相服贸会
- 是圆的问题呢(洛谷P1652题题解,Java语言描述)
- 选项不属于HTML语言特点,Web前端开发初级模拟测试卷一(2)
- 数位 dp 最低位最高位之差绝对值大于2_面试必备——手撕代码(2)“买卖股票的最佳时机”...
- linux unzip命令
- 毕业设计系列1--基于ESP8266设计智能插座--材料清单
- Win10 远程桌面黑屏问题
- springboot定时器 + Javamail + 心知天气API (通过邮件每天给女朋友发送一句情话+当日天气信息+当日生活指数)
- dolphinscheduler 2.0.4 PIGEON任务使用的一次尝试
- 剑指Offer 46.把数字翻译成字符串(Python)
- Scrape Center爬虫平台之spa8案例
- Unity3D之调用WinRT组件
- 永恒之蓝漏洞复现测试
- oracle数据库中spool的作用,Oracle中Spool命令如何使用 Oracle中Spool命令使用方法
- brew安装报错:fatal: not in a git directory Error: Command failed with exit 128: git
- jmeter+csv+ant接口自动化测试--设计jmeter脚本(一)
- OCR(Optical Character Recognition,光学字符识别)技术详解