List Interface in Java With Example and Benefits

(Last Updated On: April 13, 2019)

Learn what is list interface in java with example. Also, learn BENEFITS of using List interface with a scenario example.

Java List Interface and Example

List interface is a child interface of collection. It is implemented by classes like ArrayList, LinkedList and Vector etc.

 

Code: List interface with ArrayList class

In below program, we have created an object of class ArrayList  and assigned the object into a List interface reference. By using the list interface reference variable, we are adding the String values i.e. name of days.

public class Sample {

	public static void main(String[] args) {

		List list = new ArrayList();
		list.add("Sunday");
		list.add("Monday");
		list.add("Tuesday");
		list.add("Wednesday");
		list.add("Thursday");
		list.add("Friday");
		list.add("Saturday");

		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}
}

 

Code – List interface with other classes

In the above code just, you can change the object of ArrayList to LinkedList class or Vector class etc. Rest of the statements i.e. add method you don’t have to change.

For example,

List interface with LinkedList:

List<String> list = new LinkedList<String>();

List interface with Vector class :

List<String> list = new Vector<String>();

 

NOTE:

Don’t forget to import below items, whatever you are using in the program or else there would be error.

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

 

Benefits of using List interface

List interface provide flexible and maintainable code.

Consider the below program.

The class “Print” print() method is accepting an ArrayList and printing its elements.

 

class Print {
	public static void print(ArrayList list) {
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}
}

public class Sample {

	public static void main(String[] args) {

		ArrayList list = new ArrayList();
		list.add("Sunday");
		list.add("Monday");
		list.add("Tuesday");
		list.add("Wednesday");
		list.add("Thursday");
		list.add("Friday");
		list.add("Saturday");

		Print.print(list);
	}
}

 

Now, what if we want to send LinkedList or Vector objects to print() method of Print class to print the elements.

If you send LinkedList, you need to modify the method argument of

print(ArrayList<String> list) as

print(LinkedList<String> list) as

 

If want to send Vector then, we need to modify again as

print(Vector<String> list);

Isn’t it a pain to modify the Print class over and over?

 

Solution 

Solution to the problem is to use List interface. So, the Print class is not required to change over and over. When we use the list interface as below in the print method, it can receive any object like LinkedList, ArrayList and Vector etc.

print(List<String> list);

Check out here the flexible version of above given code example.

You just have to replace the ArrayList object in main() to LinkedList or Vector object and you no longer need to modify the Print class.

 

Flexible code:

class Print {
	public static void print(List arrlist) {
		for (int i = 0; i < arrlist.size(); i++) {
			System.out.println(arrlist.get(i));
		}
	}
}

public class Sample {

	public static void main(String[] args) {

		List list = new ArrayList();
		list.add("Sunday");
		list.add("Monday");
		list.add("Tuesday");
		list.add("Wednesday");
		list.add("Thursday");
		list.add("Friday");
		list.add("Saturday");

		Print.print(list);
	}
}