1

我正在研究一段具有 Hashmap 的代码。这个 hashmap 有一个字符串作为键,一个数组列表作为值。我使用 DQL 获取的值填充 arraylist。我为许多用户提供了三个属性。然后将值放入哈希图中。我想要的是迭代 Hashmap 并为 1 个用户获取一组 3 个属性。让我提供下面的代码

    package com;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import xtrim.util.i;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfLoginInfo;
import com.documentum.fc.common.IDfLoginInfo;

public class Adlookup {


    IDfSessionManager sessionMrg = null;
    IDfSession session=null;
    IDfClient dfclient = null;
    IDfClientX clientX = new DfClientX();
    IDfCollection total = null;
    int j;
    int flag = 0;
    WriteToExcel ex = new WriteToExcel();

    public void LookupReport(String docbaseName, String username, String password) throws DfException, IOException
    {
        dfclient = clientX.getLocalClient();
        String Docbase = docbaseName;

        IDfLoginInfo loginInfo = new DfLoginInfo();
        loginInfo.setUser(username);
        loginInfo.setPassword(password);
        sessionMrg = dfclient.newSessionManager();
        sessionMrg.setIdentity(Docbase, loginInfo);
        session = sessionMrg.getSession(Docbase);
        System.out.println("connection created for adlookup");


        String query = Getquery();
        IDfQuery dql = new DfQuery();
        dql.setDQL(query);

        total = dql.execute(session, IDfQuery.DF_EXEC_QUERY);

        System.out.println("all good for lookup");

        //String[] columnNames = new String[] { "User Name","User Login Name","Email"};
        List<String> lstValues = new ArrayList<String>();  
        Map<Integer, ArrayList<String>> myMap = new HashMap<Integer, ArrayList<String>>();  
        while (total.next())
        {

             lstValues.add(total.getString("uname")+","+total.getString("loginname")+","+total.getString("uadd"));
             myMap.put(flag, (ArrayList<String>) lstValues);

             flag++;    
             System.out.println("Flag value: " +flag);

            // lstValues.clear();

        }       

        Set setofKeys = myMap.keySet();
        Iterator itr = setofKeys.iterator();

        while(itr.hasNext())
        {
            Integer key = (Integer) itr.next();
            ArrayList<String> value = myMap.get(key);
            System.out.println("\nResult :"+value);
        }
    }

    private String Getquery() {
        // TODO Auto-generated method stub
        String query = "select user_name as uname, user_login_name as loginname, user_address as uadd  from dm_user dmu, dm_dbo.MV_V_MIDAS_MERCK_PRSN1 dma where  upper(dmu.user_login_name)=upper(dma.isid) and dmu.user_state=0 and directory_display_ind='I'";
        return query;
    }

}

我得到这样的输出

Result :

    [Sayre,Joseph,sayrej,joseph.sayre@abc.com, Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com, Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com]

    Result :[Sayre,Joseph,sayrej,joseph.sayre@abc.com, Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com, Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com]

    Result :[Sayre,Joseph,sayrej,joseph.sayre@abc.com, Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com, Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com]

但我想要这样的东西:

Result : Sayre,Joseph,sayrej,joseph.sayre@abc.com
Result : Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com
Result : Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com

我还需要在 excelsheet 中打印这些值。

任何形式的帮助将不胜感激。

4

5 回答 5

0

与其在 ArrayList 本身上使用 toString() ,不如遍历其内容并打印各个成员。在 while(itr.hasNext()) 循环中添加另一个循环。

于 2016-12-22T13:10:39.300 回答
0

如果 ArrayList 的内容是用户的属性,那么为什么不创建一个UserAttributes具有字段来存储值并覆盖toString()以输出它们的类呢?

于 2016-12-22T13:16:16.997 回答
0

我会为每个条目创建一个包含字段的对象“用户”:

lstValues.add(total.getString("uname")+","+total.getString("loginname")+","+total.getString("uadd"));

lstValues.add(new User(total.getString("uname"), total.getString("loginname"), total.getString("uadd")));

然后在对象toString()内部覆盖。User

提示:您也可以使用 map.entrySet() 进行迭代。

你的代码:

Set setofKeys = myMap.keySet();
Iterator itr = setofKeys.iterator();
while(itr.hasNext())
{
    Integer key = (Integer) itr.next();
    ArrayList<String> value = myMap.get(key);
    System.out.println("\nResult :"+value);
}

我的代码:

Map<Integer, User> map = new HashMap<>();
for( Entry<Integer, User> entry : map.entrySet() ) {
  entry.getKey();
  entry.getValue(); // the user object you want
}
于 2016-12-22T13:17:59.423 回答
0

使用高级用于显示字符串值,如下所示,

while(itr.hasNext())
    {
        Integer key = (Integer) itr.next();
        ArrayList<String> value = myMap.get(key);
        System.out.print("\nResult : ");
        for(String strValue:value){
            System.out.println(strValue + ",");
        }

    }

谢谢拉朱

于 2016-12-22T13:18:11.137 回答
0

代替

while(itr.hasNext())
        {
            Integer key = (Integer) itr.next();
            ArrayList<String> value = myMap.get(key);
            System.out.println("\nResult :"+value);
        }

用这个

while (total.next) {
            System.out.println("\nResult :" 
                 + itr.getString("uname") + ", " 
                 + itr.getString("loginname") + ", " 
                 + itr.getString("uadd") );
}

编辑:从使用 itr 对象切换到总对象。我看到您正在使用IDfCollection total添加一些您以后不会在任何地方使用的标志。失去一切,只是循环收集total。您的代码是您改变主意时幸存下来的临时想法的大混合。更改您的代码。:)

于 2016-12-22T13:24:22.690 回答