
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How Does a List Work in Java
public interface List<E> extends Collection<E>
The 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.
Example
The above interface has been implemented in various classes like ArrayList or LinkedList, etc. Following is the example to explain few methods from various class implementation of the above collection methods −
package com.tutorialspoint; import java.util.*; public class CollectionsDemo { public static void main(String[] args) { List<String> a1 = new ArrayList<>(); a1.add("Zara"); a1.add("Mahnaz"); a1.add("Ayan"); System.out.println("ArrayList: " + a1); List<String> l1 = new LinkedList<>(); l1.add("Zara"); l1.add("Mahnaz"); l1.add("Ayan"); l1.remove(1); System.out.println("LinkedList: " + l1); } }
Output
This will produce the following result −
ArrayList: [Zara, Mahnaz, Ayan] LinkedList: [Zara, Ayan]