Java List | Java Tutorial For Beginners
Java List Interface is the subinterface of Collection. It contains index-based methods to insert and delete elements. It is a factory of Java List Iterator interface. Java List Interface declaration public interface List(E) extends Collection(E) Methods of Java List - void add(int index, E element) - It is used to insert the specified element at the specified position in a list. boolean add(E e) - It is used to append the specified element at the end of a list. boolean addAll - It is used to append all of the elements in the specified collection to the end of a list. boolean addAll - It is used to append all the elements in the specified collection, starting at the specified position of the list. void clear() - It is used to remove all of the elements from this list. boolean equals(Object o) - It is used to compare the specified object with the elements of a list. int hashcode() - It is used to return the hash code value for a list. E get(int index) - It is used to fetch the element from the particular position of the list. boolean isEmpty() - It returns true if the list is empty, otherwise false. int lastIndexOf(Object o) - It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element. Object[] toArray() - It is used to return an array containing all of the elements in this list in the correct order. T[] toArray(T[] a) - It is used to return an array containing all of the elements in this list in the correct order. boolean contains(Object o) - It returns true if the list contains the specified element boolean containsAll - It returns true if the list contains all the specified element int indexOf(Object o) - It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. E remove(int index) - It is used to remove the element present at the specified position in the list. boolean remove(Object o) - It is used to remove the first occurrence of the specified element. boolean removeAll - It is used to remove all the elements from the list. void replaceAll(UnaryOperatoroperator) - It is used to replace all the elements from the list with the specified element. void retainAll - It is used to retain all the elements in the list that are present in the specified collection. E set(int index, E element) - It is used to replace the specified element in the list, present at the specified position. void sort - It is used to sort the elements of the list on the basis of specified comparator. Spliterator spliterator() - It is used to create spliterator over the elements in a list. List(E) subList(int fromIndex, int toIndex) - It is used to fetch all the elements lies within the given range. int size() - It is used to return the number of elements present in the list. Java List Example :- import java.util.*; public class ListExample{ public static void main(String args[]){ List(String) al=new ArrayList(String)(); al.add("xzy"); al.add("Abc"); al.add("Sdc"); al.add(1,"Vcd"); System.out.println("An element at 2nd position: "+al.get(2)); for(String s:al){ System.out.println(s); } } } Java List Output: An element at 2nd position: Abc xzy Vcd Abc Sdc Java Training in Chennai: https://www.besanttechnologies.com/training-courses/java-training Java Training in Bangalore: https://www.besanttechnologies.com/training-courses/java-training-in-bangalore Java Interview Questions & Answers : https://www.besanttechnologies.com/java-interview-questions-answers FACEBOOK : https://www.facebook.com/BesantTechnologies/ TWITTER : https://twitter.com/besanttech LINKEDIN : https://www.linkedin.com/company/besant-technologies Instagram : https://www.instagram.com/besant_technologies/ For Java Training and Certification, Call us: +91 996 252 8293 & +91 996 252 8294 (Or) write back to us at [email protected] #JavaList #Java #JavaTutorial
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.