如何将LinkedHashMap值加载到Hashtable中

时间:2016-08-23 20:32:40

标签: java swing jtable hashtable linkedhashmap

我有以下代码:

clientTableList = new Object[DBQueries.getAllClients().size()][3]; [I want to load 3 records for now]

LinkedHashMap<String, LinkedHashMap<String, String>> ClientHashMap = DBQueries.getAllClients();

System.out.println(clientHashMap.keySet());
//Printing all Values
System.out.println(clientHashMap.values());

结果:

[Bob Hope, Elena Hairr, Blossom Kraatz, Loreen Griepentrog]
[{UserID=2345, GivenName=Bob, FamilyName=Hope, DateOfBirth=August 30, 1963, NameSuffix=Sr, NamePrefix=, Email=francoise.rautenstrauch@rautenstrauch.com, Phone=519- ... 

我需要加载JTable,我的下一个代码是:

for (int i = 0; i < clientHashMap.size(); i++) {
    clientTableList[i] = new Object[] {
        clientHashMap.get("GivenName") + " " + clientHashMap.get("FamilyName"),
        clientHashMap.get("LoginEmail") + " ",
        clientHashMap.get("Phone") + " " 
    };

但我为null获取了所有clientTableList

我需要将所有值加载到HashTable,然后将HashTable加载到clientTableList。正确?

2 个答案:

答案 0 :(得分:1)

您的clientTableList没有这些字段,只有其值包含这些字段:

int i = 0;
for (Map<String, String> client: clientHashMap.values()) {
    clientTableList[i++] = String.format("%s %s %s %s",
            client.get("GivenName"),
            client.get("FamilyName"),
            client.get("LoginEmail"),
            client.get("Phone"));
};

答案 1 :(得分:0)

以下变更有效:

clientTableList[i++] = new Object[]{ client.get("GivenName") + " " + client.get("FamilyName") , client.get("LoginEmail") + " " , client.get("Phone") + " " };