Found 9150 Articles for Object Oriented Programming

How to iterate over a list in Java?

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

5K+ 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

How to iterate List Using Streams in Java?

Vivek Verma
Updated on 06-Jun-2025 10:49:25

22K+ Views

In Java, a List is an interface that stores a sequence of elements of similar type. Java provides various ways to iterate over a list, such as using a for loop, forEach loop, iterator object, stream, or forEach() method with a lambda expression. What is Stream in Java? In Java, a Stream is an interface, which represents a sequence of elements (or objects) supporting sequential and parallel aggregate operations. These operations can be used to produce the desired result. The Stream interface provides a method named stream(), which is used to create a sequential stream from the current collection (or ... Read More

How to iterate List using Iterator in Java?

Mahesh Parahar
Updated on 26-May-2022 13:22:29

630 Views

The List interface extends the Collection interface. It is a collection that stores a sequence of elements. ArrayList is the most popular implementation of the List interface. The user of a list has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable.List interface provides an iterator() method which returns an Iterator to iterate the list of elements. The following code snippet shows the use of iterator.Iterator iterator = list.iterator(); while(iterator.hasNext()) {    System.out.print(iterator.next() + " "); }There is another but more flexible and powerful iterator available ... Read More

How to iterate a List using for-Each Loop in Java?

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

820 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

How to 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

How to iterate a Java List using Iterator?

Mahesh Parahar
Updated on 26-May-2022 13:21:06

514 Views

The List interface extends the Collection interface and is an important member of Java Collections Framework. List interface declares the behavior of a collection that stores a sequence of elements. The most popular implementation of List interface is ArrayList. User of a list has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. List provides two methods to efficiently add element in a list.List interface provides a method iterator() to get an Iterator instance to iterate through its elements and listIterator() method to get a more ... Read More

How to search in a List of Java object?

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

How to remove element from ArrayList in Java?

Vivek Verma
Updated on 20-May-2025 16:16:58

577 Views

The article will discuss different ways to remove elements from an ArrayList. Below is a list of methods that can help in removing elements from an ArrayList: Using the remove() Method of List Interface Using the remove(object) Method Using the clear() Method Using the removeAll() Method Removing a Single Element using the remove() Method The remove() method of the List interface accepts an integer value representing an index as a parameter and removes an element at the specified index. Following is the ... Read More

How to 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

How to get the first element of the List in Java?

Vivek Verma
Updated on 19-May-2025 14:44:54

22K+ Views

A list stores a sequence of elements of a similar type. Like an array, the elements in a List are stored at specific indices, starting from index 0. The 0th index indicates the "first element", the 1st indicates the "second" element, and so on. In Java, a list is represented by the interface named List (with the same name) that extends the Collection interface. To create a List object, we can instantiate any class that implements the List interface, such as ArrayList, Stack, Vector, etc (Since we cannot instantiate an interface). We can get the first element of the List in ... Read More

Advertisements