Category: Java interview questions
-
Can we make an array volatile in Java?
Before answering this question, let us see two important points about volatile variable. Now, let us answer our original question, Can we make an array volatile in Java? Yes, we can make an array volatile in Java but only the reference variable pointing to the array will be volatile not the elements of the array.…
-
map() vs flatMap() in Java
map() and flatmap() methods are found in Optional class and Stream interfaces in Java 8. We’ll discuss the use of both these methods with respect to Optional and Stream. 1. flapMap() and map() methods in Optional 1.1 Syntax of map() <U> Optional<U> map(Function<? super T, ? extends U> mapper) 1.2 Syntax of flatMap() <U> Optional<U>…
-
Difference between Streams and Collections in Java
A collection is a group of elements. A collection is an in-memory data structure. Collection supports various operations such as sorting, searching, insertion, manipulation and deletion. In Java, collection framework provides many interfaces like Set, List, Queue and implementation classes like ArrayList, HashSet to represent different type of collections. Stream is basically a sequence of…
-
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: