• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Retrieveing data from the collections

Retrieveing data from the collections

In java language four mechanisims are available to retrieve the elements from any collection object.
1.Foreach Loop
2.Iterator Interface
3.Listiterator Interface
4.Enumeration Interface







Foreach loop

This is similar to for loop .It is used to retrieve all elements of any collection object.
Syntax:

for(variable : 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);
            }
        }
    }
}
public 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 retrieve the elements by checking one by one location in forward 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 retrieve the next element from the memory location.
Syntax:
Iterator objref=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());
        }
    }
}
public 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 retrieve the data by traversing in forward direction,Where as listIterator interface can be used to retrieved the data by traversing both forward and backward 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.
hasPrevious():
Used to check is there any element existing in it's previous location or not.
Previous():
Which can be retrieved the element of previous location.
Syntax:
ListIterator objref=collectionobject.listIterator()

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);
        ListIterator i=al.listIterator();
        while(i.hasNext())
        {
            System.out.println("The List Items Are "+i.next());
        }
        System.out.println("\n");
        while(i.hasPrevious())
        {
            System.out.println("The List Items Are "+i.previous());
        }
    }
}
public 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


The List Items Are 70
The List Items Are 10
The List Items Are 40
The List Items Are 30
The List Items Are 20

Enumeration Interface:
Which is also can be used to retrieved the data from any collection object directly without using any index value.Which also support forward 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.*;
public class TokenizerDemo
{
    public static void main(String aargs[])
    {
        String s="Hello students ,welcome to msk technologies";
        StringTokenizer st=new StringTokenizer(s,",");
        while(st.hasMoreTokens())
        {
            System.out.println(st.nextToken());
        }
    }
}         
                        

Output:
Hello students
welcome to msk technologies

Space dilimiter Example

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

Output:
Hello
students
,welcome
to
msk
technologies

Count The Number of Words in a Given String

import java.util.*;
public class TokenizerDemo
{
    public static void main(String aargs[])
    {
        String s="Hello students ,welcome to msk technologies";
        StringTokenizer st=new StringTokenizer(s," ");
        int count=0;
        while(st.hasMoreTokens())
        {
            count++;
            st.nextToken();
        }
        System.out.pritln("Number of Words are"+ count);
    }
}                                 

Output:
6
Enumeration Interface Example:

import java.util.*;
public class Main 
{
    public static void main(String[] args)
    {
        Enumeration<Integer> classNine;
        Vector<Integer> rollno = new Vector<Integer>();
        rollno.add(1);
        rollno.add(2);
        rollno.add(3);
        rollno.add(4);
        rollno.add(5);
        rollno.add(6);
        rollno.add(7);
        rollno.add(8);
        classNine = rollno.elements();
  
        while (classNine.hasMoreElements()) 
        {
            System.out.println("Roll No = " + classNine.nextElement());
        }
    }
}
                            

Output:
Roll No = 1
Roll No = 2
Roll No = 3
Roll No = 4
Roll No = 5
Roll No = 6
Roll No = 7
Roll No = 8