List Interface

  • Eclipse: Oxygen
  • Java: 1.8

List interface in Java

The interface is an outline of the methods for a particular class to implement. Here the ArrayList, LinkedList implement the list, therefore, you can use the methods that are defined in the List interface as shown here.

In the following example, List <> interface is the input parameter of printList method, which will take input as a different type of lists like ArrayList and LinkedList.

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*****************************

In the following example, we can declare the List object by using List Interface and later initialize with their specific type.

List of countries is declared with List interface and later initialize with type ArrayList.

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*****************************

Element: One

Element: Two

Element: Three

Element: Four

Element: Five

*****************************

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu