Author: Editorial Team
-
Does HashMap allows null keys and null values?
HashMap allows one null key and multiple null values.
-
How to make an ArrayList read only in Java?
Sometimes there is a need to create a list which can not be modified. That is, list should not allow add, remove or set operations. Collections class provides unmodifiableCollection(Collection c) method to create unmodifiable collection. There are separate methods like unmodifiableList(List l) and unmodifiableSet(Set s) for creating unmodifiable list and set. One of the misconception to create read only ArrayList is to use Arrays.asList(T……
-
Why Map interface doesn’t extend Collection interface?
This is because the two interfaces have a very different semantics. Following points can be provided to support this:
-
Java Program to swap two numbers without using a third variable(method 1).
This approach uses addition and subtraction to swap two numbers. The logic to solve this problem is to first assign one of the two numbers with the sum of two numbers. Since, this number will now have the sum of two numbers, assign second number the difference of first number(which is now the sum of…
-
What is Thread in Java?
Thread is a lightweight process. Threads exist within a process. So every process has at least one thread. Thread in Java is represented by java.lang.Thread class. Every Java application has one or several threads. There are two ways to create thread in Java:
-
What do you understand by fail-fast and fail-safe iterator in Java?
Fail-fast iterator It is generally not permissible for one thread to modify a collection while another thread is iterating over it. There are iterator implementations which throw ConcurrentModificationException in such scenario. This type of iterators are fail-fast iterators. All general purpose collection iterator implementations provided by JRE are fail-fast. Please note the following for ConcurrentModificationException,…
-
What is the difference between Iterator and ListIterator?
1. Introduction Iterator and ListIterator are two of the three cursors in Java. Third cursor is Enumeration. Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Both Iterator and ListIterator are defined in java.util package.Iterator allows to traverse collection elements one by one and only in forward…
-
What is the benefit of Generics in Collections Framework?
In Java, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while…
-
What is an exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. When an exception occurs within a method, the method creates an exception object and passes this to runtime system. This exception object contains information about the exception, like its type. Creating an exception…
