How to print a value from a Hashmap with a ArrayList

5.3k Views Asked by At

I am working on a piece of code which has a Hashmap. This hashmap has a string as key and an arraylist as value. I populate arraylist the with values I'm fetching using a DQL. I've three attributes for many users. Then putting the value into the hashmap. What I want is to iterate the Hashmap and fetch the set of 3 attritubes for 1 user. Let me provide the code below

    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;
    }

}

I'm getting output like this

Result :

    [Sayre,Joseph,sayrej,[email protected], Kapoor,Rohit,kapoorro,[email protected], Pineiros-Vallejo, Miguel,pineirom,[email protected]]

    Result :[Sayre,Joseph,sayrej,[email protected], Kapoor,Rohit,kapoorro,[email protected], Pineiros-Vallejo, Miguel,pineirom,[email protected]]

    Result :[Sayre,Joseph,sayrej,[email protected], Kapoor,Rohit,kapoorro,[email protected], Pineiros-Vallejo, Miguel,pineirom,[email protected]]

but I want something like this :

Result : Sayre,Joseph,sayrej,[email protected]
Result : Kapoor,Rohit,kapoorro,[email protected]
Result : Pineiros-Vallejo, Miguel,pineirom,[email protected]

Also I need to print this values in an excelsheet.

Any kind of help will appreciated.

5

There are 5 best solutions below

0
On BEST ANSWER

If the content of the ArrayList are attributes of a user, then why don't you create a class UserAttributes having fields to store the values and overrides toString() to output them?

0
On

I'd create an object "user" with fields for each entry:

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

to

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

Then override toString() inside the User object.

Tip: You can also use map.entrySet() to iterate.

Your code:

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);
}

My Code:

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

Use advanced for to display the String value like below,

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

    }

Thanks Raju

0
On

Instead of

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

use this

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

EDIT: switched from using itr object to total object. I see you are using IDfCollection total for adding some flag which you don't use later anywhere. Lose everything and just loop through collection total. Your code is big mixture of your temporarily ideas that survived when you changed your mind. Change your code. :)

0
On

Instead of using toString() on the ArrayList itself, iterate over its contents and print the individual members. Add another loop inside the while(itr.hasNext()) loop.