• May 5, 2024
Java Coding

Java Lists are a fundamental part of Java Collections, providing a dynamic, resizable array-like structure to store elements. With their versatility, Lists offer a wide range of methods to manipulate and retrieve data. In this post, we’ll explore the 20 most commonly used Java List methods that every Java developer should be familiar with.

1. add(E element):

The add method is used to insert an element at the end of the list.

   List<String> myList = new ArrayList<>();
   myList.add("Hello");

2. addAll(Collection c):

This method appends all elements from the specified collection to the end of the list.

   List<String> myList = new ArrayList<>();
   List<String> anotherList = Arrays.asList("Java", "World");
   myList.addAll(anotherList);

3. remove(Object o):

Removes the first occurrence of the specified element from the list.

   List<String> myList = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange"));
   myList.remove("Banana");

4. get(int index):

Retrieves the element at the specified index.

   List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
   String language = myList.get(0); // Retrieves "Java"

5. indexOf(Object o):

Returns the index of the first occurrence of the specified element.

   List<String> myList = Arrays.asList("Apple", "Banana", "Orange");
   int index = myList.indexOf("Banana"); // Returns 1

6. size():

Returns the number of elements in the list.

   List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
   int size = numbers.size(); // Returns 5

7. clear():

Removes all elements from the list.

   List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
   myList.clear(); // Empties the list

8. isEmpty():

Checks if the list is empty.

   List<String> myList = new ArrayList<>();
   boolean empty = myList.isEmpty(); // Returns true

9. contains(Object o):

Returns true if the list contains the specified element.

   List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
   boolean containsJava = myList.contains("Java"); // Returns true

10. addAll(int index, Collection c):

Inserts all elements in the specified collection into the list at the specified position.

List<String> myList = new ArrayList<>(Arrays.asList("Java", "Python"));
List<String> anotherList = Arrays.asList("JavaScript", "C++");

myList.addAll(1, anotherList);

11. remove(int index):

Removes the element at the specified index.

```java
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
numbers.remove(2); // Removes the element at index 2
```

12. set(int index, E element):

Replaces the element at the specified index with the specified element.

```java
List<String> myList = new ArrayList<>(Arrays.asList("Java", "Python", "JavaScript"));
myList.set(1, "C++"); // Replaces "Python" with "C++"
```

13. subList(int fromIndex, int toIndex):

Returns a view of the portion of the list between the specified `fromIndex` (inclusive) and `toIndex` (exclusive).

```java
List<String> myList = Arrays.asList("Java", "Python", "JavaScript", "C++");
List<String> sublist = myList.subList(1, 3); // Returns ["Python", "JavaScript"]
```

14. toArray():

Returns an array containing all elements in the list.

```java
List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
String[] array = myList.toArray(new String[0]);
```

15. toArray(T[] a):

Returns an array containing all elements in the list; the runtime type of the returned array is that of the specified array.

```java
List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
String[] array = myList.toArray(new String[myList.size()]);
```

16. containsAll(Collection c):

Returns true if the list contains all elements of the specified collection.

```java
List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
List<String> checkList = Arrays.asList("Java", "JavaScript");
boolean containsAll = myList.containsAll(checkList); // Returns true
```

17. removeAll(Collection c):

Removes all elements from the list that are present in the specified collection.

```java
List<String> myList = new ArrayList<>(Arrays.asList("Java", "Python", "JavaScript"));
List<String> removeList = Arrays.asList("Python", "JavaScript");
myList.removeAll(removeList); // Removes "Python" and "JavaScript"
```

18. retainAll(Collection c):

Retains only the elements in the list that are contained in the specified collection.

```java
List<String> myList = new ArrayList<>(Arrays.asList("Java", "Python", "JavaScript"));
List<String> retainList = Arrays.asList("Python", "JavaScript", "C++");
myList.retainAll(retainList); // Retains "Python" and "JavaScript"
```

19. iterator():

Returns an iterator over the elements in the list in proper sequence.

```java
List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
Iterator<String> iterator = myList.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}
```

20. listIterator():

Returns a list iterator over the elements in the list (in proper sequence).

```java
List<String> myList = Arrays.asList("Java", "Python", "JavaScript");
ListIterator<String> listIterator = myList.listIterator();
while (listIterator.hasNext()) {
    System.out.println(listIterator.next());
}
```

In conclusion, mastering these essential List methods will empower you to efficiently manipulate and navigate through your data structures in Java. Whether you are a beginner or an experienced developer, these methods are the building blocks for effective Java programming. Happy coding!

Leave a Reply