1. Diff between Iterator and ListIterator ?
a. Iterator can only traverse forward, but ListIterator can traverse forward and backward both.
b. Iterator can traverse both List and Set, but ListIterator can traverse only List.
2. Diff between HashMap and HashTable ?
a. HashTable is Sync, and HashMap is not.
b. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't.
c. difference is that HashMap permits null values in it, while Hashtable doesn't.
3. What is fail fast property ?
Any Java collection which implements Iterable interface to loop through it create an iterator.
Any Java collection which implements Iterable interface to loop through it create an iterator.
The iterator returned by this class's iterator method is named fail-fast if: the set is modified at any time after the iterator is created (except through the iterator's own remove method) the Iterator throws a ConcurrentModificationException.
So, in case of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
4. Why doesn't Collection extend Cloneable and Serializable?
This is a design related questions.
Many Collection implementations (including all of the ones provided by the JDK) will have a public clone method, but not for all. Because of giving a choice to developer to use appropriate collection. For example, what does it mean to clone a Collection that's backed by a terabyte SQL database? Should the method call cause the company to requisition a new disk farm? Similar arguments hold for serializable.
5. How we can make HashMap sync ?
5. How we can make HashMap sync ?
Collections.synchronizedMap
6. Where to use HashTable over HashMap ?
a. When you want to run on Java 1.1
b. When you want each operation to be synchronized (getting you a form of thread safety, so long as you never iterate over it) - and for some reason don't want to use Collections.synchronizedMap over a HashMap
c. When you don't want to be able to store null values
7. Diff between Vector and ArrayList ?
a. Vector is Sync, ArrayList is not
b. Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.
8. Diff between Enumeration and Iterator ?
Both are used to traverse Collections. Enumeration is old class present from JDK 1.0, and Iterator introduced later.
a. Iterator have one extra method remove().
b. Iterator are fail-safe and Enumeration is not.
6. Where to use HashTable over HashMap ?
a. When you want to run on Java 1.1
b. When you want each operation to be synchronized (getting you a form of thread safety, so long as you never iterate over it) - and for some reason don't want to use Collections.synchronizedMap over a HashMap
c. When you don't want to be able to store null values
7. Diff between Vector and ArrayList ?
a. Vector is Sync, ArrayList is not
b. Vector doubles the size of its array when its size is increased. But, ArrayList increases by half of its size when its size is increased.
8. Diff between Enumeration and Iterator ?
Both are used to traverse Collections. Enumeration is old class present from JDK 1.0, and Iterator introduced later.
a. Iterator have one extra method remove().
b. Iterator are fail-safe and Enumeration is not.
9. Why Java Vector class is considered obsolete or unofficially deprecated?
it is preferred by most developers that ArrayList is used in preference to Vector, and that HashMap is used in preference to Hashtable.
The main reason developers have this preference is because of speed. When it comes to Sync, it can be achieved by Collections.synchronizedXXX methods.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6201870
it is preferred by most developers that ArrayList is used in preference to Vector, and that HashMap is used in preference to Hashtable.
The main reason developers have this preference is because of speed. When it comes to Sync, it can be achieved by Collections.synchronizedXXX methods.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6201870
10. What is the difference between Sorting performance of Arrays.sort() vs Collections.sort() ? Which one is faster? Which one to use and when?
On implementation point there is no differance, as they both use the same algo. But you should use Array.sort to sort Array, becuase it will directly work on Arrays.
On implementation point there is no differance, as they both use the same algo. But you should use Array.sort to sort Array, becuase it will directly work on Arrays.
11. What is the difference between ArrayList and LinkedList?
Basic diff is in data structure used for both.
a. ArrayList use index based data-structure, LinkedList use node based data structure.
b. Reterival is fast in ArrayList than Linked List.
c. Addition, updtation and removal of elements are fast in LinkedList.
d. LinkedList has more memory overhead than ArrayList because in ArrayList each index only holds actual object (data) but in case of
LinkedList each node holds both data and address of next and previous node.
Basic diff is in data structure used for both.
a. ArrayList use index based data-structure, LinkedList use node based data structure.
b. Reterival is fast in ArrayList than Linked List.
c. Addition, updtation and removal of elements are fast in LinkedList.
d. LinkedList has more memory overhead than ArrayList because in ArrayList each index only holds actual object (data) but in case of
LinkedList each node holds both data and address of next and previous node.
12. What is ConcurrentModificationException ?
Read answer or question 3.
13. Why Map doesn't extends Collection Interface ?
Because Maps are not truly collections, as collection means bundle of elements. But in case of Map we have key-pair values.
Now what you will consider as element ? key or value ?
This is the reason Map does not extends Collection Interface.
Read answer or question 3.
13. Why Map doesn't extends Collection Interface ?
Because Maps are not truly collections, as collection means bundle of elements. But in case of Map we have key-pair values.
Now what you will consider as element ? key or value ?
This is the reason Map does not extends Collection Interface.
14. What is the Wrapper classes in collections ?
When we call Collections class static methods for getting Sync or immutable collections, it return wrapper classes(i.e with some extra functionality).
When we call Collections class static methods for getting Sync or immutable collections, it return wrapper classes(i.e with some extra functionality).
15. How to convert a collection to unmodifiable collection ?
By calling Collections.unmodifiableXXX methods.
16. How to sort list in natural reverse order?
Arrays.sort(a, Collections.reverseOrder());
17. What is WeakHashMap ?
Weak References are cleared aggressively by GC and Soft References are not. Using a Soft Reference tells the garbage collector that you would like it to keep the object around for a while, until memory considerations cause it to reclaim the object. By contrast, using a Weak References tells the garbage collector that there's no reason to keep the object around any longer than necessary.
Soft References are primarily for implementing memory sensitive caches. Weak References are primarily for associating extra information with an.
WeakHashMap is not for caching. The idea is, suppose you have a bunch of objects of a certain class that you can't extend, but you want to associate some other piece of information with each object. You can use a Map, with the main object as the key and the extra info as the value. Using a WeakHashMap for this will make sure that your Map won't cause a memory leak, because it won't hold a strong reference to the main (key) object; this will allow the object to be garbage collected when it's no longer needed. I believe that when the key is garbage collected, the value will soon be garbage collected too, though not immediately.
18. Diff between HashTable, Collections.synchronizedMap, ConcurrentHashMap ?
HashTable is thread safe Collection. And Thread lock on complete collection.
Collection.synchronizedMap is conditionaly thread safe implementation of Map.
ConcurrentHashMap only locked certain portion of Map while Hashtable lock full map while doing iteration
19. How HashMap works in Java
Refer how hash map works
20. What is identityHashMap?
Collection.synchronizedMap is conditionaly thread safe implementation of Map.
ConcurrentHashMap only locked certain portion of Map while Hashtable lock full map while doing iteration
19. How HashMap works in Java
Refer how hash map works
20. What is identityHashMap?
Ans)
The IdentityHashMap uses == for equality checking instead of equals().
This can be used for both performance reasons, if you know that
two different elements will never be equals and for preventing spoofing,
where an object tries to imitate another.
21. How Get method of HashMap works?
Refer How get method of hashmap works?
21. How Get method of HashMap works?
Refer How get method of hashmap works?
No comments:
Post a Comment