Tuesday, October 23, 2007

Using for loop for Iterating objects

Normal for Loop
for(Iterator itr = requestList.iterator(); itr.hasNext(); ) {

Enhanced for Loop
for(RequestBean objRequestBean: requestList){

Tuesday, October 09, 2007

HashMap Iteration

Here is one way of getting the values from a map without using the method get(Object key)

import java.util.*;

public class HashTest {

public static void main(String[] args) {

HashMap map = new HashMap();
map.put("name1", "abc");
map.put("name2", "xyz");
map.put("name3", "mno");
map.put("name4", "123");
map.put("name5", "567");


Collection collection = map.values();
Iterator its = collection.iterator();

while(its.hasNext()) {

System.out.println("Result: " + its.next() );
}
}
}