HashMap方法之Map.getOrDefault()解讀及案例
HashMap方法 Map.getOrDefault()解讀
HashMap getOrDefault(key, defaultValue) method in Java with Examples
The getOrDefault(Object key, V defaultValue) method of Map interface, implemented by HashMap class is used to get the value mapped with specified key. If no value is mapped with the provided key then the default value is returned.
Syntax:
default V getOrDefault(Object key, V defaultValue)
Parameters: This method accepts two parameters:
- key: which is the key of the element whose value has to be obtained.
- defaultValue: which is the default value that has to be returned, if no value is mapped with the specified key.
Return Value: This method returns value mapped with the specified key, otherwise default value is returned.
解釋
意思就是當(dāng)Map集合中有這個key時(shí),就使用這個key值,如果沒有就使用默認(rèn)值defaultValue
實(shí)例
Program 1:
// Java program to demonstrate? // getOrDefault(Object key, V defaultValue) method? import java.util.*;? public class GFG {? ?? ?// Main method? ?? ?public static void main(String[] args)? ?? ?{? ?? ??? ?// Create a HashMap and add some values? ?? ??? ?HashMap<String, Integer> map? ?? ??? ??? ?= new HashMap<>();? ?? ??? ?map.put("a", 100);? ?? ??? ?map.put("b", 200);? ?? ??? ?map.put("c", 300);? ?? ??? ?map.put("d", 400);? ?? ??? ?// print map details? ?? ??? ?System.out.println("HashMap: " ?? ??? ??? ??? ??? ??? ?+ map.toString());? ?? ??? ?// provide key whose value has to be obtained? ?? ??? ?// and default value for the key. Store the? ?? ??? ?// return value in k? ?? ??? ?int k = map.getOrDefault("b", 500);? ?? ??? ?// print the value of k returned by? ?? ??? ?// getOrDefault(Object key, V defaultValue) method? ?? ??? ?System.out.println("Returned Value: " + k);? ?? ?}? }?
Output:
HashMap: {a=100, b=200, c=300, d=400}
Returned Value: 200
Program 2:
// Java program to demonstrate? // getOrDefault(Object key, V defaultValue) method? import java.util.*;? public class GFG {? ?? ?// Main method? ?? ?public static void main(String[] args)? ?? ?{? ?? ??? ?// Create a HashMap and add some values? ?? ??? ?HashMap<String, Integer> map? ?? ??? ??? ?= new HashMap<>();? ?? ??? ?map.put("a", 100);? ?? ??? ?map.put("b", 200);? ?? ??? ?map.put("c", 300);? ?? ??? ?map.put("d", 400);? ?? ??? ?// print map details? ?? ??? ?System.out.println("HashMap: " ?? ??? ??? ??? ??? ??? ?+ map.toString());? ?? ??? ?// provide key whose value has to be obtained? ?? ??? ?// and default value for the key. Store the? ?? ??? ?// return value in k? ?? ??? ?int k = map.getOrDefault("y", 500);? ?? ??? ?// print the value of k returned by? ?? ??? ?// getOrDefault(Object key, V defaultValue) method? ?? ??? ?System.out.println("Returned Value: " + k);? ?? ?}? }?
Output:
HashMap: {a=100, b=200, c=300, d=400}
Returned Value: 500
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
idea下載svn的項(xiàng)目并且運(yùn)行操作
這篇文章主要介紹了idea下載svn的項(xiàng)目并且運(yùn)行操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09IntelliJ IDEA本地代碼提交到github網(wǎng)站不顯示與本地不同步問題的解決辦法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA本地代碼提交到github網(wǎng)站不顯示與本地不同步問題的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10詳解Java編程中static關(guān)鍵字和final關(guān)鍵字的使用
這篇文章主要介紹了詳解Java編程中static關(guān)鍵字和final關(guān)鍵字的使用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09java連接zookeeper實(shí)現(xiàn)zookeeper教程
這篇文章主要介紹了java連接zookeeper實(shí)現(xiàn)zookeeper教程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11