Collections

Retriveing data from the collections

In java language four mechanisms are available to retrive the elements from any collection object.
  • Foreach loop
  • Iterator interface
  • Listiterator interface
  • Enumeration interface

foreach loop:

This is similar to for loop .It is used to retrive all elements of any collection object.
Syntax:
for(varaible:collection_object)
{
...
...
}

In the above syntax collection object always checks any elements are available in the given list or not.
  • If any element that will be asigned to given variable and the statements of foreach loop will be executed and once again collection object verify any element is available or not and this process will be continued untill the last element.
  • If no elements are found in the given list control comes outside the for each loop.
  • import java.util.*;
    class ForEachLoop
    {
    void list()
    {
    ArrayList<Integer> al=new ArrayList<Integer>();
    al.add(20);
    al.add(30);
    al.add(40);
    al.add(10);
    al.add(70);
    for(int i:al)
    {
    if(i>=30)
    {
    System.out.println("The List Items Are "+i);
    }
    }
    }
    }
    
    class ArrayListDemo
    {
    public static void main(String aargs[])
    {
    ForEachLoop d=new ForEachLoop();
    d.list();
    }
    }
    OUTPUT:
    The List Items Are 30
    The List Items Are 40
    The List Items Are 70
    
    

    Iterator Interface:

    • It is a predefined interface in java.util package.It can be used to retrive the elements by checking one by one location in forword direction.
    • In every collection class iterator method is available. It is used to set the address of collection object to that iterator interface.
    • Iterator interface mainly depends on following methods.
    • hasNext():
    • Which can be used to check the element is available in the next address location or not.
    • Next():
    • Which can be used to retrive the next element from the memory location.
    Syntax:

    Iterator li=collectionobject.iterator()

    import java.util.*;
    class IteratorDemo
    {
    void list()
    {
    ArrayList<Integer> al=new ArrayList<Integer>();
    al.add(20);
    al.add(30);
    al.add(40);
    al.add(10);
    al.add(70);
    Iterator i=al.iterator();
    while(i.hasNext())
    {
    System.out.println("The List Items Are "+i.next());
    }
    }
    }
    
    class ArrayListDemo
    {
    public static void main(String aargs[])
    {
    IteratorDemo d=new IteratorDemo();
    d.list();
    }
    }
    
    OUTPUT:
    The List Items Are 20
    The List Items Are 30
    The List Items Are 40
    The List Items Are 10
    The List Items Are 70
    
          

    ListIterator Interface:

    This is similar to iterator interface but only the difference is iterator interface can be used to retrive the data by traversing in forword direction,Where as listIterator interface can be used to retrived the data by traversing both forword and backword direction.
  • Methods of listIterator interface:

    • hasNext():
    • Used to check is there any element is available in the next location or not.
    • Next():
    • Used to get the next element.
    • hasPrevoius():
    • Used to check is there any element existing in it's previous location or not.
    • Prevoius():
    • Which can be retrived the element of previous location.

    Syntax:

    ListIterator li=collectionobject.listIterator()

    import java.util.*;
    class ListIteratorDemo
    {
    void list()
    {
    ArrayList<Integer> al=new ArrayList<Integer>();
    al.add(20);
    al.add(30);
    al.add(40);
    al.add(10);
    al.add(70);
    ListIterator i=al.listIterator();
    while(i.hasNext())
    {
    System.out.println("The List Items Are "+i.next());
    }
    }
    }
    
    class ArrayListDemo
    {
    public static void main(String aargs[])
    {
    ListIteratorDemo d=new ListIteratorDemo();
    d.list();
    }
    }
    
    OUTPUT:
    The List Items Are 20
    The List Items Are 30
    The List Items Are 40
    The List Items Are 10
    The List Items Are 70
    
          

    Enumeration Interface:

    Which is also can be used to retrived the data from any collection object directly without using any index value.Which also support forword traversing like Iterator.
    Enumeration offers following methods:

    • hasMoreElements():
    • Which will text whether enumeration contains more elements or not.
    • nextElements():
    • returns the next element of enumeration
    Example:StringTokenizer class.

    StringTokenizer class:

    It is a predefined class in java.util package. It can be used to split the given string into number of parts based on dilimiter(dilimiter is a special symbol). Syntax:

    StringTokenizer st=new StringTokenizer(String value,"any special symbol");

    import java.util.*;
    class TokenizerDemo
    {
    public static void main(String aargs[])
    {
    String s="Hello students ,welcome to msk tutorials";
    StringTokenizer st=new StringTokenizer(s,",");
    
    while(st.hasMoreTokens())
    {
    System.out.println(st.nextToken());
    }
    }
    }          
    
    OUTPUT:
    Hello students
    welcome to msk tutorials