Part 9
Interview questions on Collections in java for experienced covered in this post:
How null
key is handled in HashMap?
-
Hashmap handles the different ways to handle null keys while
putting and retrieving.
-
Null key always map to the 0th index.
-
There are a couple of separate methods for that,
putForNullKey (V value)
and
getForNullKey ().
-
Equals() and hashcode() methods are not used in the case of
null keys.
-
For getting value by null key it also has a method
getForNullKey
which it calls from inside the get method.
-
Please refer to the below example.
private V getForNullKey() {
if (size == 0) {
return null;
}
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
} |
What is the time Complexity for map.get()?
-
The time complexity of hashmap for get operation and put
operation is generally O(1).
-
O(1) certainly isn't guaranteed - but it's usually what you
should assume when considering which algorithms and data structures to
use.
What is LinkedHashMap? Can you explain internal working of
LinkedHashMap?
-
LinkedHashMap is a data structure that preserves the insertion
order and stores the objects in the form of key/value pair like a Hashmap.
-
LinkedHashMap is a child class of Hashmap and implements a Map
interface.
-
LinkedHashMap is not a thread-safe.
-
It can contain only one null key and multiple null values.
-
It contains only unique elements.
-
Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.LinkedHashMap; public class LinkedHashMapDemo { public static void main(String[] args) {
LinkedHashMap<Integer, String> linkHm
= new
LinkedHashMap<Integer, String>();
linkHm.put(100, "One Hundred");
linkHm.put(200, "Two Hundred");
linkHm.put(300, "Three Hundred");
linkHm.put(500, "Five Hundred");
linkHm.put(200, "Two Hundred");
linkHm.put(100, "One Hundred");
linkHm.put(null, "NULL Key");
linkHm.put(600, null);
linkHm.put(700, null);
System.out.println(linkHm);
}
}
Output: {100=One Hundred, 200=Two Hundred, 300=Three Hundred, 500=Five
Hundred, null=NULL Key, 600=null, 700=null} |
What is WeakHashMap?
-
In the case of hashmap even though the object doesn’t have any
reference still it is not eligible for garbage collection. If it is associated
with HashMap i.e. Hashmap dominates garbage collector.
-
In the case of WeakHashMap if the object doesn’t contain any
reference is eligible for garbage collection. Even though the object is
associated with WeakHashMap. Garbage collector dominates WeakHashMap.
- Please refer to the below example.
package simplifiedjava.crackedInterview; public class Temp {
public
String toString() {
return
"temp"; }
public
void
finalize() {
System.out.println("Finalize() method called.");
} } // Demo for HashMap package simplifiedjava.crackedInterview; import java.util.HashMap; public class HashMapDemoForWeaknessCompare {
public
static
void
main(String[] args) throws
InterruptedException {
HashMap m
= new
HashMap();
Temp t
= new
Temp();
m.put(t, "Testing");
System.out.println(m);
t
= null;
System.gc();
Thread.sleep(5000);
System.out.println(m);
}
}
Output:
{temp=Testing} {temp=Testing} // Demo for WeakHashMap package simplifiedjava.crackedInterview; import java.util.WeakHashMap; public class WeakHashMapDemo {
public
static
void
main(String[] args) throws
InterruptedException {
WeakHashMap m
= new
WeakHashMap();
Temp t
= new
Temp();
m.put(t, "Testing");
System.out.println(m);
t
= null;
System.gc();
Thread.sleep(5000);
System.out.println(m);
}
}
Output:
{temp=Testing}
Finalize() method called. {}
Please cross verify the output for both HashMap and WeakHashMap. In
case of WeakHashMap finalize() method has been called means garbage
collections performed his task and second time we are not able to get
the object which has evacuated by GC. |
What is IdentityHashMap?
-
In the case of IdentityHashMap JVM will use “==” to identify
duplicate keys which are meant for reference comparison.
-
For normal HashMap, JVM will use the .equals() method to
identify duplicate keys which is meant for content comparison.
-
Please refer couple of examples below.
package simplifiedjava.crackedInterview;
import
java.util.HashMap; import java.util.IdentityHashMap; public class HashMapDemoForIdentityHashMap { public static void main(String[] args) {
HashMap<Integer, String> hm
= new
HashMap<Integer, String>();
hm.put(new
Integer(10), "Yogesh");
hm.put(new
Integer(10), "Arpita");
System.out.println(hm);
IdentityHashMap<Integer, String> ihm
= new
IdentityHashMap<Integer, String>();
ihm.put(new
Integer(10), "Yogesh");
ihm.put(new
Integer(10), "Arpita");
System.out.println(ihm);
}
}
{10=Arpita}
{10=Yogesh, 10=Arpita} |
What is SortedMap? How objects will be stored in SortedMap bydefault?
-
SortedMap is a child interface of Map interface.
-
The map is ordered according to the natural sorting order of
its keys but not the values.
-
All keys inserted into a sorted map must implement the
Comparable interface (or be accepted by the specified comparator).
-
TreeMap is the implementation class of the SortedMap
interface.
-
Please refer to the below example. The reference is SortedMap
and its implementation is TreeMap.
package simplifiedjava.crackedInterview;
import
java.util.SortedMap; import java.util.TreeMap; public class SortedMapDemo {
public
static
void
main(String[] args) {
SortedMap<Integer, String>
sMap
= new
TreeMap<Integer, String>();
sMap.put(1000, "Thousand");
sMap.put(1, "One");
sMap.put(10000, "Ten Thousand");
sMap.put(100, "Hundred");
sMap.put(10, "Ten");
System.out.println(sMap);
}
}
Output:
{1=One, 10=Ten, 100=Hundred, 1000=Thousand, 10000=Ten
Thousand} |
-
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