Found 9326 Articles for Object Oriented Programming

How to iterate a Java List using Iterator?

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

404 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?

Mahesh Parahar
Updated on 26-May-2022 13:18:37

3K+ Views

The List interface extends Collection interface and represents a collection storing a sequence of elements. 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. ArrayList is the most popular implementation of the List interface among the Java developers.Java List interface provides two methods, indexOf() method which can be used to get the location of an element in the list and lastIndexOf() method which can be used to get the last occurence of an element. In this article, we're discussing both indexOf() ... Read More

How to remove element from ArrayList in Java?

Mahesh Parahar
Updated on 26-May-2022 13:18:03

384 Views

The List interface is a member of Java Collections Framework. It extends Collection and stores a sequence of elements. ArrayList and LinkedList are the most commonly used implementations of List interface. List provides user a precise control over where an element to be inserted in the List. These elements can be accessed by their index and are searchable.The List provides two remove() methods to remove an element from list by providing the index of element or element itself.Remove using Index.E remove(int index)NotesRemoves the element at the specified position in this list.Shifts any subsequent elements to the left (subtracts one from ... Read More

How to remove all elements of ArrayList in Java?

Mahesh Parahar
Updated on 26-May-2022 13:17:24

9K+ Views

The List interface extends Collection interface and stores a sequence of elements. The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list. Unlike sets, list allows duplicate elements, allows multiple null values if null value is allowed in the list. List provides add, remove methods to add/remove elements. In order to clear a list or remove all the elements from the list, we can use clear() method of the List. We can also use removeAll() method as well to achive the same effect as clear() method.In this article, we're going ... Read More

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

Mahesh Parahar
Updated on 26-May-2022 13:15:35

17K+ Views

The List interface extends Collection interface. It is a collection that stores a sequence of elements. ArrayList is the most popular implementation of the List interface. 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 a get() method to get the element at particular index. You can specify index as 0 to get the first element of the List. In this article, we're exploring get() method usage via multiple examples.SyntaxE get(int index)Returns the element at the specified position.Parametersindex - ... Read More

How to get Sublist of an ArrayList using Java?

Mahesh Parahar
Updated on 26-May-2022 13:14:54

1K+ Views

The List interface is a Collection and it stores a sequence of elements. ArrayList is the most popular implementation of the List interface. A list provides user has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most common implementation of List interface.Java List provides a method subList() which returns the portion of the list based on start and end index provided. In this article, we'll see the usage of subList() method to get the sublist from an ArrayList.SyntaxList subList(int fromIndex, int toIndex)NotesReturns ... Read More

How to find an element in a List with Java?

Mahesh Parahar
Updated on 26-May-2022 13:14:11

17K+ Views

The List interface extends Collection interface and represents a collection storing a sequence of elements. 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. ArrayList is the most popular implementation of the List interface among the Java developers.In Java List, there are couple of ways to find an element.Use indexOf() method. - This method returns the index of the element if present otherwise -1.Use contains() method. - This methods returns true if element is present otherwise false.Loop through the elements of ... Read More

How to convert between List and Array in Java?

Mahesh Parahar
Updated on 26-May-2022 13:11:47

754 Views

The List interface is a Collection and it stores a sequence of elements. ArrayList is the most popular implementation of the List interface. A list provides user has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most common implementation of List interface.An array is a data structure which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a ... Read More

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

Mahesh Parahar
Updated on 10-May-2022 09:01:24

324 Views

An array can be converted to a List easily using multiple ways.Way #1Create an empty list. Iterate through array and add each item to list using its add method.for (int i = 0; i < array.length; i++) { list.add(array[i]); }Way #2Use Arrays.asList() method to get a list from an array.List list = Arrays.asList(array);Way #3Use Collections.addAll() method to add elements of array to the list.Collections.addAll(list, array);Way #4Use Streams to collect all elements of an array into the list.List list = Arrays.stream(array).collect(Collectors.toList());ExampleFollowing is the example showing the various methods to get a list from an array −package com.tutorialspoint; ... Read More

How to check if ArrayList contains an item in Java?

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

4K+ Views

You can utilize contains() method of the List interface to check if an object is present in the list.contains() methodboolean contains(Object o)Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).Parameterc − Element whose presence in this list is to be tested.ReturnsTrue if this list contains the specified element.ThrowsClassCastException − If the type of the specified element is incompatible with this list (optional).NullPointerException − If the specified element is null and this list does not permit null elements ... Read More

Advertisements