---
title: "List in java"  
description: "In this article I am telling you to use of List in java"  
author: "Manoj Pandey"  
published: 2015-04-13  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1719/list-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# List in java

The List [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) extends [Collection](https://www.mindstick.com/articles/1718/collections-in-java) and declares the behaviour of a collection that stores a [sequence](https://www.mindstick.com/interview/34504/difference-between-identity-vs-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](https://www.mindstick.com/forum/158773/write-a-function-to-remove-duplicate-elements-from-a-list-in-python).

· In addition to the [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) defined by Collection.

· Several of the list methods will throw an [UnsupportedOperationException](https://www.mindstick.com/forum/159251/how-to-handle-the-unsupportedoperationexception-in-java) if the collection cannot be modified, and a [ClassCastException](https://www.mindstick.com/forum/159244/what-is-the-classcastexception-in-java-and-how-can-it-be-avoided) is generated when one object is incompatible with another.

##### We can define List data type which data type will be store in the list.

I.e.

```
List<String> myList=new ArrayList( );// store only String type valuemyList.add(" Data1");myList.add(" Data2");myList.add(" Data3");or List mList=new ArrayList( );// store any type value mList.add(" Data1");mList.add(" Data2");mList.add(" Data3");
```

![List in java](https://www.mindstick.com/mindstickarticle/5483e4cb-bdf8-452e-b820-283705cb5e74/images/2fbeb0a4-b2e0-45d5-94c8-362961c1fde2.png)

In List class we can add several types of data but List<e> interface we can add only e type data for example

```
List mList=new ArrayList( );mList.add(" Data1");mList.add(123);mList.add('C');mList.add(3.63);
```

##### Insert value in list

**Syntax -:**listObj.add(Value);

myList.add("MyData");

Update value in particular index

Syntax -: listObj.set(int index, Value;

myList.set(4, "Update");

##### Find value from index

##### \

Syntax-: listobj.get(int index);

##### myList.get(2);

##### Remove value from index

Syntax-: listobj.remove(int index);\
myList.remove(2);

##### Get index of Object

Syntax-: listobj.indexOf(Object);\
int index=myList.indexOf("Data2");\
If record found then return index position otherwise return -1.

Here I am creating example if List sample for insert, manipulation, deletion etc.

```
import java.util.ArrayList;import java.util.List; public class Demo {     static List<String> myList;      public static void main(String args[]) {            myList = new ArrayList<String>();           // insertData("Data1");            for (int i = 1; i < 11; i++) {                insertData("Item" + i);            }           updateList("Data10", 10);           DeleteByData("Data10");           DeleteByIndex(9);           System.out.print(myList.toString());      }      public static void insertData(String data) {           myList.add(data);           // System.out.println("Data Inserted.");      }      public static void updateList(String data, int index) {           index = index - 1;           if (index < myList.size()) {                myList.set(index, data);                System.out.println("Data Updated.");           } else    System.out.println("You have not enough data index for update.");     }      public static void DeleteByIndex(int index) {           index = index - 1;           if (index < myList.size()) {                myList.remove(index);                System.out.println("Data Deleted.");           } else  System.out.println("You have not enough data index for delete.");      }      public static void DeleteByData(String data) {           int index = myList.indexOf(data);           if (index >= 0) {                myList.remove(index);                System.out.println("Data Deleted.");           } else                System.out.println("Data not found for delete.");     }} 
```

##### Output

![List in java](https://www.mindstick.com/mindstickarticle/5483e4cb-bdf8-452e-b820-283705cb5e74/images/f3803bf2-84b4-4daa-ac99-c52ae4a2ed54.png)

---

Original Source: https://www.mindstick.com/articles/1719/list-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
