Friday, 30 August 2013

Iterator Design Pattern - Implementation


Click here to watch in Youtube : https://www.youtube.com/watch?v=uRiKSJAnmeA

Click the below Image to Enlarge
Iterator Design Pattern - Implementation

















IteratorPatternDemo.Java

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class IteratorPatternDemo
{

public static void main( String[] args )
{
ArrayList<String> listOfCountries = new ArrayList<String>();
listOfCountries.add("India");
listOfCountries.add("US");
listOfCountries.add("Japan");
listOfCountries.add("France");

Iterator<String> iterator = listOfCountries.iterator();
System.out.println("Iterator type for the Datastructure ArrayList : "+iterator.toString());
System.out.println();
while( iterator.hasNext() )
{
String countryName = iterator.next();
System.out.println("Country Name : " + countryName);
}
System.out.println();
Set<String> setOfCountries = new HashSet<String>();
setOfCountries.add("India");
setOfCountries.add("US");
setOfCountries.add("Japan");
setOfCountries.add("France");
Iterator<String> iterator1 = setOfCountries.iterator();
System.out.println("Iterator type for the Datastructure HashSet : "+iterator1.toString());
System.out.println();
while( iterator1.hasNext() )
{
String countryName = iterator1.next();
System.out.println("Country Name : " + countryName);
}

}

}

Output

Iterator type for the Datastructure ArrayList : java.util.AbstractList$Itr@12dacd1

Country Name : India
Country Name : US
Country Name : Japan
Country Name : France

Iterator type for the Datastructure HashSet : java.util.HashMap$KeyIterator@30c221

Country Name : France
Country Name : US
Country Name : Japan
Country Name : India

No comments:

Post a Comment