如何使用ArrayList从Hashmap打印值

时间:2016-12-22 13:07:55

标签: java hashmap documentum dfc documentum-dfc

我正在处理一段有Hashmap的代码。这个hashmap有一个字符串作为键,一个arraylist作为值。我在arraylist中填充了我使用DQL获取的值。我为很多用户提供了三个属性。然后将值放入hashmap中。我想要的是迭代Hashmap并为1个用户获取3个attritubes的集合。我来提供以下代码

    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中打印这些值。

任何形式的帮助都会受到赞赏。

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我创建了一个对象&#34; user&#34;每个条目的字段:

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

toString()

然后覆盖User对象中的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.entrySet()进行迭代。

您的代码:

Map<Integer, User> map = new HashMap<>();
for( Entry<Integer, User> entry : map.entrySet() ) {
  entry.getKey();
  entry.getValue(); // the user object you want
}

我的代码:

{{1}}

答案 3 :(得分:0)

使用advanced来显示String值,如下所示

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 + ",");
        }

    }

由于 拉朱

答案 4 :(得分: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。你的代码是你改变主意时幸存下来的临时想法的重要组合。改变你的代码。 :)