generated from nus-cs2030/base-template
-
Notifications
You must be signed in to change notification settings - Fork 4
HashMap
nmtuan2001 edited this page Oct 11, 2021
·
3 revisions
HashMap is a part of Java's collection and this class can be found in java.util package. It allows us to store a key-value pair and access them by an index of another type. One object is used as a key (index) to another object (value). If we try to insert a duplicate key, it will then replace the element of the corresponding key.
// IMPORT
import java.util.HashMap;
// PUT
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("firstKey", 1);
hashMap.put("secondKey", 2);
hashMap.put("thirdKey", 3);
// GET
int value = hashMap.get("firstKey");
// REMOVE
hashMap.remove("secondKey");
// CHECK
hashMap.containsKey("thirdKey");
// ITERATE USING FOR EACH
for (String key : hashMap.keySet()) {
System.out.println(hashMap.get(key));
}
Peer Learning
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Stu
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
SSH Without Password
Copying Files From PE Nodes
Using tmux
CS2030 Contents
Lecture 1 SummaryLecture 2 Summary
Access Modifiers
Lecture 3 Summary (Polymorphism)
Compile Time Type VS Runtime Type
Abstraction, Encapsulation, Inheritance, and Polymorphism
SOLID Principles
Class VS Abstract Class VS Interface
Comparable VS Comparator
Generic Types T
HashMap
Raw Types with Generic
Lambda expression
PECS (Producer Extends Consumer Super)
Optional
Streams
Parallel Streams
Monad
Functors and Monads with Category Theory