Found 9150 Articles for Object Oriented Programming

How to get Sublist of an ArrayList using Java?

Vivek Verma
Updated on 21-May-2025 16:49:38

2K+ Views

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

How to find an element in a List with Java?

Vivek Verma
Updated on 16-May-2025 19:20:46

24K+ Views

In Java, a List is an interface that extends the Collection interface and represents a sequence of elements. Since the List is an interface. To create a List object, we need to instantiate a class that implements the List interface, such as ArrayList.  The List provides various methods that help to check or find the element in it. We will discuss those methods in the coming section with suitable examples. Below is a list of various ways to find an element in the Java List: Using the get() Method Using ... Read More

How to convert between List and Array in Java?

Vivek Verma
Updated on 27-May-2025 14:31:46

1K+ Views

In Java, a List is an interface that represents an ordered collection of elements of the same type. You cannot directly create an object of the List interface, instead, you need to instantiate an ArrayList class or another class that implements the List interface.An array is a container that holds a fixed size of similar types of data. As the size is fixed, it can not be changed once created.The conversion between List and array is important because array provides faster access to the elements than List interface (faster in case of searching elements), and provides a fixed size of ... Read More

How to convert an Array to a List in Java program?

Vivek Verma
Updated on 27-May-2025 14:23:13

662 Views

In Java, an array is a container object that holds a similar type of data (elements), but has a fixed size. You can declare it by assigning values in curly braces or creating it using the new keyword by specifying the size. A List in Java is an interface that represents a sequence of elements of the same type (duplicates are allowed) and has a dynamic size. To create an object of the List interface, you can use one of its classes, such as ArrayList, Stack, Vector, etc.Converting an Array to a List in JavaSometimes we need to insert more data ... Read More

How to check if ArrayList contains an item in Java?

Vivek Verma
Updated on 19-May-2025 15:27:41

5K+ Views

To check if an ArrayList contains a specific item, we can either compare each element with the given item and print the result if they are equal, or use a predefined method that directly checks for the containing element. ArrayList in Java In Java, an ArrayList is a class similar to an Array, but unlike an array, it has a dynamic size that automatically increases or decreases, depending on the number of elements added or removed from it. Following is the syntax for creating an ArrayList in Java - ArrayList list_name = new ArrayList(); Here, ... Read More

How does a list work in Java?

Mahesh Parahar
Updated on 10-May-2022 08:42:04

209 Views

public interface List extends CollectionThe List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.Elements can be inserted or accessed by their position in the list, using a zero-based index.A list may contain duplicate elements.In addition to the methods defined by Collection, List defines some of its own, which are summarized in the following table.Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.ExampleThe above interface has been implemented in various classes like ArrayList or LinkedList, ... Read More

How do you turn an ArrayList into a Set in Java?

Aishwarya Naglot
Updated on 30-May-2025 18:10:34

11K+ Views

In this article, let's learn how to convert an ArrayList into a Set in Java. ArrayList is a collection that allows us to store duplicates, whereas Set is a collection that does not allow duplicates. So, when we convert an ArrayList to a Set, all the duplicate elements will be removed. If someone tries to add duplicate elements to a Set, it will not throw an error but will simply ignore the duplicate elements. Let's take an example: Input: [1, 2, 3, 4, 5, 5, 6] Output: [1, 2, 3, 4, 5, 6] Ways to Convert an ArrayList to ... Read More

How do you search for an element in an ArrayList in Java?

Aishwarya Naglot
Updated on 30-May-2025 18:29:25

7K+ Views

In this article, we will learn how to search for an element in an ArrayList in Java. ArrayList is an interface that extends another interface called the List interface. It provides a way to store elements and also provides resizable functionality. Let's take an example: Input: [1, 2, 3, 4, 5] Element to search: 3 Output: Element 3 found at index 2 Ways to Search for an Element in an ArrayList Below are the different approaches to searching for an element in an ArrayList: Using indexOf() method Using contains() method Using for loop Using Stream API ... Read More

How do you make a shallow copy of a list in Java?

Aishwarya Naglot
Updated on 30-May-2025 18:35:52

922 Views

In this article, we will learn how to make a shallow copy of a List in Java. What is the List in Java? List is a part of the Java collection framework. It stores elements sequentially and also allows us to store duplicate elements. It is an interface, not a class, so it cannot be instantiated directly. However, there are several classes that implement the List interface, such as ArrayList, LinkedList, and Vector. Shallow Copy of a List A shallow copy means it is a copy of the original list, but elements in the copied list are still referencing the ... Read More

How do you make a list iterator in Java?

Aishwarya Naglot
Updated on 05-Jun-2025 12:29:16

365 Views

List Iterators in Java are mainly useful for traversing a list in both directions (forward and backward). They allow you to iterate through the elements of a list while providing methods to modify the list during iteration. In this article, we will learn how to create or make a list iterator in Java. Ways to Create a List Iterator in Java Using ListIterator Interface Using the Iterator Interface Using Stream API Using ListIterator Interface We can utilize listIterator() method of the List interface, which allows element ... Read More

Advertisements