map遍历

最常规的一种遍历方法,最常规就是最常用的,虽然不复杂,但很重要,这是我们最熟悉的。
  
 public static void work(Map<String, Student> map) {
        Collection<Student> c = map.values();
        Iterator it = c.iterator();
        for (; it.hasNext();) {
            System.out.println(it.next());
        }
    }

  //利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!!
 
  public static void workByKeySet(Map<String, Student> map) {
        Set<String> key = map.keySet();
        for (Iterator it = key.iterator(); it.hasNext();) {
            String s = (String) it.next();
            System.out.println(map.get(s));
        }
    }

  //比较复杂的一种遍历在这里,呵呵~~他很暴力哦,它的灵活性太强了,想得到什么就能得到什么~~
  
 public static void workByEntry(Map<String, Student> map) {
        Set<Map.Entry<String, Student>> set = map.entrySet();
        for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) {
            Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next();
            System.out.println(entry.getKey() + "--->" + entry.getValue());
        }
    }
}


Collection<Node> nodeSections = mainFrameBO.getTopNode();

		Iterator<Node> iterator = nodeSections.iterator();

		while (iterator.hasNext()) {
			Node node = iterator.next();
			Section section = new Section();
			
			String dataSetId=String.valueOf(node.getId());
			String dataTreeId="dt"+dataSetId;
			
			accordion.addSection(section);
			
			section.setCaption(node.getNodeName());

			DataTree dataTree = new DataTree();
			dataTree.setDataSet(dataSetId);
			dataTree.setExpandingMode(ExpandingMode.sync);
			dataTree.setId(dataTreeId);
			BindingConfig bindingConfig = new BindingConfig();

			//bindingConfig.setLabelProperty("childNodes");
			
	
			bindingConfig.setRecursive(true);
			bindingConfig.setChildrenProperty("childNodes");
			bindingConfig.setLabelProperty("nodeName");		
			//bindingConfig.setAutoCheckChildren(true);
			//bindingConfig.setHasChild(true);
			
			
			dataTree.addBindingConfig(bindingConfig);
			
			//dataTree.addBindingConfig(bindingConfig);
			
			StringBuffer clientScript=new StringBuffer();
			
			clientScript.append("var currentEntity=this.get('#"+dataTreeId+".currentEntity');\n");


取出 Map中key和value的值
Set entries = map.entrySet( );
if(entries != null) {
Iterator iterator = entries.iterator( );
while(iterator.hasNext( )) {
Map.Entry entry =iterator.next( );
Object key = entry.getKey( );
Object value = entry.getValue();
}
}


public static Map.Entry[] getSortedHashtable(Hashtable h){
   Set set = h.entrySet();
   Map.Entry[] entries = (Map.Entry[])set.toArray(new Map.Entry[set.size()]);  
   Arrays.sort(entries,new Comparator(){
    public int compare(Object arg0, Object arg1) {
     Object key1 = ((Map.Entry)arg0).getKey();
     Object key2 = ((Map.Entry)arg1).getKey();
     return ((Comparable)key1).compareTo(key2);
    } 
   }); 
  return entries;
}
//调用这个方法:
Map.Entry[] set = getSortedHashtable(t); 
   //perportyTable
   for (int i=0;i<set.length;i++){
    System.out.println(set[i].getKey().toString());
     System.out.println(set[i].getValue().toString());
   }

你可能感兴趣的:(java)