0% found this document useful (0 votes)
3 views

Java 2

A HashMap in Java is a widely used data structure for storing key-value pairs, allowing for efficient addition, retrieval, and deletion of values using keys. To create a HashMap, the java.util.HashMap package must be imported, and it requires specifying the types for keys and values. Key methods include .put() for adding entries and .get() for retrieving values, with additional considerations for using custom classes as keys, which necessitate overriding the equals and hashCode methods.

Uploaded by

Angel Ciobotaru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java 2

A HashMap in Java is a widely used data structure for storing key-value pairs, allowing for efficient addition, retrieval, and deletion of values using keys. To create a HashMap, the java.util.HashMap package must be imported, and it requires specifying the types for keys and values. Key methods include .put() for adding entries and .get() for retrieving values, with additional considerations for using custom classes as keys, which necessitate overriding the equals and hashCode methods.

Uploaded by

Angel Ciobotaru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

JAVA 2

Hash Map
A HashMap is, in addition to ArrayList, one of the most widely used of
Java's pre-built data structures. The hash map is used whenever data is
stored as key-value pairs, where values can be added, retrieved, and
deleted using keys.

In the example below, a HashMap object has been created to search for
cities by their postal codes, after which four postal code-city pairs have
been added to the HashMap object. At the end, the postal code "00710"
is retrieved from the hash map. Both the postal code and the city are
represented as strings.
Creating an HashMap
Using a hash map requires the import java.util.HashMap; statement at
the beginning of the class.
Two type parameters are required when creating a hash map - the type of
the key and the type of the value added.
If the keys of the hash map are of type string, and the values of type
integer, the hash map is created with the following
statement HashMap<String, Integer> hashmap = new
HashMap<>();
Adding to the hash map is done through the .put(*key*,
*value*) method that has two parameters, one for the key, the other for
the value.
Retrieving from a hash map happens with the help of
the get(*key*) method that is passed the key as a parameter and returns
a value.
Declaring an HashMap into a class
A Reference Type Variable as a Hash Map Value
.containsKey()

For-each loop with keySet


Primitive Variables In Hash Maps
Method to Test For Equality - "equals"
For a class to be used as a HashMap's key, we need to define for it:
1) the equals method, so that all equal or approximately equal objects
cause the comparison to return true and all false for all the rest
2) the hashCode method, so that as few objects as possible end up with
the same hash value

hashCode method.

Adding an ArrayList for a hashmap values


Extending a class
Calling a superclass method

Calling the constructor of the superclass

You might also like