Thursday 27 January 2022

Top 16 Hashmap interview question and answers 2023? QAE - SDET - SDE Java Collections Interview Questions 2023

 


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:

SET-02 10-20

Java Interview question Set-3:

SET-03 21-30

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?



Click Here For The Answer



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:

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, HashMap allows one null key, which is stored at the first location of the bucket array e.g., bucket[0] = value. HashMap doesn't call HashCode() on the null key because it will throw NullPointerException, hence when a user calls the get() method with null, then the value of the first index is returned.

16. Can you store the duplicate value 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:

41. What is Eager and Lazy Initialization of Singleton Class ?


42. What modifiers are accepted for Main class in Java?

Ans: public, abstract & final are permitted
 
43. Write code to reverse a string without using function?

Ans:
public class reverse {
    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);

          }
    }

44. How to do string manipulation in java?





No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

API Status Codes with examples for QA-Testers

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 🏮In API testing, it...