首页 > 编程知识 正文

Java LinkedHashMap getOrDefault方法与示例,诈骗短信示例及辨别方法

时间:2023-05-05 15:31:55 阅读:197130 作者:1915

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。