Home > DeveloperSection > Articles > List in java

List in java

Java Java 
0 Comment(s)
 600  View(s)
Rate this:

List in java

The List interface extends Collection and declares the behaviour 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.

·         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.


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


List<String> myList=new ArrayList( );

// store only String type value

myList.add(" Data1");

myList.add(" Data2");

myList.add(" Data3");


List mList=new ArrayList( );

// store any type value


mList.add(" Data1");

mList.add(" Data2");

mList.add(" Data3");


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");





Insert value in list

Syntax -: listObj.add(Value);



Update value in particular index

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

                  myList.set(4, "Update");


Find value from index

Syntax-: listobj.get(int index);



Remove value from index

Syntax-: listobj.remove(int index);



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);







     public static void insertData(String 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()) {


                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) {


                System.out.println("Data Deleted.");

           } else

                System.out.println("Data not found for delete.");








Don't want to miss updates? Please click the below button!

Follow MindStick