如何使用 ArrayList 从 Hashmap 打印值
How to print a value from a Hashmap with a ArrayList
我正在编写一段具有哈希图的代码。这个 hashmap 有一个字符串作为键和一个数组列表作为值。我使用 DQL 获取的值填充数组列表。对于许多用户,我有三个属性。然后将值放入哈希图中。我想要的是迭代 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
我还需要在 Excel 表中打印这些值。
我们将不胜感激。
与其在 ArrayList 本身上使用 toString(),不如迭代其内容并打印各个成员。在 while(itr.hasNext()) 循环中添加另一个循环。
如果 ArrayList 的内容是用户的属性,那么为什么不创建一个 class UserAttributes
具有字段来存储值并覆盖 toString()
以输出他们?
我将创建一个对象 "user",其中包含每个条目的字段:
lstValues.add(total.getString("uname")+","+total.getString("loginname")+","+total.getString("uadd"));
到
lstValues.add(new User(total.getString("uname"), total.getString("loginname"), total.getString("uadd")));
然后在 User
对象中覆盖 toString()
。
提示:
您也可以使用 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
}
使用 advanced for 显示字符串值,如下所示,
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 + ",");
}
}
谢谢
拉朱
而不是
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") );
}
EDIT:从使用 itr 对象切换到 total 对象。我看到您正在使用 IDfCollection total
添加一些以后不会在任何地方使用的标志。丢掉所有东西,然后循环遍历集合 total
。您的代码是您改变主意时幸存下来的临时想法的大混合体。更改您的代码。 :)
我正在编写一段具有哈希图的代码。这个 hashmap 有一个字符串作为键和一个数组列表作为值。我使用 DQL 获取的值填充数组列表。对于许多用户,我有三个属性。然后将值放入哈希图中。我想要的是迭代 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
我还需要在 Excel 表中打印这些值。
我们将不胜感激。
与其在 ArrayList 本身上使用 toString(),不如迭代其内容并打印各个成员。在 while(itr.hasNext()) 循环中添加另一个循环。
如果 ArrayList 的内容是用户的属性,那么为什么不创建一个 class UserAttributes
具有字段来存储值并覆盖 toString()
以输出他们?
我将创建一个对象 "user",其中包含每个条目的字段:
lstValues.add(total.getString("uname")+","+total.getString("loginname")+","+total.getString("uadd"));
到
lstValues.add(new User(total.getString("uname"), total.getString("loginname"), total.getString("uadd")));
然后在 User
对象中覆盖 toString()
。
提示: 您也可以使用 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
}
使用 advanced for 显示字符串值,如下所示,
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 + ",");
}
}
谢谢 拉朱
而不是
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") );
}
EDIT:从使用 itr 对象切换到 total 对象。我看到您正在使用 IDfCollection total
添加一些以后不会在任何地方使用的标志。丢掉所有东西,然后循环遍历集合 total
。您的代码是您改变主意时幸存下来的临时想法的大混合体。更改您的代码。 :)