Tag: java collection interview questions
-
What is the difference between Set and Map?
Duplicate Objects Set doesn’t allow duplicates. Map doesn’t allow duplicate keys while it allows duplicate values. Null elements Set just allow one null element as there is no duplicate permitted while in Map you can have null values and at most one null key. Order Set doesn’t maintain any order. Few of its classes sort…
-
What Is RandomAccess Interface?
This interface is a member of the Java Collections Framework. This interface introduced in Java version 1.4. It marks the implementations of list which can be accessed randomly. This way a caller can either access the data randomly if that is efficient, or use another means if it is not. Marker interface used by List…
-
What is the difference between ArrayList and LinkedList?
Differences between ArrayList and LinkedList can be summarized in following points:
-
What’s the difference between Enumeration and Iterator interfaces ?
Difference between Enumeration and Iterator can be summarized with the help of following points: Both Iterator and Enumeration are available in java.util package. Both Iterator and Enumerations are unidirectional forward access cursor. According to Java API Docs, Iterator is always preferred over the Enumeration. NOTE: The functionality of this interface is duplicated by the Iterator…
-
How can we create a synchronized collection from given collection?
java.util.Collections class provides static methods to return synchroni collection backed by the specified collection. Following are the methods provided by Collections class: For example, to create synchronized list:
-
What is Collections class in Java?
This class is a member of the Java Collections Framework. This class directly extends java.lang.Object and consists static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, “wrappers”, which return a new collection backed by a specified collection. The methods of this class all throw a NullPointerException if the…
-
Which collection classes are thread-safe?
synchronized is not a class property. It is only applicable to methods and blocks. Following are the few implementations which can be safely used in multi-threaded environment: You can get a synchronized version of a Java Collection with
-
What is EnumSet?
EnumSet can be explained with following points: EnumSet example Output EnumSet allOf() and noneOf() example Output EnumSet add() example Output
-
Why there is no method like Iterator.add() to add elements to the collection?
The purpose of iterator is to traverse elements of collection. An Iterable object might be immutable. In this case iterator add method will be trying to insert an element in an immutable collection. Consider the case of HashSet and TreeSet. There is no prediction in general whether the insert will be “after” or “before” the…