日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java LinkedHashMap getOrDefault()方法与示例

發布時間:2023/12/1 java 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 objectMap < Integer, String > map = new LinkedHashMap < Integer, String > ();// By using put() method is to add// key-value pairs in a LinkedHashMapmap.put(10, "C");map.put(20, "C++");map.put(50, "JAVA");map.put(40, "PHP");map.put(30, "SFDC");// Display LinkedHashMapSystem.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 valueObject val_ele = map.getOrDefault(50, "Microservices");//Display val_eleSystem.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

總結

以上是生活随笔為你收集整理的Java LinkedHashMap getOrDefault()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。