1. How does HashMap work in Java?
2. What is the difference between poll() and remove() method of Queue interface? (answer)
Though both poll() and remove() method from Queue is used to remove the object and returns the head of the queue, there is a subtle difference between them. If Queue is empty() then a call to remove() method will throw Exception, while a call to poll() method returns null. By the way, exactly which element is removed from the queue depends upon queue's ordering policy and varies between different implementation, for example, PriorityQueue keeps the lowest element as per Comparator or Comparable at head position.
3. What is the difference between fail-fast and fail-safe
Iterators?
Java 5 has added several new Concurrent Collection classes e.g.ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue etc.Java Also provided a
way to get Synchronized copy of collection e.g. ArrayList, HashMap by usingCollections.synchronizedMap() Utility
function. One Significant difference is that Concurrent Collections has better
performance than synchronized Collection because they lock only a portion of
Map to achieve concurrency and Synchronization.
This is a beginner level collection interview questions and mostly
asked during interviews of Junior Java developer up to experience of 2 to 3
years Iterator duplicate functionality of Enumeration with one addition of remove() method and both provide navigation functionally on objects
of Collection.Another difference is that Iterator is more safe than Enumeration and doesn't allow another thread to modify
collection object during iteration except remove() method and throwsConcurrentModificaitonException. See Iterator
vs Enumeration in Java for more differences.
7. How does HashSet is implemented in Java, How does it use Hashing? (answer)
This is a tricky question in Java because for hashing you need both key and value and there is no key for the store it in a bucket, then how exactly HashSetstore element internally. Well, HashSet is built on top of HashMap. If you look at source code of java.util.HashSet class, you will find that that it uses a HashMap with same values for all keys, as shown below:
private transient HashMap map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
When you call add() method of HashSet, it put entry in HashMap :
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
Since keys are unique in a HashMap, it provides uniqueness guarantee of Set interface.
9. The difference between HashMap and Hashtable?
.
This is another advanced level collection interview questions in
Java which normally asked to check whether the interviewer is familiar with
optimization done onConcurrentHashMap or
not. ConcurrentHashMap is better
suited for situation where you have multiple readers and one
Writer or fewer writers since Map gets locked only during the
write operation. If you have an equal number of reader and writer than ConcurrentHashMap will
perform in the line of Hashtable or synchronized HashMap.
11. What is the difference between Set and List in Java? (answer)
11. What is the difference between Set and List in Java? (answer)
Another classical Java Collection interviews popular on telephonic
round or the first round of interview. Most of Java programmer knows that Set
doesn't allowed duplicate while List does and List maintains insertion order
while Set doesn't. What is key here is to show the interviewer that you can
decide which collection is more suited based on requirements.
What is the
difference between Hashtable and ConcurrentHashMap in Java?
What is the
difference between Vector and ArrayList in Java?
Can you
write code to traverse Map in Java on 4 ways?
What is the
difference between ArrayList and LinkedList in Java?
How do you find
if ArrayList contains duplicates or not?
Ans: Here
are five ways we can check if an array has duplicates or not:
1) brute
force method which compares each element of Array to all other
elements and returns true if it founds duplicates. Though this is not an efficient
choice it is the one which first comes to mind.
2) Another quick
way of checking if a Java array contains duplicates or not is to convert
that array into Set. Since Set doesn’t allow duplicates size of the
corresponding Set will be smaller than original Array if Array contains
duplicates otherwise the size of both Array and Set will be same.
3) One more way to
detect duplication in java array is adding every element of the array into
HashSet which is a Set implementation. Since the add(Object obj) method of Set
returns false if Set already contains an element to be added, it can be used to
find out if the array contains duplicates in Java or not.
Which two method you need to implement for key Object in
HashMap ?
In order to
use any object as Key in HashMap, it must implements equals and hashcode method
in Java.
Comments
Post a Comment