Guide: Java List All Methods

Introduction

In this blog we will learn about all methods used in List API.

Creating a list

This is the simplest way to create a list

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        System.out.println(nums);
    }
}
1
[10, 20, 30]

Updating the list at a given index

We can update the list a given index by using set method()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);

        nums.set(1,200);
        System.out.println(nums);
    }
}
1
[10, 200, 30]

Adding element at a given index position

For a given list we can add element at a given position. If the given index is already having a element all the elements are pushed to next index position

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);

        nums.add(1,100);
        System.out.println(nums);
    }
}
1
[10, 100, 20, 30]

Adding collection of elements at the end of the list

We can add elements of other list to the end of given list using addAll() method which takes other list as an argument.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);

        List<Integer> other = new ArrayList<>();

        other.add(100);
        other.add(200);
        other.add(300);

        nums.addAll(other);
        System.out.println(nums);
    }
}
1
[10, 20, 30, 100, 200, 300]

Adding collection of elements at a given index position

We can add collection of elements to a given list at a given index

Index should not be out of range (index < 0 || index > size())

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);

        List<Integer> other = new ArrayList<>();

        other.add(100);
        other.add(200);
        other.add(300);

        nums.addAll(1,other);
        System.out.println(nums);
    }
}
1
[10, 100, 200, 300, 20, 30]

Deleting element at a given index position

We can delete element from the list by using remove() method which takes index position as an argument

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);

        System.out.println(nums);
        System.out.println("=========AFTER DELETING===========");

        nums.remove(1);
        System.out.println(nums);
    }
}
1
2
3
[10, 20, 30, 40, 50]
=========AFTER DELETING===========
[10, 30, 40, 50]

Deleting elements based on specfied collection

Removes from a given list all of its elements that are contained in the specified collection by using removeAll() which takes collection of elements as an argument.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);

        System.out.println(nums);
        System.out.println("=========AFTER DELETING===========");

        nums.removeAll(Arrays.asList(30,50));
        System.out.println(nums);
    }
}
1
2
3
[10, 20, 30, 40, 50]
=========AFTER DELETING===========
[10, 20, 40]

Retaining elements based on specfied collection

Retains only the elements in this list that are contained in the specified collection and deletes other elements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);

        System.out.println(nums);
        System.out.println("=========AFTER RETAINING===========");

        nums.retainAll(Arrays.asList(10,20));
        System.out.println(nums);
    }
}
1
2
3
[10, 20, 30, 40, 50]
=========AFTER RETAINING===========
[10, 20]

Checking if collections of elements are present in the list

Returns true if this list contains all of the elements of the specified collection in the containsAll() argument.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);

        System.out.println(nums);
        System.out.println( nums.containsAll(Arrays.asList(10,30)));
    }
}
1
2
[10, 20, 30, 40, 50]
true

Getting index position of the element from the list

indexOf() method gives you the index position of the element in the list. If the element is not present it returns -1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);

        System.out.println(nums);

        System.out.println( nums.indexOf(40));
        System.out.println( nums.indexOf(100));
    }
}
1
2
3
[10, 20, 30, 40, 50]
3
-1

Getting number of elements in the list

size() method returns number of elements in the list

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);

        System.out.println(nums);

        System.out.println( nums.size());
    }
}
1
2
[10, 20, 30, 40, 50]
5

Getting the index of the last occurrence of the specified element in this list.

If the list has duplicate elements, lastIndexOf() returns the index of the last occurrence of the specified element in this list.

In the below example 40 is present twice. lastIndexOf() return index of the last occurence which is 5.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.ArrayList;
import java.util.List;


public class App {

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        nums.add(40);

        System.out.println(nums);

        System.out.println( nums.lastIndexOf(40));
    }
}
1
2
[10, 20, 30, 40, 50, 40]
5

I hope this guide was useful to you to understand all the required methods for using List.