Java – List Example

 

List interface is part of java.util package. List interface can add value elements by add(value) method.
List can implement Vector, ArrayList class.
List value can get by Iterator interface.

List example give a method, how to use List in java. List Interface is part of java 1.2 and java 1.8. In List, sequence of adding elements is remain same and get elements in same order sequence which was inserted. No change in order of element in collection. Object can be get from collection by index value, e.g. 0, 1…

1. To insert element in collection, we can use add(), addAll() method.
2. To get elements from list, we have option to use get() method with index get(int index)
3. To remove elements from list, remove() method can be used.

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class ListExample {

    public static void main(String[] args) {

        // List Example implement with ArrayList
        List<String> ls=new ArrayList<String>();

        ls.add("one");
        ls.add("Three");
        ls.add("two");
        ls.add("four");

        Iterator it=ls.iterator();

        while(it.hasNext())
        {
          String value=(String)it.next();

          System.out.println("Value :"+value);
        }
    }
}

Output

List Value :o ne
List Value :Three
List Value :two
List Value :four

Tags:

 

3 Responses to “Java – List Example”

  1. maheshbabu says:

    import java.util.Iterator;
    import java.util.List;
    import java.util.ArrayList;

    public class ListExample {

    public static void main(String[] args) {

    // List Example implement with ArrayList
    List ls=new ArrayList();

    ls.add(“one”);
    ls.add(“Three”);
    ls.add(“two”);
    ls.add(“four”);

    Iterator it=ls.iterator();

    while(it.hasNext())
    {
    String value=(String)it.next();

    System.out.println(“Value :”+value);
    }
    }
    }

Leave a Reply