List Interface – Special Article 01

Hello guys, as I promised you, today I am bringing you a special article about List Interface in Java. Since we have covered all 3 classes that implement List interface in Java Collections Framework, I thought of giving you guys kind of a summary to help to memorize the concepts better. Let’s get into it.

The 3 classes that implement List Interface in Java Collections Framework are,

  • ArrayList
  • LinkedList
  • Vector

First of all, let’s get some general idea about the List Interface in Java.

  • Lists can contain duplicate elements
  • The elements in the lists can be accessed through a zero-based index.
  • List Interface extends Collection in Java

Let’s learn about the methods that List Interface provides. So, since ArrayList, LinkedList and Vector classes implement List Interface, all of these methods can be used when we are using these 3 classes.

I am going to use ArrayList class to provide examples

1. void add (int index, Object obj)

The object gets added to the specified index in the list. If there is an object in the specified index already, that element is pushed to the next position.

Take a look at this code snippet.

        //Creating an ArrayList
        ArrayList myArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");

        System.out.println(myArrayList);
        myArrayList.add(3, "Rachel");
        System.out.println(myArrayList);

Here is the console output :

[Joey, Chandler, Ross, Monica]
[Joey, Chandler, Ross, Rachel, Monica]
2. void addAll(int index, Collection c)

Here we can add data in some other list into our ArrayList. We can specify an index as from where to start adding the data in the list. If we don’t specify an index, the data will be added to the end of the list. Here is an example.

        //Creating ArrayLists
        ArrayList myArrayList = new ArrayList();
        ArrayList newArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");

        newArrayList.add("Rachel");
        newArrayList.add("Pheobe");

        myArrayList.addAll(newArrayList);
        System.out.println(myArrayList);

Here is the console output :

[Joey, Chandler, Ross, Monica, Rachel, Pheobe]
3. Object get(int index)

Returns the element in the specified index of the list

        //Creating a vector object
        ArrayList myArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");

        System.out.println(myArrayList.get(2));

Here is the console output :

Ross
4. int indexOf (Object obj)

This returns the index of the specified element if it exists. It returns -1 if the element does not exist.

        //Creating a vector object
        ArrayList myArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");

        System.out.println(myArrayList.indexOf("Chandler"));
        System.out.println(myArrayList.indexOf("Rachel"));

Here is the console output :

1
-1
5. int lastIndexOf(Object obj)

Returns the last index of the specified object.

        //Creating an ArrayList
        ArrayList myArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");
        myArrayList.add("Chandler");

        System.out.println(myArrayList);
        System.out.println(myArrayList.lastIndexOf("Chandler"));

Here is the console output :

[Joey, Chandler, Ross, Monica, Chandler]
4
6. Object remove(int index)

This removes the element in the specified index and returns true if the removal process was done successfully.

        //Creating an ArrayList
        ArrayList myArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");
        myArrayList.add("Rachel");

        System.out.println(myArrayList);
        System.out.println(myArrayList.remove("Ross"));
        System.out.println(myArrayList);

Here is the console output :

[Joey, Chandler, Ross, Monica, Rachel]
true
[Joey, Chandler, Monica, Rachel]
7. Object set(int index, Object obj)

This updates the element in the specified index with the given object. And it returns the object that is currently in the specified index.

        //Creating an ArrayList
        ArrayList myArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");
        myArrayList.add("Rachel");

        System.out.println(myArrayList);
        System.out.println(myArrayList.set(2,"Pheobe"));
        System.out.println(myArrayList);

Here is the console output :

[Joey, Chandler, Ross, Monica, Rachel]
Ross
[Joey, Chandler, Pheobe, Monica, Rachel]
8. List subList(int start, int end)

This returns a list from the specified starting index to the specified ending index(Excluding the element in the starting index).

        //Creating an ArrayList
        ArrayList myArrayList = new ArrayList();

        //Adding Elements
        myArrayList.add("Joey");
        myArrayList.add("Chandler");
        myArrayList.add("Ross");
        myArrayList.add("Monica");
        myArrayList.add("Rachel");
        myArrayList.add("Pheobe");

        System.out.println(myArrayList);
        System.out.println(myArrayList.subList(2,4));

Here is the console output :

[Joey, Chandler, Ross, Monica, Rachel, Pheobe]
[Ross, Monica]

This ends our special article about List Interface in Java. I hope you guys have a much clearer understanding now. If you enjoyed the article, drop a like and follow my blog. I’ll see you guys next time. Stay Safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: