Part 7
Interview questions on Collections in java for experienced covered in this post:
What are the characteristics of Map
Interface?
-
Map represents the group of objects as a key-value pair.
-
Map is not a child interface of the Collection interface.
-
Both keys and values are objects only.
-
Characteristics
of the map are as follows.
1.
Underlying data structure is hashtable.
2.
Insertion order is not preserved.
3.
Inertion order is based on hashcode keys.
4.
Duplicates keys are not allowed.
5.
Values can be duplicated.
6.
Heterogeneous (Different types of Object) objects are allowed
for both keys and values.
7.
Null is allowed for keys only once.
8.
Null is allowed for values multiple times.
9.
Hashmap is Serializable because Hashmap implements a
Serializable interface.
10.
Hashmap is Clonable because Hashmap implements a Clonable
interface.
11.
Hashmap has not implemented RandomAccess.
What is the default size of Map?
-
Default size of the map is 16.
What is EntrySet?
-
A map is a group of key-value pair.Each key-value pair is
called an Entry. Set of Entry is called EntrySet.
-
Entry is an inner interface of Map Interface.
-
Inside entry actual key and value pair will be stored.
-
While iterating the map object we have to place iterator on
Entry, not on a map.
-
Without an existing Map object there is no chance of existing
Entry object.
- Please refer to the below image for entry set.
|
What is keySet?
-
A map is a group of key-value pairs. The Set of keys are
called keyset.
- Graphical representation of the key set.
|
What is HashMap?
-
HashMap is an implementation class of Map interface.
-
Underlying data structure of HashMap is Hashtable.
-
HashMap stores an object in the form of key and value
pair.
Explain the internal working of
HashMap?
-
Hashmap internally stores mapping in the form of Map.Entry
object which contains key and value.
-
Hashmap works on hashing principle.
-
We use the put() method to insert elements in hashmap and
get() method to retrieve elements from the hashmap object.
-
Internal working of inserting an element in hashmap.
1.
When we use the put() method to insert key and value objects
into a hashmap, internally hashmap uses hashing technique to identify the
bucket reference.
2.
Once the bucket is identified object is stored in Map.Entry
object in the form of key and value.
3.
It may happen multiple objects can have the same hashcode so
multiple objects will be stored in the same bucket.
4.
If one bucket has multiple objects then those objects are
stored in the form of a LinkedList.
5.
We must override hashcode() and equals() method.
6.
Hashcode() method calculates the hashcode as an integer value
and the equals() method is used to identify the key if the key is matching
then it will replace the existing element otherwise that element will be
stored next to the current element.
-
Internal working of
retrieving element in hashmap.
1.
When you want to retrieve an object from hashmap then you can
call the get() method and pass the key object.
2.
Hashmap generates the hashcode for that key and finds the
appropriate bucket.
3.
If there is only one element available in that bucket then
simply that element will be returned.
4.
If we want to retrieve an object from this linked list, we
need an extra check to search for the correct element, this is done by the
equals() method.
-
Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.HashMap; public class HashMapDemo { public static void main(String[] args) {
HashMap<Integer, String> hMap
= new
HashMap<Integer, String>();
hMap.put(1, "One");
hMap.put(10, "Ten");
hMap.put(100, "Hundred"); hMap.put(1000, "Thousand");
System.out.println(hMap);
System.out.println(hMap.get(100));
}
}
Output:
{1=One, 100=Hundred, 1000=Thousand, 10=Ten} Hundred |
|
What will happen if two different
objects have the same hashcode?
-
If the two different object have same hashcode, bucket
location would be same that time collision will occur in HashMap Since
HashMap uses LinkedList to store object internally, this entry (object of
Map.Entry contains key and value) will be stored in LinkedList.
What happen if hashmap object gets
full?
-
Default size of the hashmap is 16 and the load factor is
75%.
-
If the load factor is filled 75% then internally hashmap size
gets changed like other data structures like ArrayList, vector etc.
- Hashmap re-size itself by creating a new bucket array of size twice the previous size of hashmap and then start putting all old elements into the new bucket array this process is called rehashing.
-
Java interview questions and answers all MNC - Click here
-
Basic core java interview questions and answers for freshers
- Click here
-
Core java interview questions for 3 years experience - Click here
-
Core java interview questions and answers for 3-5 years exp
- Click here
-
Core java interview questions and Answers for 5 - 7 Years exp
- Click here
-
Basic Java Interview Questions and Answers - Click here
-
Java interview questions and answers on oops - Click here
-
Java interview questions and answers on Strings - Click here
-
Java interview questions on exception handling - Click here
-
Interview questions on multithreading in java for experienced
- Click here
-
Interview questions on serialization in java for experienced
- Click here
-
Interview questions on inner class in java for experienced
- Click here
-
Interview questions on Collections in java for experienced
- Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on
some support projects and then I moved to java projects.
I had worked with many multi-national companies like
R-Systems, Tata Consultancy Services, Cybage
Softwares. Fortunately, TCS and Cybage has
given me an opportunity to take interviews for
experienced candidates. I have conducted more than 1000
interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments