What is List interface in java

The List interface is a sub interface of Collection under java.util package . it’s index stars from zero (0) . The diagram represents, List interface and it’s implementations .
List is an ordered collection , it allows both duplicates and null elements .
we can search elements by index .
It is not a synchronized collection ,
Use Collections.synchronizedList(List list)for synchronized List .
All methods of List interface :
![]() |
Syntax:
List ‹generic type› ‹object name›= new <List implemented class name>‹generic type›(); or |
Example for List interface :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.List; import java.util.ArrayList; class ListExample { public static void main(String[] args) { List<Object> listVal = new ArrayList<object>(); listVal.add("Cat"); listVal.add("Dog"); listVal.add("Mouse"); listVal.add("Donkey"); System.out.println("List elements are :"+listVal); //get(int) method System.out.println("Get by index :"+listVal.get(2)); //indexOf(Object) method returns int System.out.println("Index of the element :"+listVal.indexOf("Dog")); //isEmpty() returns boolean System.out.println("isEmpty() :"+listVal.isEmpty()); //lastIndexOf() returns int System.out.println("lastIndex of element :"+listVal.lastIndexOf("Donkey")); //set(int,element) method listVal.set(3,"Hen"); System.out.println("After replace element :"+listVal); //size() method returns int System.out.println("Size of the List :"+listVal.size()); //subList(int,int) method returns List List subList = listVal.subList(1,3); System.out.println("SubList :"+subList); //remove(index or element) method listVal.remove(2); System.out.println("After removed :"+listVal); } } |
1 2 3 4 5 6 7 8 9 |
List elements are :[Cat, Dog, Mouse, Donkey] Get by index :Mouse Index of the element :1 isEmpty() :false lastIndex of element :3 After replace element :[Cat, Dog, Mouse, Hen] Size of the List :4 SubList :[Dog, Mouse] After removed :[Cat, Dog, Hen] |
Few more methods :
Method Name | Returns | Description |
addAll(Collection) | Boolean | To add Collection of elements ( if they are not present). |
containsAll(Collection) | Boolean | Returns true , if the List having all elements. |
iterator() | Iterator | To iterate List . |
removeAll(Collection) | boolean | To Remove specified Collection of elements . |
retainAll(Collection) | boolean | To Keep specified Collection of elements . |
toArray() | Object[] | It returns an array with all elements . |
toArray(T[] t) | T[] | It returns specified array(ex:String[]) ,with all elements . |
listIterator() | ListIterator | To iterate List . |
Some of the List implementations prohibits to add null elements .

Related Posts : |
![]() |
What is an interface and how to work with an interface in java |
![]() |
What is the difference between interface and abstract class in java |
![]() |
What is Map interface in java |
![]() |
What is Set interface in java |
![]() |
Difference between List and Set |