Pre-requisite: How to install JDK and set Java Home path in Windows and MacOs ?
Ans: Click here for Steps
Java Interview question Set-1:
SET-01 1-10
Java Interview question Set-2:
Java Interview question Set-3:
Below are the Top Hashmap Interview questions and answers for Java Interview preparation:
1. How to initialize a map with key as string and value as Integer?
HashMap<String, Integer> map = new HashMap<>();
2. How to add and remove elements from Hashmap?
Add Elements:
map.put("james", 10);
map.put("sidharth", 30);
map.put("shukla", 20);
Remove Elements:
map.remove("vishal", 10);
3. How to iterate through hashmap?
for (Map.Entry<String, Integer> e : map.entrySet())
System.out.println("Key: " + e.getKey()
+ " Value: " + e.getValue());
}
4. Does hashmap allow duplicate key and values?
Ans:
Hashmap doesnt allow duplicate key, but allows duplicate values
5. Does hashmap allow null key and values for Hashmap?
Ans:
Hashmap allows null key but only once,multiple null values supported
6. How to sort Hashmap in Java?
7. Do hashmap is thread safe?
Ans: Hashmap is not thread safe as it is not synchronized
8. Does hashmap maintain insertion order?
Ans:
Hashmap doesn't maintain insertion order, but Treemap/LinkedhashMap maintains the insertion order.
9. Which method helps to get all the keys in hashmap?
- keyset(): Returns a
Set
view of the keys contained in this map
....> HashMap<String, Integer> map = new HashMap<>();
....> map.keyset();
"String Question and Answers" <== Click
10. How to replace value in hashmap?
with replace():Replaces the entry for the specified key only if it is currently mapped to some value
HashMap<String, Integer> map = new HashMap<>();
map.put("james", 10);
map.replace("james", 50);
11. What is Hashmap?
- Part of Collection Framework
- Implements Map interface
- There are 4 main fields are Hash , Key , Value , Node
12. How to find duplicate elements using Hashmap ?
Click here for Answer With Code
13. Difference between Hashmap and Hashtable?
Hashtable:
Hashtable is synchronized. It is thread-safe
Hashtable doesn't allow any null key or value.
Hashtable is inherited from Dictionary class.
HashMap is non synchronized.It's not-thread safe.
HashMap allows one null key and multiple null values.
HashMap is inherited from AbstractMap class.
Bonus:
14. How hashmap works?
HashMap in Java works on hashing principles. Hashing is a process in which hash functions are used to link keys and values in HashMap.
If you want an in-depth explanation of how Hashmap works,
please refer to this link.
15. Can we store a null key in Java HashMap?
Yes, you can put duplicate values in HashMap of Java. It allows duplicate values; that's why when you retrieve all values from the Hashmap by calling the values() method, it returns a Collection and not a Set.
{ Array Question and Answers } <--Click
REVISION:
public static void main (String args[]) {
String str = "automationreinvented";
int n = str.length();
String rev="";
for (int i=n-1;i>=0;i--)
rev = rev + str.charAt(i);
System.out.println(rev);
}
}
****************************************
TOP 15 BDD - CUCUMBER Interview Q&A
************************************************
✍️AUTHOR: LinkedIn Profile
************************************************
Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session
*************************************************
SeleniumWebdriver Automation Testing Interview Questions:
https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver
API Testing Interview Question Set:
https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html
DevOps Interview Q&A:
https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes
Docker Interview Question Set
https://automationreinvented.blogspot.com/Docker
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign
Java Related Interview Question Set
https://automationreinvented.blogspot.com/search/label/Java
GIT Interview Question Set:
https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html
Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions
Mobile Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Mobile%20Testing
Python Interview Question Set for QAE - SDET - SDE:
https://automationreinvented.blogspot.com/search/label/Python
No comments:
Post a Comment