Difference between hashmap and map in Java
This tutorial explains the key differences between Map
and in Java HashMap
.
In Java, Map
is an interface for storing data in key-value pairs, and HashMap
is Map
an implementation class of the interface. Java has several classes ( TreeHashMap
, LinkedHashMap
) that implement Map
the interface to store data into key-value pairs. Let's look at some examples.
Map
Interfaces
in Java
You cannot use the interface alone Map
to save data, but we can create an object of its implementation class and then use Map
the reference to save the object. Here, we use HashMap
the class to store the data and the Map
interface to save the reference of that class. See the example below.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map);
}
}
Output:
{One=1, Two=2, Three=3}
HashMap
Classes
in Java
HashMap
It is Map
an implementation class of the interface. Therefore, we can use it to create a collection of key-value pairs. See the example below.
import java.util.HashMap;
public class SimpleTesting {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map);
}
}
Output:
{One=1, Two=2, Three=3}
Map
Using references to store objects
in Java
Since Map
is an interface, we can use it to save references to its implementing classes, such as HashMap
, TreeMap
etc. We can save TreeMap
a or HashMap
object Map
in an interface. See the example below.
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SimpleTesting {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map);
Map<String, Integer> tmap = new TreeMap<>(map);
System.out.println(tmap);
}
}
Output:
{One=1, Two=2, Three=3}
{One=1, Three=3, Two=2}
Map
Using references to store objects
in Java
This is an important example of using reference when working with implementation classes Map
. Look, we have a method that takes Map
an object as a parameter. So, while calling, we can pass an object of any class like HashMap
or HashTable
. See the example below.
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class SimpleTesting {
static void printMap(Map<String, Integer> map) {
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
public static void main(String[] args) {
HashMap<String, Integer> hashmap = new HashMap<>();
hashmap.put("One", 1);
hashmap.put("Two", 2);
hashmap.put("Three", 3);
printMap(hashmap);
TreeMap<String, Integer> tmap = new TreeMap<>(hashmap);
printMap(tmap);
LinkedHashMap<String, Integer> lmap = new LinkedHashMap<>(hashmap);
printMap(lmap);
}
}
Run the following code.
Java SimpleTesting 10 20
Output:
10
20
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Get user home directory in Java
Publish Date:2025/04/14 Views:161 Category:Java
-
This tutorial explains how to get the user's home directory in Java and lists some sample code to guide you through the topic. For a multi-user operating system, each user has a file system directory; this directory is called the user's hom
Difference between size and length in Java
Publish Date:2025/04/14 Views:114 Category:Java
-
This tutorial explains the difference between size and length in Java. We have also listed some sample codes to help you understand the topic. Java has a size() method and a length property. Beginners may think that they are interchangeable
Mutex Locks in Java
Publish Date:2025/04/14 Views:198 Category:Java
-
In the field of computer science, mutual exclusion or mutex is known as the property of concurrency control. Every computer uses a minimum sequence of program instructions called a thread. At a time, the computer works on one thread. For be
How to compare characters for equality in Java
Publish Date:2025/04/14 Views:128 Category:Java
-
This tutorial shows you how to check if two characters are equal in Java. In Java, we can use the equals( == ) operator or the Character compare() equals() method of the Character class to compare two characters. If you are working with pri
Compile multiple Java files with a single command in Java
Publish Date:2025/04/14 Views:192 Category:Java
-
This tutorial explains how to compile multiple java files using a single command in Java. Compilation is a term used to refer to the process of converting java source code into bytecode using JDK. To execute any Java file, we need to follow
Arrow operator in Java ->
Publish Date:2025/04/14 Views:113 Category:Java
-
This tutorial explains - the role of the arrow operator ( ) in Java and lists some sample code to understand the topic. In Java 8, a new feature lambda expression was added, and the arrow operator appeared in Java to form lambda expressions
>> operator in Java
Publish Date:2025/04/14 Views:66 Category:Java
-
This guide will introduce you to the operator in Java . To understand this concept, you need to be familiar with some lower-level computing concepts. For example, bits, bytes, etc. Let's take a deeper look. Operators in Java In Java, the op
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Sending Emails Using the Mail Form in PHP
Publish Date:2025/04/12 Views:116 Category:PHP
-
This article will demonstrate installing sendmail the library and sending an email through the PHP mail form. Install sendmail to send email from PHP on local server PHP has a built-in function mail() to send emails. However, this function