Java ListList in Java provides the facility to maintain the ordered collection. It contains the index-based methods to insert, update, delete and search the elements. It can have the duplicate elements also. We can also store the null elements in the list. The List interface is found in the java.util package and inherits the Collection interface. It is a factory of ListIterator interface. Through the ListIterator, we can iterate the list in forward and backward directions. The implementation classes of List interface are ArrayList, LinkedList, Stack and Vector. The ArrayList and LinkedList are widely used in Java programming. The Vector class is deprecated since Java 5. List Interface declarationJava List Methods
Java List vs ArrayListList is an interface whereas ArrayList is the implementation class of List. How to create ListThe ArrayList and LinkedList classes provide the implementation of List interface. Let's see the examples to create the List: In short, you can create the List of any type. The ArrayList<T> and LinkedList<T> classes are used to specify the type. Here, T denotes the type. Java List ExampleLet's see a simple example of List where we are using the ArrayList class as the implementation. Test it NowOutput: Mango Apple Banana Grapes How to convert Array to ListWe can convert the Array to List by traversing the array and adding the element in list one by one using list.add() method. Let's see a simple example to convert array elements into List. Test it NowOutput: Printing Array: [Java, Python, PHP, C++] Printing List: [Java, Python, PHP, C++] How to convert List to ArrayWe can convert the List to Array by calling the list.toArray() method. Let's see a simple example to convert list elements into array. Test it NowOutput: Printing Array: [Mango, Banana, Apple, Strawberry] Printing List: [Mango, Banana, Apple, Strawberry] Get and Set Element in ListThe get() method returns the element at the given index, whereas the set() method changes or replaces the element. Test it NowOutput: Returning element: Apple Mango Dates Banana Grapes How to Sort ListThere are various ways to sort the List, here we are going to use Collections.sort() method to sort the list element. The java.util package provides a utility class Collections which has the static method sort(). Using the Collections.sort() method, we can easily sort any List. Output: Apple Banana Grapes Mango Sorting numbers... 1 11 21 51 Java ListIterator InterfaceListIterator Interface is used to traverse the element in a backward and forward direction. ListIterator Interface declarationMethods of Java ListIterator Interface:
Example of ListIterator InterfaceOutput: Traversing elements in forward direction index:0 value:Amit index:1 value:Sachin index:2 value:Vijay index:3 value:Kumar Traversing elements in backward direction index:3 value:Kumar index:2 value:Vijay index:1 value:Sachin index:0 value:Amit Example of List: BookLet's see an example of List where we are adding the Books. Test it NowOutput: 101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications and Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6 Related TopicsDifference between ArrayList and LinkedList Difference between Array and ArrayList When to use ArrayList and LinkedList in Java Difference between ArrayList and Vector How to Compare Two ArrayList in Java How to reverse ArrayList in Java When to use ArrayList and LinkedList in Java How to make ArrayList Read Only Difference between length of array and size() of ArrayList in Java How to Synchronize ArrayList in Java How to convert ArrayList to Array and Array to ArrayList in java How to Sort Java ArrayList in Descending Order How to remove duplicates from ArrayList in Java
Next TopicJava HashSet class
|