EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Software Development Basics Hashmap and Hashtable
Secondary Sidebar
Software Development Basics
  • Basics
    • Microsoft Expression Web
    • IDE
    • Microsoft Flow
    • Unity Dashboard
    • Servlet Dispatcher
    • Types of Algorithms
    • Vue.js? nextTick
    • Vue.js Transition
    • Page Replacement Algorithms
    • What is CLI
    • Open Source Software
    • Solve Problems With Technology (Simple)
    • What is Application Software & Types
    • Microsoft Word Alternative
    • ADDIE Model
    • V-model advantages and disadvantages
    • Gatsby Plugins
    • Putty version
    • Xampp versions
    • Avro converter
    • Avro Data Types
    • Avro Schema Evolution
    • Avro Serialization
    • Cloudera Impala
    • Cloudera Careers
    • Entity Framework Core
    • Gulp File Include
    • Gulp Autoprefixer
    • Gulp File
    • Gulp Terser
    • System Software Tools
    • System Software Components
    • Typography App
    • Software as a Service (Saas)
    • Icon Font Pack
    • Interpret Results Using ANOVA Test
    • Blogging Insights Your Analytics
    • Increase Productivity Technology
    • Free Multimedia Software
    • Information Technology Benefits
    • What is SPSS and How Does It Work
    • Learn to Code For Beginners (Advance)
    • Uses of Coding
    • Uses Of Raspberry Pi
    • What Is System Design
    • Introduction to NLP
    • What is MapReduce
    • What is SoapUI
    • What is MVC
    • What is Multithreading
    • What is Neural Networks
    • What is Swift
    • What is PLC
    • What is Open Cart
    • What is Mainframe
    • What is JMS
    • What is Cognos
    • What is Open Source
    • What is Bot
    • What is SOAP
    • What is COBOL
    • What is GraphQL
    • What is Microcontroller
    • What is Open-Source License
    • What is Visual Studio Code
    • What is Pandas
    • What is Hypervisor
    • What is Common Gateway Interface
    • What is IDE?
    • What is MVC Design Pattern
    • What is Application Server
    • What is GPS
    • What is Botnet
    • What is Assembly Language
    • System Analysis And Design
    • HTTP Caching
    • What is Buffer Overflow
    • What is Ajax
    • What is Appium
    • What is SVN
    • What is SPSS
    • What is WCF
    • What is Groovy
    • What is Clickbait
    • What is SOA
    • What is GUI
    • What is FreeBSD
    • What is WebSocket
    • What is WordPress
    • What is OSPF
    • What is Coding
    • What is Raspberry Pi
    • HTTP Cookies
    • What is Hub?
    • What is Bridge
    • What is Switch
    • What is Internet Application
    • What is Sensors
    • What is Proximity Sensors
    • What is Full Stack
    • System Design Interview Questions
    • What is Salesforce technology
    • What is Salesforce Sales Cloud
    • What is OOP
    • What is CMD
    • What is React
    • React Redux Typescript
    • What is DSS
    • What is SVG
    • SVG File
    • Bash Sleep Command
    • What is MTU
    • What is Apex
    • What is Desktop Software
    • Tor Browser, Anonymity and Other Browsers
    • Avoid Pitfalls of Shadow IT
    • Freelance Web Graphic Designer
    • What is Storage Virtualization
    • What is Web Services?
    • What is Social Networking?
    • What is Microservices Architecture?
    • Microservices Tools
    • Advantages of Microservices
    • Uses of Internet
    • Software Platforms
    • Uses of Internet for Business
    • Architecture of Web Services
    • Web Application Testing
    • Advantages of Web Service
    • CPU Virtualization
    • Types of Web Services
    • Web Services Testing
    • What is RabbitMQ?
    • RabbitMQ Architecture
    • Advantages of Bitcoin
    • LINQ foreach
    • Penetration Testing Services
    • Puppet Alternatives
    • What is Memcached?
    • What is Browser?
    • Types of Satellites
    • Model Driven Architecture
    • Types of Variables in Statistics
    • Best Statistics Certifications
    • Integration Architecture
    • What is API Integration?
    • What is Grid Computing?
    • Asus File Manager
    • What is GPRS?
    • What is Gradle?
    • What is Basecamp?
    • Software System Architecture
    • GSM Architecture
    • What is Nagios?
    • AppDynamics Tool
    • Logical Architecture
    • What is Microsoft Planner
    • What is Circuit Switching
    • What is ARM?
    • Embedded Control Systems
    • Embedded System Programming
    • Embedded System Development
    • Embedded Systems Software
    • Embedded System Project
    • Types of Embedded Systems
    • Requirement Engineering
    • Types of Engineering
    • What is WAP
    • What is Registry?
    • What is Dynatrace?
    • What is Digital Forensics?
    • Hardware Virtualization
    • AppDynamics Careers
    • Bandwidth Monitoring Tools
    • Ping Monitor Tools
    • Dynatrace Tools
    • What is Trello?
    • What is AppDynamics?
    • What is Remote Desktop?
    • What is Extranet?
    • What is LTE Network?
    • What is Firebase?
    • Website Monitoring Tool
    • Number Systems
    • Service Desk Manager
    • Static Website
    • Dynamic Website
    • What is Email?
    • What is URL Link?
    • What is Program?
    • What is Lock Screen?
    • What is Grafana
    • Unguided Media Transmission
    • IT Governance
    • IT Governance Framework
    • Remote Support Softwares
    • What is Unification?
    • Topological Map
    • What is LAMP?
    • USB Flash Drive
    • Software Development Models
    • Digital Circuit
    • What is Webpack?
    • Fault Tolerance
    • What is DSL Modem?
    • What is Mozilla Firefox?
    • What is Vagrant?
    • Types of Research Methodology
    • Grafana Plugins
    • Ionic Components
    • Nginx Error_page
    • Nginx Include
    • Nginx Version
    • Nginx Force HTTPS
    • Nginx Environment Variables
    • Nginx Container
    • RabbitMQ Routing Key
    • CakePHP
    • Telegram Features
    • What is CDN
    • RethinkDB
    • Symfony Version
    • UWP
    • cPanel version
    • What is assembly?
    • Seed7
    • Switching Techniques
    • OCaml
    • Pseudocode?Algorithm
    • Quality Control Methods
    • What is OneNote?
    • Workstation Uses
    • Soft Computing Techniques
    • Remote Access Software
    • Remote Desktop Tools
    • OneNote Shortcuts
    • Software Review
    • What is Qubit?
    • Static Analysis Tools
    • Register in Microprocessor
    • What is VDI?
    • What is Svelte?
    • RabbitMQ Version
    • Groovy Version
    • Code Walkthrough
    • What is Telegram?
    • Gradle Version
    • What is Recycle Bin?
    • What is Cordova?
    • Swagger version
    • Doxygen
    • Phalcon
    • Metasploit Framework
    • Microsoft Word Shortcut Keys
    • Wordpad shortcut keys
    • Burp Suite
    • Google Docs Shortcuts
    • Install VPN
    • Frontend Challenges
    • CodeIgniter Version
    • VMware Tools
    • CDMA Advantages
    • CDMA Uses
    • Servlet Session Management
    • ServletConfig
    • Servlet Class
    • Log4j Version
    • Remote Desktop Softwares
    • Soapui Load Test
    • Scikit Learn Version
    • VMware Benefits
    • Google Slides Shortcuts
    • What is XAMPP?
    • What is PyGTK?
    • VMware Fusion
    • What is cPanel?
    • Ubuntu Version
    • Server Types
    • App Analytics Tools
    • DNS Types
    • Evernote Features
    • Restful architecture
    • GNOME Keyboard Shortcuts
    • AngelScript
    • NativeScript Layouts
    • PowerPoint Version
    • setInterval Function
    • Shopify Apps
    • TypeScript foreach loop
    • Socio Technical System
    • PowerPoint Shortcut Keys
    • Civil Engineering Tools
    • OpenLayers vs Leaflet
    • Circuit Switching Advantages and Disadvantages
    • LotusScript
    • Multiplexer
    • Multiple Access Protocol
    • Types of Broadband
    • What is Standardization
    • Methods of Development
    • Software Requirement Specification
    • CentOS restart network
    • Bouncy numbers
    • Burp suite proxy
    • Redshift window functions
    • Mesh Topology Advantages and Disadvantages
    • What is Zabbix?
    • Test Techniques
    • Test Development
    • What is PyCharm
    • What is REST
    • JDBC version
    • System software features
    • Ableton versions
    • Unreal engine version
    • RAD advantage disadvantage
    • Incremental Model Advantage and Disadvantage
    • Disadvantages of Internet
    • What is VoIP
    • WAP Architecture
    • CentOS unzip
    • Cubase Shortcuts
    • Cubase Versions
    • Libreoffice shortcut keys
    • Archiving Software
    • Layered Architecture
    • Coverage Types
    • What is Kivy?
    • Types of Methodology
    • Swift JSON
    • JSON Serialize
    • TypeScript?boolean
    • TypeScript keyof object
    • TypeScript RegEx
    • TypeScript?date
    • TypeScript object
    • CentOS Version
    • XSLT if else
    • Binary Search JavaScript
    • Binary search with recursion
    • PLSQL Replace
    • Evernote Notes
    • Rust vs Python
    • Test Scenario
    • Deadlock in Operating System
    • MVVM Architecture
    • MVVM Flutter
    • What is Keyboard
    • WordPress Hosting
    • Software requirement
    • CentOS Add User to Group
    • Backup Types
    • Firewall Rules
    • Microprocessor Features
    • Maven Versions
    • OneNote features
    • Binary search tree insertion
    • Quick sort algorithm
    • B+ tree insertion
    • What is Automation?
    • What is Digital Electronics?
    • Wireless Transmission Media
    • Border Gateway Protocol
    • Email Encryption Software
    • Endpoint Encryption
    • Outlook Alternative
    • What is Abacus
    • Encapsulation Benefits
    • FL Studio Keyboard Shortcuts
    • NordVPN Features
    • Statsmodels API
    • Statsmodels Linear Regression
    • Buzz number
    • Krishnamurthy Number
    • What is Compact Disc?
    • Bucket Sort Algorithm
    • Insertion Sort Algorithm
    • Redis Version
    • Chatbot Benefits
    • Full Stack Technologies
    • Civil Engineering Types
    • Tomcat Web Server
    • Tomcat Native
    • Tkinter Scrolledtext
    • Anaconda Navigator
    • UML Class Diagram
    • System Monitoring Tool
    • Drupal Features
    • Drupal Free Themes
    • Drupal Modules
    • Drupal 9
    • Drupal Developer
    • Drupal Webform
    • Drupal 8
    • Drupal 8 Themes
    • Drupal Views
    • System Software Functions
    • What is Linker?
    • What is K Map?
    • Website Testing Tool
    • TypeScript map
    • TypeScript enum
    • TypeScript class
    • Hill Climbing Algorithm
    • Hashmap and Hashtable
    • Nexus Plugin
    • Entity Framework Delete by ID
    • What is NumPy?
    • What is NLP?
    • Vishing Attack
    • Test Plan in Software Testing
    • Guest Mode
    • What is Mockito?
    • Advantage of the Internet
    • SVG Creator
    • Rails Logger
    • Intellij Plugins
    • Intellij Shortcuts
    • IntelliJ Maven
    • IntelliJ JavaFX
    • IntelliJ Lombok Plugin
    • IntelliJ Format Code
    • IntelliJ gitignore
    • IntelliJ Find and Replace
    • RESTEasy

Related Courses

Software Testing Training

Selenium Training Certification

Appium Training

JMeter Certification Training

Hashmap and Hashtable

Hashmap and Hashtable

Definition of Hashmap and Hashtable

Hashmap and Hashtable are some of the main data structures of java used according to the requirement. Both the data structure are part of the collection in Java. They have certain distinctions among themselves with some differences as well. This difference mainly revolves around the synchronization aspects like Hash map is non-synchronized whereas hashtable is synchronized. HashMap allows one null key, but there can be many numbers of keys as well. On the other hand, the Hash table does not follow any kind of ordering for numbers, and also it does not map to any other particular order.

Syntax 0f Hashmap and Hashtable

There is no particular syntax for Hashmap or Hashtable, but it is called within the method once we declare the collection while importing from the standard library with hashmap and hashtable also supported by a certain standard of the library.

Syntax for HashMap()

HashMap hm1 = new HashMap();
.
.
Set k_ys = hm1.keySet ();
for (obj k1: k_ys) {
hm1.put (objct & key_value pair);
}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax for Hashtable()

Hashtable hs_tb = new Hashtable();
.
.
Enumeration ky_s = hs_tb.ky_s();
for (Enumeration en_0 = hs_tb.el() ; en.hasMoreel() ; en.nextel())
{
hs_tb.put (key & vl_pair);
}

In both cases, a ConcurrentModificationException will be present in the object definition with key-value and pair.

How do Hashmap and Hashtable work?

  • Both Hashmap and Hashtable play a pivotal role, whether with collections frameworks or generally with respect to any of the scenarios being mentioned as part of the requirement.
  • Hashmap doesnot preserve any specific order when used with Java as a programming language, so if a scenario comes where a specific order needs to be maintained, then the use of sorting gets arises for implementation.
  • Both hashmap and hashtable implement the same interface with java.util.Map interface where they have very minor differences in terms of their working.
  • Hashmap is non-synchronized, and it’s mostly used in a multithreaded environment where there it gets a way to access and process the Hashmap simultaneously.
  • On the other hand, when compared to Hashmap, it behaves completely opposite in a sense it is synchronized in nature where it makes sure that one thread can access any of the multiple threads at a given point of time where the thread works by acquiring a lock to make other threads wait till it gets fully terminated.
  • Since Hashmap doesnot consist of order and synchronization thus it makes use of some sorting techniques where the hashing with sorting takes place; then, it is needed to keep keys and values paired within it, which is mandatory.
  • Using sorting within hashmap with TreeMap makes its perfect usage with the keys it provides. Also, LinkedHashMap which gets its uses.
  • There is a convention that if in case we make use of LinkedHashMap, then it is needed to get the set converted into a list, and further that converted list gets added into LinkedHashMap in that very same order.
  • Both have differences, although supports for the almost same set of libraries.

Examples of Hashmap and Hashtable

Different examples are mentioned below:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,629 ratings)

Example #1

This program demonstrates the Hashmap with an ability to store the key and value pair by creating a hashmap and parallelly inserting the values into it, as shown in the output below.

Code:

import java.util.*;
public class HashMapEx{
public static void main(String args[]){
HashMap<Integer,String> mp_0=new HashMap<Integer,String>();
mp_0.put(1,"Sedan");
mp_0.put(2,"SUV");
mp_0.put(3,"Micro");
mp_0.put(4,"Macro");
System.out.println("Iteration_with_map_for_working.");
for(Map.Entry mp_2 : mp_0.entrySet()){
System.out.println(mp_2.getKey()+" "+mp_2.getValue());
}
}
}

Output

Output-1.2

Example #2

This program demonstrates the hashtable example where the hashtable takes into account the values in the form of marks for their respective subjects, as shown in the output below.

import java.util.*;
public class Hashtbl_ex
{
public static void main(String args[]){
Hashtable<Integer,String> hi_o=new Hashtable<Integer,String>();
hi_o.put(56,"Physics_marks");
hi_o.put(12,"Chem_mrks");
hi_o.put(100,"Bio_mrks");
hi_o.put(13,"Maths_mrks");
for(Map.Entry m_0:hi_o.entrySet()){
System.out.println(m_0.getKey()+" "+m_0.getValue());
}
}
}

Output:

Output-1.3

Example #3

This program demonstrates the Hashmap where the List of students defined and traversed is getting updated using the update and replace function incorporating respective keys and elements as shown in the output below.

import java.util.*;
public class HashMap3_Ex{
public static void main(String args[]){
HashMap<Integer,String> hm_0=new HashMap<Integer,String>();
hm_0.put(10,"Anu_0");
hm_0.put(11,"Jack_1");
hm_0.put(12,"Rosy_8");
System.out.println("A_List_of_Students:");
for(Map.Entry m_2:hm_0.entrySet())
{
System.out.println(m_2.getKey()+" "+m_2.getValue());
}
System.out.println("List_of_elements_updated_for:");
hm_0.replace(10, "Anu_1");
for(Map.Entry m_2:hm_0.entrySet())
{
System.out.println(m_2.getKey()+" "+m_2.getValue());
}
System.out.println("list_of_elements_updated..:");
hm_0.replace(12, "Rosy_0", "Raj_7");
for(Map.Entry m_2:hm_0.entrySet())
{
System.out.println(m_2.getKey()+" "+m_2.getValue());
}
System.out.println("Next_List_of-elemets_updated:");
hm_0.replaceAll((k,v) -> "Joy_8");
for(Map.Entry m_2:hm_0.entrySet())
{
System.out.println(m_2.getKey()+" "+m_2.getValue());
}
}
}

Output:

Hashmap and Hashtable-1.4

Example #4

This program demonstrates the hashtable where the elements inserted within the stack can be removed with the same index, which in turn can return the same set of elements to the end-user, as shown in the output below.

import java.util.*;
public class Hashtabl3_Ex
{
public static void main(String args[])
{
Hashtable<Integer,String> mp_0=new Hashtable<Integer,String>();
mp_0.put(10,"Ani_0");
mp_0.put(12,"Revi_1");
mp_0.put(11,"Biden_8");
mp_0.put(13,"Rahl_0");
System.out.println("Representation_Before_map "+ mp_0);
mp_0.remove(12);
System.out.println("Representation_After_map_removal: "+ mp_0);
}
}

Output:

Hashmap and Hashtable-1.5

Example #5

This program demonstrates sorting being performed on the values of HashMap, as shown in the output below.

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SortHashMapValue
{
public static void main(String[] args)
{
HashMap<Integer, String> hm_0 = new HashMap<Integer, String>();
hm_0.put(7, "Anu_0");
hm_0.put(8, "Ash_1");
hm_0.put(10, "Zoy_8");
hm_0.put(18, "Ysh_7");
hm_0.put(11, "Prvn_0");
hm_0.put(37, "Bby_6");
hm_0.put(1, "Rtesh_0");
System.out.println("Representation_Before_Sorting");
Set set = hm_0.entrySet();
Iterator itr_7 = set.iterator();
while(itr_7.hasNext())
{
Map.Entry mp_1 = (Map.Entry)itr_7.next();
System.out.println("Roll no:  "+mp_1.getKey()+"     Name:   "+mp_1.getValue());
}
Map<Integer, String> mp_2 = sortValues(hm_0);
System.out.println("\n");
System.out.println("Representation_After_Sorting:");
Set st2 = mp_2.entrySet();
Iterator itr2 = st2.iterator();
while(itr2.hasNext())
{
Map.Entry je2 = (Map.Entry)itr2.next();
System.out.println("Roll no:  "+je2.getKey()+"     Name:   "+je2.getValue());
}
}
private static HashMap sortValues(HashMap mp_2)
{
List lst_0 = new LinkedList(mp_2.entrySet());
Collections.sort(lst_0, new Comparator()
{
public int compare(Object l1, Object l2)
{
return ((Comparable) ((Map.Entry) (l1)).getValue()).compareTo(((Map.Entry) (l2)).getValue());
}
});
HashMap sorthsh_Map = new LinkedHashMap();
for (Iterator it_9 = lst_0.iterator(); it_9.hasNext();)
{
Map.Entry etry = (Map.Entry) it_9.next();
sorthsh_Map.put(etry.getKey(), etry.getValue());
}
return sorthsh_Map;
}
}

Output:

Hashmap and Hashtable-1.6

Conclusion

HashMap and Hashtable both use almost the same set of the library but have helped boost the key and value pair lists when it comes to maintaining them. The data structure has also helped the collection framework by providing developers leverage to play around with a huge set of elements defined in terms of keys and values.

Recommended Articles

This is a guide to Hashmap and Hashtable. Here we also discuss the definition and how hashmap and hashtable work? Along with different examples and their code implementation. You may also have a look at the following articles to learn more –

  1. Hashtable in Java
  2. HashMap vs TreeMap
  3. Hadoop vs Cassandra
  4. Java hashCode()
Popular Course in this category
Software Testing Training (11 Courses, 2 Projects)
  11 Online Courses |  2 Hands-on Projects |  65+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Selenium Automation Testing Training (11 Courses, 4+ Projects, 4 Quizzes)4.9
Appium Training (2 Courses)4.8
JMeter Testing Training (3 Courses)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more