getOrDefault: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - " \|([ps][0-9]+) *= " to " |$1= ")
m (Text replacement - "(\|[pr][0-9]+ *= *[^-]+) *- *C([a-eg-z])" to "$1 - c$2")
 
(3 intermediate revisions by 3 users not shown)
Line 8: Line 8:
|descr= Retrieves a value out of a [[HashMap]] after looking it up by its key. When the HashMap doesn't contain the key, the default value is returned
|descr= Retrieves a value out of a [[HashMap]] after looking it up by its key. When the HashMap doesn't contain the key, the default value is returned


|s1= hashMap [[getOrDefault]] [key, defaultValue]
|s1= hashMap [[getOrDefault]] [key, defaultValue, setDefault]


|p1= hashMap : [[HashMap]]
|p1= hashMap: [[HashMap]]


|p2= key: [[HashMapKey]] - Case sensitive key
|p2= key: [[HashMapKey]] - case sensitive key


|p3= defaultValue: [[Anything]] - (Optional, default [[nil]])
|p3= defaultValue: [[Anything]] - (Optional, default [[nil]])


|p4= setDefault: [[Boolean]] - (Optional, default [[false]]) - If set to [[true]] and value does not exist in provided hash map, '''defaultValue''' will be added to the hash map
|p4= setDefault: [[Boolean]] - (Optional, default [[false]]) if set to [[true]] and value does not exist in provided hash map, ''defaultValue'' will be added to the hash map
|p4since= arma3 2.04
|p4since= arma3 2.04


|r1= [[Anything]]
|r1= [[Anything]]


|x1= <code>[[private]] _myValue = _myHashMap [[getOrDefault]] ["a", "Not Found!"];</code>
|x1= <sqf>private _myValue = _myHashMap getOrDefault ["a", "Not Found!"];</sqf>


|x2= <code>[[private]] _myValue = _hashMap [[getOrDefault]] ["a", "Not Found!", [[true]]]; {{cc|Will add default value to the hash map if it does not exist}}</code>
|x2= <sqf>private _myValue = _hashMap getOrDefault ["a", "Not Found!", true]; // will add default value to the hash map if it does not exist</sqf>


|seealso= [[HashMap]] [[get]]
|x3= <sqf>
private _array = _hashmap getOrDefault [_key, [], true]; // a new array will be created if it doesn't exist
_array pushBack _item; // _item will be added to the array (the one in the hashmap is updated by reference)
</sqf>
 
|seealso= [[getOrDefaultCall]] [[HashMap]] [[get]]
}}
}}

Latest revision as of 13:59, 8 November 2023

Hover & click on the images for description

Description

Description:
Retrieves a value out of a HashMap after looking it up by its key. When the HashMap doesn't contain the key, the default value is returned
Groups:
HashMap

Syntax

Syntax:
hashMap getOrDefault [key, defaultValue, setDefault]
Parameters:
hashMap: HashMap
key: HashMapKey - case sensitive key
defaultValue: Anything - (Optional, default nil)
since Arma 3 logo black.png2.04
setDefault: Boolean - (Optional, default false) if set to true and value does not exist in provided hash map, defaultValue will be added to the hash map
Return Value:
Anything

Examples

Example 1:
private _myValue = _myHashMap getOrDefault ["a", "Not Found!"];
Example 2:
private _myValue = _hashMap getOrDefault ["a", "Not Found!", true]; // will add default value to the hash map if it does not exist
Example 3:
private _array = _hashmap getOrDefault [_key, [], true]; // a new array will be created if it doesn't exist _array pushBack _item; // _item will be added to the array (the one in the hashmap is updated by reference)

Additional Information

See also:
getOrDefaultCall HashMap get

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note