Java’s final keyword is a modifier that can be applied to classes, methods, and variables. When applied to a class, it means that the class cannot be subclassed. When applied to a method, it means that the method cannot be overridden in a subclass. When applied to a variable, it means that the variable’s value cannot…
Category: Collections
Fail Fast Vs Fail Safe Iterator In Java
Fail fast iterator while iterating through the collection , instantly throws Concurrent Modification Exception if there is structural modification of the collection. Fail Safe Iterator makes copy of the internal data structure (object array) and iterates over the copied data structure.Any structural modification done to the iterator affects the copied data structure. Fail Fast Iterator Fail Safe Iterator…
Iterable interface in Java
The Iterable Interface is defined in java.lang package. The Iterable interface is the super interface in collection framework. It is the root interface for all the collection classes. The Collection interface extends the Iterable interface. So,all the subclasses that implementing the Collection interface also implement the Iterable interface. Implementing this interface allows an object to be the target of the enhanced “for each” statement. The for-each loop is used…
Iterator vs ListIterator in Java
Iterator ListIterator Can traverse all the collections including set, map, etc. It can be used to traverse only list type collection like ArrayList, LinkedList. Iterates the collection only in the forward direction. Can iterate over the collection in forward as well as backward direction. Cannot obtain indexes. Can obtain indexes. No way to add new elements…
Top 5 frequently asked questions on Iterator Interface in Java
Q #1) What is the Iteration in Java? Answer: An iteration is a process by which a code block is repeatedly executed until a given condition holds or doesn’t exist. Using iteration you can traverse through a sequence of elements or process the data. Q #2) How many types of Iterators are there in Java? Answer: Iterators are…
Iterable vs Iterator interfaces in Java.
Iterable Interface Iterator Interface Represents a collection that can be traversed using foreach loop. Allows to iterate over some other collection. The class that implements the iterable interface has to override iterator() method. hasNext() and next() methods of Iterator interface are to be overridden by class implementing it. Does not store the current state. Stores the…
Iterator Interface in Java
In Java, an Iterator is a construct that is used to traverse or step through the collection. In order to use an Iterator, you need to get the iterator object using the iterator() method of the collection interface. Java Iterator is a collection framework interface and is a part of the java.util package. Using Java Iterator we…
Comparable vs Comparator interface in Java
Comparable and Comparator both are interfaces and can be used to sort collection elements. There are many differences between Comparable and Comparator interfaces that are mentioned as follows: Comparable in Java Comparator in Java Comparable interface is used to sort the objects with natural ordering. Comparator in Java is used to sort attributes of different objects.…
HashMap in Java
Here is the list of methods available in HashMap class. I have also covered examples using these methods at the end of this post. void clear(): It removes all the key and value pairs from the specified Map. Object clone(): It returns a copy of all the mappings of a map and used for cloning them…
Comparator interface in Java
Java Comparator interface is used to order the objects of a user-defined class. This interface is found in java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element). It provides multiple sorting sequences, i.e., we can sort the elements on the basis of any data member. This interface is part of java.util package. We can use…