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時,就使用這個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)文章
JAVA?IDEA項(xiàng)目打包為jar包的步驟詳解
在Java開發(fā)中我們通常會將我們的項(xiàng)目打包成可執(zhí)行的Jar包,以便于在其他環(huán)境中部署和運(yùn)行,下面這篇文章主要給大家介紹了關(guān)于JAVA?IDEA項(xiàng)目打包為jar包的相關(guān)資料,需要的朋友可以參考下2024-08-08
教你Java中的Lock鎖底層AQS到底是如何實(shí)現(xiàn)的
本文是基于ReentrantLock來講解,ReentrantLock加鎖只是對AQS的api的調(diào)用,底層的鎖的狀態(tài)(state)和其他線程等待(Node雙向鏈表)的過程其實(shí)是由AQS來維護(hù)的,對Java?Lock鎖AQS實(shí)現(xiàn)過程感興趣的朋友一起看看吧2022-05-05
Spring中的BeanFactory與FactoryBean區(qū)別詳解
這篇文章主要介紹了Spring中的BeanFactory與FactoryBean區(qū)別詳解,BeanFactory是一個接口,它是spring中的一個工廠,FactoryBean也是一個接口,實(shí)現(xiàn)了3個方法,通過重寫其中方法自定義生成bean,需要的朋友可以參考下2024-01-01
spring-session簡介及實(shí)現(xiàn)原理源碼分析
這篇文章主要介紹了spring-session簡介及實(shí)現(xiàn)原理源碼分析,具有一定參考價值,需要的朋友可以了解下。2017-11-11
Java實(shí)現(xiàn)ATM銀行管理系統(tǒng)(控制臺版本)
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)控制臺版本的ATM銀行管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06

