Java LinkedHashMap getOrDefault()方法与示例

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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LinkedHashMap的getOrDefault方法是用来获取指定键映射到的值,如果该键不存在,则返回默认值。\[1\]这个方法接受两个参数,第一个参数是要获取值的键,第二个参数是默认值。如果指定的键存在于LinkedHashMap中,则返回该键对应的值;如果指定的键不存在,则返回默认值。\[2\]这个方法LinkedHashMap中的时间复杂度是O(1)。\[1\] 举个例子,假设有一个LinkedHashMap对象,其中包含键值对:{A=1, B=2, C=3}。如果我们使用getOrDefault方法来获取键D对应的值,默认值为0,那么返回的值就是0,因为键D不存在于LinkedHashMap中。\[1\] 另外,LinkedHashMap还有一个forEach方法,用于对每个键值对执行给定的操作。\[3\]这个方法接受一个BiConsumer函数式接口作为参数,用于定义要执行的操作。在这个例子中,我们可以使用forEach方法来遍历LinkedHashMap中的每个键值对,并打印出键和值。\[3\] #### 引用[.reference_title] - *1* *3* [LinkedHashMap用法详解](https://blog.csdn.net/jpfice/article/details/129153492)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Java LinkedHashMap getOrDefault()方法示例](https://blog.csdn.net/cumtb2002/article/details/107766639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值