java Map嵌套多个Map

符号 阅读:988 2021-03-31 22:36:08 评论:0
package com.vmware.client; 
 
import com.vmware.util.Session; 
import com.vmware.vim25.DatastoreSummary; 
import com.vmware.vim25.mo.*; 
 
import java.util.HashMap; 
import java.util.Map; 
 
/** 
 * 指定物理服务器所关联的DataStore(物理服务器存储) 
 * Created by vixuan-008 on 2015/4/24. 
 */ 
public class HostSystemDataStoreSystem { 
    public static  void main(String[] args){ 
        try{ 
            //获取 HostSystem 
            ServiceInstance serviceInstance= Session.getInstance("172.16.1.20", "root", "vmware"); 
 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            ManagedEntity[] mes=new InventoryNavigator(rootFolder).searchManagedEntities("HostSystem"); 
            if(mes==null && mes.length>0){ 
                serviceInstance.getServerConnection().logout(); 
                throw  new Exception(); 
            }else{ 
              <span style="color:#ff0000;">  Map<String,Map<String,String>> map=new HashMap<String, Map<String, String>>();//Map多重嵌套</span> 
                for(int i=0;i<mes.length;i++){ 
                    HostSystem hostSystem=(HostSystem)mes[i]; 
                    System.out.println("name:" + hostSystem.getName()); 
                    if(hostSystem==null) 
                    { 
                        System.out.println("Host not found"); 
                        serviceInstance.getServerConnection().logout(); 
                        return; 
                    } 
 
                    HostDatastoreSystem hostDatastoreSystem = hostSystem.getHostDatastoreSystem(); 
                    Datastore[] datastores=hostDatastoreSystem.getDatastores(); 
                    if(datastores!=null){ 
                        if(datastores.length>0){ 
                            for (int j=0;j<datastores.length;j++){ 
                                Datastore datastore=datastores[j]; 
                                //datastore 相关描述信息 
                                DatastoreSummary datastoreSummary=datastore.getSummary(); 
                                System.out.println("------start-------------"); 
                                System.out.println("capacity:"+datastoreSummary.capacity); 
                                System.out.println("freeSpace:"+datastoreSummary.freeSpace); 
                                System.out.println("name:"+datastoreSummary.name); 
                                System.out.println("type:"+datastoreSummary.type); 
                                System.out.println("uncommitted:"+datastoreSummary.uncommitted); 
                                System.out.println("url:"+datastoreSummary.url); 
                                System.out.println("------end-------------"); 
                              <span style="color:#ff0000;">  if(map.get(hostSystem.getName())!=null){     //核心代码 
                                    Map tempMap=map.get(hostSystem.getName()); 
                                    tempMap.put(datastore.getName(),""+datastoreSummary.freeSpace); 
                                }else{ 
                                    Map<String,String> datastoreMaps=new HashMap<String, String>(); 
                                    datastoreMaps.put(datastore.getName(),""+datastoreSummary.freeSpace); 
                                    map.put(hostSystem.getName(),datastoreMaps); 
                                }</span> 
 
 
                            } 
 
                        } 
                    } 
                } 
 
 
 
 
 
            } 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
    } 
} 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号