When to Use an Abstract Class and When to Use an Interface in Java

Shriansh Kumar
Updated on 27-May-2025 10:43:00

48K+ Views

An interface can be used to define a contract for behavior that classes must implement, and it can also act as a contract between two systems to interact. An abstract class is mainly used to define default behavior for subclasses. All child classes inherited from the abstract class can override or extend its methods. If we compare an interface and an abstract class, both can contain abstract methods and cannot be instantiated directly. Also, both are used to achieve abstraction. We may often get confused about when to use an abstract class and when to use an interface in Java.  ... Read More

Add Two Complex Numbers in Java

Shriansh Kumar
Updated on 27-May-2025 10:22:40

2K+ Views

Complex numbers are expressed in the form of p + iq, where p and q indicate real numbers and i represents imaginary numbers. For instance, 8.2 + 5.6i is a complex number, where 8.2 is the real part and 5.6i is the imaginary part. As you can see, the real number part contains an integer value (mathematical, not Java integer), and the imaginary part has an additional symbol i. In this article, we will understand how to add two complex numbers in Java. Adding Complex Numbers Since complex numbers are divided into two parts (real and imaginary), the real numbers ... Read More

Iterate Over a List in Java

Vivek Verma
Updated on 26-May-2025 20:02:38

6K+ Views

In Java if you try to print a List (or any) object directly using the print statement a hash-code of the object will be printed (default behaviour). If you want to print each element of your object (such as List, Array, etc.) or perform any operation on them one by one, you need to iterate through it. Java provides various ways to iterate through objects. Following are different approaches to iterate through a List object: Using For Loop Using While Loop Using Iterator Object Iterate over ... Read More

Search in a List of Java Objects

Vivek Verma
Updated on 26-May-2025 20:01:00

4K+ Views

A List is a sequence of elements of similar data types, and like an array, its elements are stored at specific indices, which can be accessed and searchable through the index (i.e., starting at index 0). We can search through each element in the list and retrieve its index (or position) if it is found in the list. We can search in a List in the following ways - Using indexOf() Method of List Interface Using lastIndexOf() Method Using Comparison Approach Searching in a List using ... Read More

Remove All Elements of ArrayList in Java

Vivek Verma
Updated on 26-May-2025 19:55:35

10K+ Views

In Java, the ArrayList class provides various built-in methods to remove all or a single element from it. Once those methods are called on the ArrayList, the list will become empty (). You can verify this using the size() method; it returns 0, if the list is empty. We can remove all the elements of an ArrayList in Java: Using clear() Method of List Interface Using removeAll() Method Removing all ArrayList Elements using clear() Method The clear() method of the List interface removes (deletes) all the elements from a list. ... Read More

Iterate a List Using For Loop in Java

Vivek Verma
Updated on 26-May-2025 19:48:39

9K+ Views

To iterate over a List using a for loop, we need to know the size (which can be calculated using the size() method) of the list, so that the loop can execute an accessing element code block till the last position, which is starts from index 0. Once the loop reaches at the last index of the list, it will stop executing and exit. Here are a few examples of how to iterate over a list using a for loop: Iterate a List using For Loop A for-loop is a "control flow statement " which is ... Read More

Iterate a List Using For-Each Loop in Java

Vivek Verma
Updated on 26-May-2025 19:48:11

867 Views

A List is an ordered collection or an interface that contains elements of similar types. Since the list is a collection object, we can iterate (or loop) a list using different control flow statements or an Iterator object. This article will discuss one way to (i.e., using forEach loop) iterate over a list: Iterate over a List Using forEach Loop A forEach loop is a control flow statement that helps to iterate (or loop through) over a collections object. As the List is an ordered collection, it can be easily iterated using a forEach loop or any other control flow ... Read More

Get Sublist of List in Java

Vivek Verma
Updated on 26-May-2025 19:42:36

7K+ Views

In Java, a List is an interface that stores elements of the same type. You can retrieve a sub-list from a List in Java. A sub-list is the view of a portion (or part) of the List. For example, if the given list is {a, b, c, d, e}, then the possible sub-lists can be {a, b}, {a, b, c}, {b, c, d}, {d, e}, etc. The List interface in Java provides a built-in method named subList(), which returns a sub-list from the given List. We just need to specify the range of extraction. Sublist from a List using the ... Read More

Use List Size Method in Java with Examples

Vivek Verma
Updated on 26-May-2025 19:39:42

709 Views

In Java, a List is an interface which stores a sequence of elements of similar types. Since the list contains multiple elements, we might need to know how many element the list currently have. To count the total number of elements present in a list, the List interface provides a method named size(). The List size() Method The size() method of List interface is used to retrieve size of the current List. The size refers to total number of items currently the list holds. For example, a list have {1, 2, 3}, then the size of the list will be ... Read More

Remove Element from a Java List

Vivek Verma
Updated on 26-May-2025 19:20:28

5K+ Views

In Java, List is an interface that stores a sequence of elements of similar types. Like an array, elements in the list are stored at a specific index starting at 0. Since we can access elements in the list through their index, and we can pass this index value to the remove() method (i.e., a basic method for removing elements), which will remove the element at the specified index. We can remove an element from the List in Java: Using remove() Method of List Interface Using remove(object) Method ... Read More

Advertisements