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

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

LinkedHashMap类的getOrDefault()方法 (LinkedHashMap Class getOrDefault() method)

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

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

  • getOrDefault() method is used to get the value associated with the given key element when it exists otherwise it gets the default value for the given key element when no previous value associated with the given key.

    getOrDefault()方法用于获取与给定键元素关联的值(如果存在),否则,当没有先前与给定键关联的值时,它将获取给定键元素的默认值。

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

    getOrDefault()方法是一个非静态方法,只能由类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • getOrDefault() method does not throw an exception at the time of getting the value element.

    getOrDefault()方法在获取value元素时不会引发异常。

Syntax:

句法:

public getOrDefault(Object key_ele, Value def_val);

Parameter(s):

参数:

  • Object key_ele – represents the key element (key_ele) to which the associated value is to be retrieved.

    对象key_ele –表示要将关联值检索到的键元素(key_ele)。

  • Value def_val – represents the default value (def_val) is to be retrieved when no previous value exist for the given key element.

    值def_val –表示在给定键元素没有先前值的情况下将检索默认值(def_val)。

Return value:

返回值:

The return type of the method is Value, it returns the linked value for the given key element if exists otherwise it returns the default value (def_val).

该方法的返回类型为Value ,如果存在则返回给定键元素的链接值,否则返回默认值(def_val)。

Example:

例:

// Java program to demonstrate the example // of getOrDefault(Object key_ele, Value def_val)// method of LinkedHashMap import java.util.*;public class GetOrDefaultOfLinkedHashMap {
public static void main(String[] args) {
// Instantiates a LinkedHashMap object Map < Integer, String > map = new LinkedHashMap < Integer, String > (); // By using put() method is to add // key-value pairs in a LinkedHashMap map.put(10, "C"); map.put(20, "C++"); map.put(50, "JAVA"); map.put(40, "PHP"); map.put(30, "SFDC"); // Display LinkedHashMap System.out.println("LinkedHashMap: " + map); // By using getOrDefault() method is to // return the value associated for the // given key element if exists otherwise // it returns the default value Object val_ele = map.getOrDefault(50, "Microservices"); //Display val_ele System.out.print("map.getOrDefault(50,Microservices): "); System.out.println(val_ele); }}

Output

输出量

LinkedHashMap: {10=C, 20=C++, 50=JAVA, 40=PHP, 30=SFDC}map.getOrDefault(50,Microservices): JAVA

翻译自:

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

你可能感兴趣的文章
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>