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/tr...
Java Training in Bangalore:
https://www.besanttechnologies.com/tr...
Java Interview Questions & Answers :
https://www.besanttechnologies.com/ja...
FACEBOOK : / besanttechnologies
TWITTER : / besanttech
LINKEDIN : / besant-technologies
Instagram : / besant_technologies
For Java Training and Certification, Call us: +91 996 252 8293 & +91 996 252 8294 (Or) write back to us at info@besanttech.com
#JavaList #Java #JavaTutorial
In questa pagina del sito puoi guardare il video online Java List | Java Tutorial For Beginners della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Besant Technologies 06 maggio 2019, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 433 volte e gli è piaciuto 1 spettatori. Buona visione!