In Java, an ArrayList is a class of the List interface, which stores elements of similar data types and can be null while creating. A sub-list is the view of a portion (or part) of an ArrayList. For example, if the given array list is {1, 2, 3, 4, 5}, then the possible sub-lists can be {1, 2}, {1, 2, 3}, {2, 3, 4}, {4, 5}, etc.The List interface in Java provides a built-in method named subList(), which directly returns a sub-list from the given ArrayList. Sublist from an ArrayList using the subList() Method The subList() method of the List interface returns a portion ... Read More
In Java, a class is a datatype which defines properties (variables) and behaviors (methods) of an object. Defining an object does not consume memory; only its object or instance does.Depending on the requirement, we will create various types of classes in Java. In this article, we are going to discuss them. Types of classes in Java The Java class is classified into different types based on its methods, as shown in the list given below: Concrete class Abstract class Final class POJO class Static class Inner Class Wrapper Class Singleton Class Concrete class Any normal class which does ... Read More
Every Java object has two very important methods, equals() and hashCode(), and these methods are designed to be overridden according to their specific general contract. Since the Object class is the parent class of every class, the default implementation of the equals() and hashCode() methods is already present in each class. However, we need to override these methods based on the requirement. Let's discuss the contract between equals() and hashCode() methods in Java. But before that, we need to understand these methods. The hashCode() Method The hashCode() method returns an integer value, which is referred to as the hash code value ... Read More
In Java, interfaces are used to achieve abstraction and multiple inheritance. They can contain methods and variables, but there are specific rules about how those members should behave. For example, all variables declared in an interface are public, static, and final by default, even if you don't use these keywords while defining variables. To understand the reason behind it, we first need to understand what static and final mean in Java. What is a Static Variable in Java? The static variables are defined using the static keyword. These variables belong to the class rather than to any specific object, which ... Read More
In Java, ArrayStoreException is a public class that extends the RuntimeException class of the java.lang package. It is thrown by the Java Virtual Machine when a runtime error occurs. Since it is an unchecked exception, it does not require an explicit declaration in a method or a constructor's throws clause. Let's understand why the ArrayStoreException is thrown and how we can avoid it. Reason for ArrayStoreException in Java As mentioned earlier, ArrayStoreException is an unchecked exception, and it can occur when we try to store an object of one type in an array of a different type. Usually, one would ... Read More
Double Order Traversal Double order traversal means each node in a tree is traversed twice in a particular order. A binary tree is a non-linear data structure where each node contains at most two children (i.e, left and right). Therefore, suppose we have a given binary tree, and the task is to find its double-order traversal. A double order traversal is a tree traversal technique in which every node traverses twice in the following order: Visit the node. Traverse the left subtree. Visit the node. ... Read More
What is Expression Tree?An expression tree is a binary tree used to represent expressions. In an expression tree, internal nodes correspond to operators, and each leaf node corresponds to an operand. Let's see an expression and construct a tree for [5 + ((4+3)*2)]. Constructing an Expression TreeOur task is to construct an expression tree from a prefix expression. We have given a character array arr[] representing a prefix expression, so we have to build an expression tree for the expression and then display the infix and postfix expressions of the created tree. Input/Output Scenario Following are the examples to ... Read More
Circular Doubly Linked ListA circular linked list is called a circular doubly linked list in which each node has two links connecting it to the previous node and the next node. In Circular Doubly Linked List two consecutive elements are linked or connected by previous and next pointer and the last node points to first node by next pointer and the first node also points to last node by previous pointer. Characteristics of Circular Doubly Linked List The following are the characteristics of the circular doubly linked list: Circular: The main feature is that ... Read More
The java.time package of Java provides a class named LocalDateTime is used to get the current value of local date and time. Using this in addition to date and time values, you can also get other date and time fields, such as day-of-year, day-of-week, and week-of-year. Setting the Local time to a column To set the local date and time value to a column in a table. Obtain the LocalDateTime object: You can obtain the LocalDateTime object by invoking the static method now() as: LocalDateTime localDateTime = LocalDateTime.now(); Get the LocalDate and LocalTime objects from the above obtained LocalDateTime as: ... Read More
Sorted Circular Doubly Linked ListA sorted circular doubly linked list is a type of circular doubly linked list in which elements are arranged in a specific order, typically ascending or descending based on the data values. The insertion operation makes sure that the new node is placed in its correct sorted position. In Circular Doubly Linked List two consecutive elements are linked or connected by previous and next pointer and the last node points to first node by next pointer and the first node also points to last node by previous pointer. Characteristics of Sorted Circular Doubly Linked List ... Read More