More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Q112. Does an ArrayList allow elements of different types ? If not, Why the following code works
List list1 = new ArrayList<>();
list1.add(1);
list1.add("1");
Ans. With Java 7 or Later.
If you don't declare the list to be of specific type , it treats it as list of objects.
int 1 is auto boxed to Integer and "1" is String and hence both are objects.
Help us improve. Please let us know the company, where you were asked this question :
Ans. IdentityHashMap is a class that implements AbstractMap and provides a data structure with Elements having Key Value pair, just like HashMap. It is similar to HashMap except that it uses reference equality when comparing elements.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Here is the list of classes that implements Collections Interface - http://www.buggybread.com/2015/02/java-collections-classes-that-implement.html
Having Collection interface to extend Cloneable interface would mean necessarily implement clone method by all implementing classes. As not all collection classes allow duplicate elements, it makes no sense to clone elements for them.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Array List works on Array and when we add an element in middle of the list, Array List need to update the index of all subsequent elements. I the capacity is full, it even may need to move the whole list to a new memory location . Linked List works on Double linked list algorithm and all it has to do is to adjust the address of the previous and next elements.
Help us improve. Please let us know the company, where you were asked this question :
Ans. But Collections.emptyList() returns an Immutable list whereas new arraylist() creates a mutable list.
Advantage of getting an empty list using Collections.emptyList is that it returns a singleton list which can be shared among many references and hence made immutable. This is good fit for situations where we would like to initialize a list to basic minimum empty to avoid null pointer exception.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Spliterator has better performance potential than iterators but only if the potential is used. Spliterator can iterate streams in parallel as well as in sequence whereas iterator can only iterate in sequence.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Collections in java is a framework of classes that provides an abstracted data structure to store and manipulate the group of objects. Each class is unique in the way it stores , search , sort and modify the elements.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes its useless if we are not going to use its objects within Hash collection, For example - HashSet , HashMap. HashCode is used internally by these collections for Search.
Help us improve. Please let us know the company, where you were asked this question :
Ans. HashTable locks the complete collection to provide synchronization whereas ConcurrentHashMap only locks segments to achieve synchronization and hence better efficient and faster.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, they both aim at providing synchronized access to the Map collection. The only difference is in their implementation. ConcurrentHashMap came after HashTable and hence technically more efficient as it doesn't lock the complete map while accessing it.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Garbage collection mechanism frees up the memory that is no longer needed. In languages like C / C++ the deallocation needs to be done explicitly by the programmer and hence any leniency may result in memory leak. Garbage collection in java ensures that all unused memory is reclaimed and hence there are no memory leaks.Moreover it relieves the programmer from the hassle of carefully releasing all memory.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections.
Enumeration is less safer than Iterator
Enumeration is fail safe whereas Iterator is fail fast
Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method.
Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2
Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove
Help us improve. Please let us know the company, where you were asked this question :