Source Code : Listing the Elements of a Collection

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Listing the Elements of a Collection

     

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {
  public static void main(String[] argv) {
    List collection = new ArrayList();

    // For a set or list
    for (Iterator it = collection.iterator(); it.hasNext();) {
      Object element = it.next();
    }
    Map map = new HashMap();
    // For keys of a map
    for (Iterator it = map.keySet().iterator(); it.hasNext();) {
      Object key = it.next();
    }

    // For values of a map
    for (Iterator it = map.values().iterator(); it.hasNext();) {
      Object value = it.next();
    }

    // For both the keys and values of a map
    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
      Map.Entry entry = (Map.Entry) it.next();
      Object key = entry.getKey();
      Object value = entry.getValue();
    }
  }
}

   
    
    
    
    
  

Thank with us