Category: Java interview questions
-
How to Convert a Map to a List in Java?
A Map maps keys to values. Map provides following two methods to return keys and values of a map: public Collection<V> values() This method returns a Collection view of the values contained in this map. public Set keySet() This method returns a Set view of the keys contained in this map Output
-
Difference between DOM and SAX parser in Java
DOM XML parser in Java DOM stands for Document Object Model and represents an XML document in tree format, each element representing tree branches. The XML file will be loaded as a whole and all its contents will be built as an in-memory representation of the tree the document represents. Parsing XML file using DOM…
-
Difference between HashTable and HashMap in Java
Both HashMap and HashTable map keys to values. HashTable was introduced in JDK 1.0. As of JDK 1.2, this class was retrofitted to implement the Map interface. Following are the differences between HashTable and HashMap: HashMap HashTable HashMap extends java.util.AbstractMap HashTable extends java.util.Dictionary HashMap allows only one null key and any number of null values.…
-
Can a constructor be synchronized in Java?
Constructors cannot be synchronized. Using the synchronized keyword with a constructor is a syntax error – Illegal modifier for the constructor in type your class name; only public, protected & private are permitted’. Synchronizing constructors doesn’t make sense, because only the thread that creates an object should have access to it while it is being…
-
Difference between Runnable and Callable
Both Runnable and Callable interface are used to encapsulate task supposed to be executed by another thread. Runnable and Callable both run on a different thread than the calling thread. There are certain things which Runnable can not do like throwing checked exception and returning result after computation. Java team could have changed the signature…
-
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 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…
-
What is the trade-off between using an unsorted array versus a sorted array ?
The tradeoff is in terms of speed. The major advantage of a sorted array is that to search an element in sorted array is faster. Sorted array has time complexity of O(log n), compared to that of an unsorted array, which is O (n). The disadvantage of a sorted array is that the insertion operation…
