05 September, 2013

Iterate Through Java Map

There are multiple ways to iterate through a Java Map.

Assuming the following map declaration:
Map mapObj = new HashMap();

//Use Iterator [Generic]
Iterator> iter= mapObj.entrySet().iterator();
while ( iter.hasNext() ) {
    Entry item = iterator.next();
    System.out.println(item.getKey() + " - " + item.getValue());
}


//User Iterator [Without Generic]
Iterator iter= mapObj.entrySet().iterator();
while ( iterator2.hasNext() ) {
    Entry item = (Entry) iterator.next();
    System.out.println(item.getKey() + " - " + item.getValue());
}


// Use For Each [Generic]
for ( Entry item : mapObj.entrySet() ) {
    System.out.println(item.getKey()+ " - " + item.getValue());
}


// Use For Each [Without Generic]
for ( Entry item : mapObj.entrySet() ) {
    System.out.println(item.getKey()+ " - " + item.getValue());
}


//Fetch Value using Key
for ( String key : mapObj.keySet() ) {
    System.out.println(key + " - " + mapObj.get(key));
}



// Loop through Keys only
for ( String key : mapObj.keySet() ) {
    System.out.println(key);
}



// Loop through Values only
for ( Object value : mapObj.values() ) {
    System.out.println(value);
}


Thanks

No comments:

Post a Comment

ADF : Scope Variables

Oracle ADF uses many variables and each variable has a scope. There are five scopes in ADF (Application, Request, Session, View and PageFl...