What is List interface

What is List interface

Answer: public interface List extends Collection

List is ordered Collection (also called sequence). Lists can contain duplicate elements. Because it extends Collection all operations is inherited from it, List interface includes following below operations:

  • Search – it searches to specified object in the list and will returns its number position. Methods include lastIndexOf AND indexOf.
  • Positional access – this manipulates the elements based on its number position in list which includes methods as set, get, add, addAll, and remove.
  • Range view – sublist method performs arbitrary range operations to the list.
  • Iteration – it extends Iterator semantics and takes advantage of lists sequential nature. listIterator methods provide this kind of behavior.

List interface has two general purpose implementations:

  • ArrayList: This is usually better performance implementation of the List
  • LinkedList: Under certain criteria this offers better performance

Below is java class example:

package com.javahonk.arraylistandlinkedlist;

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

public class ArrayListAndLinkedList {

    public static void main(String[] args) {

    System.out.println("ArrayList example\n");
    List<String> list = new ArrayList<String>();
    list.add("Java");
    list.add("Honk");
    list.add("Test");
    list.add(null);
    list.add(null);

    for (String string : list) {
        System.out.println(string);
    }

    System.out.println("\nLinkedList example\n");
    LinkedList<String> linkedList = new LinkedList<String>();

    linkedList.add("Java");
    linkedList.add("Honk");
    linkedList.add("Test");
    linkedList.add(null);
    linkedList.add(null);
    
    for (String string : linkedList) {
        System.out.println(string);
    }


    }

}

Output:
What is List interface

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *