Found 9321 Articles for Object Oriented Programming

How to get sublist of List in Java?

Mahesh Parahar
Updated on 26-May-2022 13:25:38

6K+ Views

The List interface extends Collection and declares the behavior of a collection that stores 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.List interface method subList() can be used to get a sublist of the list. It requires start and end index. This sublist contains the same objects as in original list and changes to sublist will also reflect in original list. In this article, we're discussing ... Read More

How to remove an element from a Java List?

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

4K+ 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.Elements can be removed from a List using multiple ways.Way #1Remove an element using its index.SyntaxE remove(int index)NotesRemoves the element at the specified position in this list.Shifts any subsequent elements to the left (subtracts one from their indices).Returns the element that ... Read More

How to iterate over a list in Java?

Mahesh Parahar
Updated on 26-May-2022 13:23:51

5K+ 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.There are multiple ways to iterate a Java List. Following are some of the ways −Way #1Use for/while loop to iterate a List and get element by index.for(int i= 0; i < list.size(); i++) {    System.out.println(list.get(i)); }Way #2Use foreach loop to iterate list ... Read More

How to iterate List Using Streams in Java?

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

17K+ Views

The List interface extends the 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, the list allows duplicate elements, and allows multiple null values if a null value is allowed in the list.You can use stream() method of the List interface which gives a stream to iterate using forEach method. In forEach method, we can use the lambda expression to iterate over all elements. The following code snippet shows the usage of streams to iterate over the list.list.stream().forEach(i -> ... Read More

How to iterate List using Iterator in Java?

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

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

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

671 Views

The List interface extends 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.A forEach loop helps in iterating an array or collection of objects. As List contains objects, it can be easily iterated using forEach loop. Following code snippet shows how to use a forEach loop to ... Read More

How to iterate a List using for Loop in Java?

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

7K+ Views

The List interface extends the 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, the list allows duplicate elements and allows multiple null values if a null value is allowed in the list.To use for loop, we need the size of the collection and indexed access to its item. The list has a size() method to give the size of the list and the get() method to get an item at a particular index. The following snippet shows how ... Read More

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

396 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

Advertisements