import java.util.*;
/**
*
* @author Amirtharaj
*/
public class CollectionLoopThroDemo {
public static void main(String arg[]) {
CollectionLoopThroDemo cd = new CollectionLoopThroDemo();
List al = new ArrayList();
al.add("C");
al.add("A");
al.add("B");
cd.iterateList(al);
Set setDet = new HashSet();
setDet.add("B");
setDet.add("A");
setDet.add("C");
cd.iterateSet(setDet);
Map mapDet = new HashMap();
mapDet.put("B", 2.0);
mapDet.put("A", 1.0);
mapDet.put("C", 3.0);
cd.iterateMap(mapDet);
}
public void iterateList(List al) {
//access via Iterator
Iterator iterator = al.iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
System.out.println(str);
}
//access via new for-loop
for (String str : al) {
System.out.println(str);
}
}
public void iterateSet(Set setDet) {
//access via Iterator
Iterator iterator = setDet.iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
System.out.println(str);
}
//access via new for-loop
for (String str : setDet) {
System.out.println(str);
}
}
public void iterateMap(Map mapDet) {
//access via Iterator
Iterator iterator = mapDet.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Double value = (Double) mapDet.get(key);
System.out.println(key + "," + value);
}
//access via new for-loop
for (String key : mapDet.keySet()) {
Double value = mapDet.get(key);
System.out.println(key + "," + value);
}
}
}
/**
*
* @author Amirtharaj
*/
public class CollectionLoopThroDemo {
public static void main(String arg[]) {
CollectionLoopThroDemo cd = new CollectionLoopThroDemo();
List
al.add("C");
al.add("A");
al.add("B");
cd.iterateList(al);
Set
setDet.add("B");
setDet.add("A");
setDet.add("C");
cd.iterateSet(setDet);
Map
mapDet.put("B", 2.0);
mapDet.put("A", 1.0);
mapDet.put("C", 3.0);
cd.iterateMap(mapDet);
}
public void iterateList(List
//access via Iterator
Iterator iterator = al.iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
System.out.println(str);
}
//access via new for-loop
for (String str : al) {
System.out.println(str);
}
}
public void iterateSet(Set
//access via Iterator
Iterator iterator = setDet.iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
System.out.println(str);
}
//access via new for-loop
for (String str : setDet) {
System.out.println(str);
}
}
public void iterateMap(Map
//access via Iterator
Iterator iterator = mapDet.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Double value = (Double) mapDet.get(key);
System.out.println(key + "," + value);
}
//access via new for-loop
for (String key : mapDet.keySet()) {
Double value = mapDet.get(key);
System.out.println(key + "," + value);
}
}
}
No comments:
Post a Comment