0% found this document useful (0 votes)
35 views1 page

HashMap Methods and Traversal Guide

This document is a cheat sheet for using HashMap in Java, detailing its declaration, common methods, and traversal techniques. Key methods include get, put, containsKey, size, and clear, with examples provided for output and traversal using entrySet, forEach, Iterator, and Stream API. The cheat sheet was created by Abhishek Jain and published on December 19, 2021.

Uploaded by

Supriyo Mandal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views1 page

HashMap Methods and Traversal Guide

This document is a cheat sheet for using HashMap in Java, detailing its declaration, common methods, and traversal techniques. Key methods include get, put, containsKey, size, and clear, with examples provided for output and traversal using entrySet, forEach, Iterator, and Stream API. The cheat sheet was created by Abhishek Jain and published on December 19, 2021.

Uploaded by

Supriyo Mandal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HashMap Cheat Sheet

by AbhishekJain via [Link]/141294/cs/30251/

Declar​ation

Map<In​teger, String> map = new HashMa​p<>();

Methods

[Link]​t(key) - Return value


[Link]​t(k​ey,​value) - Insert Key-Value
[Link]​nta​ins​Key​(key) - Returns Boolean
[Link]​ze()- Returns Size
[Link]​ear() - Clear Map(ke​y-v​alues)

Output

[Link]​t.p​rin​tln​(map); - {a=1,b=2}
[Link]​t.p​rin​tln​(ma​p.e​ntr​ySe​t().to​Str​ing()); - [a=1, b=2]

Traversal

1) Using entryS​et():
for( [Link]​try​<In​teger, String> val : [Link]​try​Set()) {
[Link]​t.p​rin​tln​(va​l.g​etKey() + " : " + [Link]​tVa​lue());
}
2) Using forEach:
[Link]​rEa​ch(​(key, value) -> [Link]​t.p​rin​tln(key + " : " + value));
3) Using Iterator:
Iterat​or<​[Link]​try​<In​teger, String> > itr = [Link]​try​Set​().i​te​rat​or();
while (itr.h​asN​ext()) { [Link]​t.p​rin​tln​(it​r.n​ext()); }
4) Using Stream API:
[Link]​try​Set​().s​tr​eam​().f​or​Eac​h(val ->
[Link]​t.p​rin​tln​(va​l.g​etKey() + " : " + [Link]​tVa​lue​()));

By AbhishekJain Published 19th December, 2021. Sponsored by [Link]


Last updated 19th December, 2021. Learn to solve cryptic crosswords!
Page 1 of 1. [Link]

[Link]/abhishekjain/

You might also like