18 Most Used Java List Methods
Essential Java List Methods: A Comprehensive Guide to 18 Frequently Used Operations
In Java, a List is an interface that extends the Collection interface, and it is part of the Java Collections Framework. It represents an ordered collection of elements, where each element has a specific index. Unlike sets, lists typically allow duplicate elements. The List interface provides methods to add, remove, access, and manipulate elements based on their position in the list.
Commonly used classes that implement the List interface include ArrayList, LinkedList, and Vector. Each implementation has its own characteristics and is suitable for different use cases.
For example, ArrayList provides fast random access but may be slower when inserting or removing elements in the middle, while LinkedList excels at frequent insertions and removals but may be less efficient for random access.
Overall, List provides a versatile and widely used data structure in Java for managing ordered collections of elements, making it easy to work with sequences of data in a program.
Here are 18 most used methods that you should know
1. add(E element) - Adds the specified element to the end of the list.
2. addAll(Collection<? extends E> c) - Adds all elements of the specified collection to the end of the list.
3. remove(Object o) - Removes the first occurrence of the specified element from the list.
4. remove(int index) - Removes the element at the specified position in the list.
5. get(int index) - Returns the element at the specified position in the list.
6. set(int index, E element) - Replaces the element at the specified position in the list with the specified element.
7. indexOf(Object o) - Returns the index of the first occurrence of the specified element in the list.
8. contains(Object o) - Returns true if the list contains the specified element.
9. size() - Returns the number of elements in the list.
10. isEmpty() - Returns true if the list contains no elements.
11. clear() - Removes all elements from the list.
12. toArray() - Returns an array containing all the elements in the list.
13. subList(int fromIndex, int toIndex) - Returns a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive.
14. addAll(int index, Collection<? extends E> c) - Inserts all elements of the specified collection into the list, starting at the specified position.
15. iterator() - Returns an iterator over the elements in the list.
16. sort(Comparator<? super E> c) - Sorts the elements of the list according to the specified comparator.
17. replaceAll(UnaryOperator<E> operator) - Replaces each element of the list with the result of applying the given operator.
18. forEach(Consumer<? super E> action) - Performs the given action for each element of the list until all elements have been processed or the action throws an exception.
Shall I clear your list? Vote now 😂
🎁 FREE 4 Hour Java Course
Thank you for sharing :)
Nice content Nelson, keep on going.