Java ListJava, a versatile and widely-used programming language, offers a robust set of data structures to handle collections of objects efficiently. One of the fundamental data structures in Java is the List interface that provides an ordered collection of elements with dynamic sizing. It is a part of Java Collections Framework, provides a versatile and ordered way to handle collections of elements. In this section, we will explore the features, implementations, and best practices associated with Java Lists. List 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 Class Methods
How to create List?The ArrayList and LinkedList classes provide the implementation of List interface. Let's see the examples to create the List: In short, we 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. Common List OperationsHere are some essential operations we can perform on Java Lists. 1. Adding Elements: 2. Removing Elements: 3. Accessing Elements: 4. Iterate Over List: Java List ExampleLet's see a simple example of List where we are using the ArrayList class as the implementation. ListExample1.java Output: Mango Apple Banana Grapes How to convert Array to ListConverting an array to a list in Java is a common operation. We can convert the Array to List by traversing the array and adding the element in list one by one using the List.add() method. It is a common approach that we use. Beside this there are some other approaches we can use as follows: Method 1: Using Arrays.asList() Method 2: Using ArrayList Constructor ArrayToListExample.java Output: 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. ListToArrayExample.java Output: 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. ListExample2.java Output: Returning element: Apple Mango Dates Banana Grapes How to Sort a List?Sorting a list in Java can be done using the Collections.sort() method or by using the List.sort() method introduced in Java 8. Here are examples for both approaches: SortArrayList.java 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 InterfaceListIteratorExample1.java Output: 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. ListExample5.java Output: 101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications and Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6 Java List MCQ1. Which of the following statements about the ListIterator interface in Java is true?
Answer: b) Explanation: The ListIterator interface provides methods for traversing a list in both forward and backward directions, allowing bidirectional access to list elements. 2. What is the difference between ArrayList and LinkedList in Java?
Answer: d) Explanation: ArrayList internally uses a dynamic array to store elements, while LinkedList uses a doubly-linked list. This fundamental difference affects their performance characteristics and suitability for different scenarios. 3. What is the time complexity of the add(int index, E element) operation in ArrayList and LinkedList respectively?
Answer: b) Explanation: In ArrayList, inserting an element at a specific index requires shifting subsequent elements, resulting in a time complexity of O(n). In LinkedList, inserting an element at a specific index can be done in constant time (O(1)) by adjusting the pointers, as it involves traversing to the desired position. 4. What is the purpose of the replaceAll(UnaryOperator <E> operator) method in the List interface?
Answer: c) Explanation: The replaceAll() method in the List interface applies the specified unary operator to each element in the list, replacing each element with the result of the operator. 5. Which method is used to obtain a portion of a list between two specified indices?
Answer: c) Explanation: The subList(int fromIndex, int toIndex) method in the List interface is used to obtain a portion of a list between the specified fromIndex (inclusive) and toIndex (exclusive). Next TopicJava HashSet class |