1

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,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]

but I want something like this :

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

Also I need to print this values in an excelsheet.

Any kind of help will appreciated.

CinCout
  • 9,486
  • 12
  • 49
  • 67
Zeus07
  • 168
  • 1
  • 5
  • 17
  • 3
    You are **mixing** up responsibilities. You see, your method LookupReport (better named lookupReport btw) ... should not do so many things. It should just **collect** that information. Then some *other* method could be doing the printing. And *another* method to insert that data into an excel sheet. (and hint: you do **not** print into a sheet - you insert data) – GhostCat Dec 22 '16 at 13:10
  • i´d start with creating a class `User`, changing the `Map` to `Map` or `Map>` if there should be more than one user for each key. I´d continue with overriding the `toString` method of the freshly created class `User` in order to at least have a little bit of `OOP` and java conventions in there. Also a hint: using `Generics` would save you from typcasting stuff around – SomeJavaGuy Dec 22 '16 at 13:10
  • Also you're using the same List for each Map entry, which is obviously not intended. But in general, above's two comments are totally the right recommendations. – qqilihq Dec 22 '16 at 13:12
  • There is too much going on in the code provided that it is unclear what you are actually looking for. My best guess would be that your goal is actually to produce an Excel dokument from the query, but you are asking how to print it nicely to the command prompt. Maybe do like comments above suggests, then come back and ask how to save these values into an Excel document. – Mads Hoel Dec 22 '16 at 13:31

5 Answers5

0

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.

Jim Kiley
  • 3,632
  • 3
  • 26
  • 43
0

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?

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
0

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
}
beeb
  • 1,615
  • 1
  • 12
  • 24
0

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

Raju
  • 1
  • 4
0

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. :)

Miki
  • 2,493
  • 2
  • 27
  • 39