Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linkedhashmap
putifabsent
dart
communitycreator

What is LinkedHashMap.putIfAbsent method in Dart?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The LinkedHashMap is a hash-table based implementation of MapMap contains a list of key-value pairs as an element.. The LinkedHashMap maintains the insertion order of the entries.

Note: Read more about the LinkedHashMap here.

The putIfAbsent method adds the key-value pair to the map if the key is not present in the map.

If the key is already present, then it skips the operation.

Syntax

V putIfAbsent(K key, V ifAbsent())

Parameters

This method takes two parameters.

  1. key: This is the key for which value is to be added if there is no entry already available.

  2. ifAbsent: This is a function that will be invoked when there is no entry for the key present in the map. This method should return a value. That value is used when we add a new entry.

Return value

If the key is already present in the map, then the old value associated with the key is returned and insertion will be skipped.

If the key is not present in the map, then a new key-value pair is inserted and the new value is returned.

Example

The code below demonstrates how to use the putIfAbsent method:

import 'dart:collection';
void main() {
//create a new LinkedHashmap which can have string type as key, and int type as value
LinkedHashMap map = new LinkedHashMap<String, int>();
// add two entries to the map
map["one"] = 1;
map["two"] = 2;
print('The map is $map');
print('\nCalling put if absent for key "one"');
// using putIfAbsent to add entry for key "one"
var result = map.putIfAbsent("one", ()=> 10);
print('map.putIfAbsent("one", ()=> 10) : $result');
print('The map is $map');
print('\nCalling put if absent for key "three"');
// using putIfAbsent to add entry for key "three"
result = map.putIfAbsent("three", ()=> 3);
print('map.putIfAbsent("one", ()=> 10) : $result');
print('The map is $map');
}
Using putIfAbsent to add a new entry if the key doesn't have a entry in map

Explanation

  • Line 1: We import the collection library.

  • Line 4: We create a new LinkedHashMap object with the name map.

  • Lines 7–8: We add two new entries to the map. Now the map is {one: 1, two: 2}.

  • Line 14: We use the puIfAbsent method with the following paramters:

  • one as the key.

  • The ifAbsent function, which returns 10 as return value.

In our case, there is an entry already available for the key one. The old value 1 associated with the key one is returned.

  • Line 20: We use the puIfAbsent method with the following parameters:

  • three as the key.

  • The ifAbsent function which returns 3 as a return value.

In our case, there is no entry available for the key three. So a new entry with key three and value 3 is added to map and the new value 3 is returned. Now, map is {three: 3, one: 1, two: 2}.

RELATED TAGS

linkedhashmap
putifabsent
dart
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring