¡SORTEO de Teclado Keychron K7 Pro!
¿Cómo participar?
→ Like y RT a este post
→ ¡Sígueme!
✓ Participación abierta a todo el mundo
✓ Envíos internacionales
Retos de programación: https://t.co/x4k5deHHcP
Java List Methods Cheatsheet. Which is your favourite?
In Java, you can work with lists using the java.util.List interface, which is implemented by various classes like ArrayList, LinkedList, and others. Here's a list of some of the most common methods you can use with Java lists:
1. Adding Elements:
- add(E e): Adds an element to the end of the list.
- add(int index, E element): Inserts an element at the specified index.
- addAll(Collection<? extends E> c): Adds all elements from a collection to the list.
2. Accessing Elements:
- get(int index): Retrieves the element at the specified index.
- indexOf(Object o): Returns the index of the first occurrence of the specified element.
- lastIndexOf(Object o): Returns the index of the last occurrence of the specified element.
3. Removing Elements:
- remove(int index): Removes the element at the specified index.
- remove(Object o): Removes the first occurrence of the specified element.
- clear(): Removes all elements from the list.
4. Checking Size and Empty:
- size(): Returns the number of elements in the list.
- isEmpty(): Checks if the list is empty.
5. Sublist and Range Operations:
- subList(int fromIndex, int toIndex): Returns a view of the portion of the list between the specified indexes.
- addAll(int index, Collection<? extends E> c): Inserts all elements from a collection into the list at the specified index.
- removeAll(Collection<?> c): Removes all elements that are in the specified collection.
6. Replacing and Setting Elements:
- set(int index, E element): Replaces the element at the specified index with the given element.
7. Iterating through Elements:
- You can use various forms of loops or the enhanced for-loop to iterate through list elements.
8. Sorting and Reversing:
- sort(Comparator<? super E> c): Sorts the elements of the list according to the provided comparator.
- Collections.sort(List<T> list): Sorts the list in natural order.
- Collections.reverse(List<?> list): Reverses the order of elements in the list.
9. Checking for Element Existence:
- contains(Object o): Checks if the list contains the specified element.
- containsAll(Collection<?> c): Checks if the list contains all elements from the specified collection.
10. Array Conversion:
- toArray(): Converts the list to an array.
- toArray(T[] a): Converts the list to an array of the specified type.
These are some of the most common methods for working with Java lists.
The specific methods available may vary depending on the concrete implementation of the List interface you are using (e.g., ArrayList, LinkedList, etc.).
🚨 Join Waiting List for New 10+ Hour Linux Course - https://t.co/EolNQecppN
#systemdesign #coding #interviewtips