![](/img/trans.png)
[英]Creating a hashmap from excel file of type <String, HashMap> in java
[英]Creating a HashMap of type <String , Object>
在上一篇文章中, 创建ToolTip托管bean
我能够创建一个经过管理的bean,仅通过一次查找就可以收集和显示工具提示文本,并将其存储在Application Scope变量中。 效果很好。
我正处于JAVA学习曲线中比较陡峭的部分,所以请原谅我。
我还有另一个托管bean要求来创建HashMap Application Scope,但是这次它必须是String,Object类型。 在该应用程序中,我有一个“主”数据库,其中包含大多数代码,自定义控件和XPages。 该主数据库将指向一个或多个应用程序数据库,这些数据库将存储特定于所讨论应用程序的Notes文档。 因此,我在Master中创建了一系列应用程序文档,这些文档指定了特定于该应用程序的应用程序,帮助和规则数据库的RepID,以及有关该应用程序的许多其他信息。 这应该允许我重用将通过向其传递应用程序名称来打开特定数据库的相同自定义控件。 例如,主设计数据库可能指向“采购”,“客户投诉”,“旅行请求”等。下面的代码可以加载和存储HashMap,但是我在检索数据时遇到了麻烦。 编译器显示两个错误,一个在公共Object get(String key)方法上显示,另一个在mapValue = this.internalMap.get(key);上显示。 在getAppRepID方法中,我认为这主要是语法,但不确定。 我已在出现的代码中注释了该错误。
/**
*This Class makes the variables that define an application within Workflo!Approval
*available as an ApplicationScope variable.
*/
package ca.wfsystems.wfsAppUtils;
import lotus.domino.Base;
import lotus.domino.Session;
import lotus.domino.Database;
import lotus.domino.View;
import lotus.domino.NotesException;
import lotus.domino.ViewColumn;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;
import lotus.domino.Name;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import com.ibm.xsp.extlib.util.ExtLibUtil;
/**
* @author Bill Fox Workflo Systems WFSystems.ca
* July 2014
* This class is provided as part of the Workflo!Approval Product
* and can be reused within this application.
* If copied to a different application please retain this attribution.
*
*/
public abstract class ApplicationUtils implements Serializable, Map<String, Object> {
private static final long serialVersionUID = 1L;
private Session s;
private Name serverName;
private String repID;
private String thisKey;
private ViewEntryCollection formVECol;
private Vector formNames;
private Database thisDB;
private Database appDB;
private View appView;
private View formView;
private ViewEntry formVE;
private ViewEntry tFormVE;
private ViewEntry ve;
private ViewEntry tVE;
private ViewEntryCollection veCol;
private final Map<String, Object> internalMap = new HashMap<String, Object>();
public ApplicationUtils() {
this.populateMap(internalMap);
}
private void populateMap(Map<String, Object> theMap) {
try{
s = ExtLibUtil.getCurrentSession();
//serverName = s.createName(s.getServerName());
thisDB = s.getCurrentDatabase();
appView = thisDB.getView("vwWFSApplications");
veCol = appView.getAllEntries();
ve = veCol.getFirstEntry();
ViewEntry tVE = null;
while (ve != null) {
rtnValue mapValue = new rtnValue();
tVE = veCol.getNextEntry(ve);
Vector colVal = ve.getColumnValues();
thisKey = colVal.get(0).toString();
mapValue.setRepID(colVal.get(2).toString());
// ...... load the rest of the values .......
theMap.put(thisKey, mapValue);
recycleObjects(ve);
ve = tVE;
}
}catch(NotesException e){
System.out.println(e.toString());
}finally{
recycleObjects(ve, veCol, appView, tVE);
}
}
public class rtnValue{
private String RepID;
private String HelpRepID;
private String RuleRepID;
private Vector FormNames;
public String getRepID() {
return RepID;
}
public void setRepID(String repID) {
RepID = repID;
}
public String getHelpRepID() {
return HelpRepID;
}
public void setHelpRepID(String helpRepID) {
HelpRepID = helpRepID;
}
public String getRuleRepID() {
return RuleRepID;
}
public void setRuleRepID(String ruleRepID) {
RuleRepID = ruleRepID;
}
public Vector getFormNames() {
return FormNames;
}
public void setFormNames(Vector formNames) {
FormNames = formNames;
}
}
public void clear() {
this.internalMap.clear();
this.populateMap(this.internalMap);
}
public boolean containsKey(Object key) {
return this.internalMap.containsKey(key);
}
public boolean containsValue(Object value) {
return this.internalMap.containsValue(value);
}
public Set<java.util.Map.Entry<String, Object>> entrySet() {
return this.internalMap.entrySet();
}
public Object get(String key) {
//error on Object get Method must return a result of type Object
try {
if (this.internalMap.containsKey(key)) {
return this.internalMap.get(key);
}
} catch (Exception e) {
System.out.println(e.toString());
rtnValue newMap = new rtnValue();
return newMap;
}
}
public boolean isEmpty() {
return this.internalMap.isEmpty();
}
public Set<String> keySet() {
return this.internalMap.keySet();
}
public Object put(String key, Object value) {
return this.internalMap.put(key, value);
}
public Object remove(Object key) {
return this.internalMap.remove(key);
}
public int size() {
return this.internalMap.size();
}
public Collection<Object> values() {
return this.internalMap.values();
}
public void putAll(Map<? extends String, ? extends Object> m) {
this.internalMap.putAll(m);
}
public String getAppRepID(String key){
/*get the Replica Id of the application database
* not sure this is the correct way to call this
*/
rtnValue mapValue = new rtnValue();
mapValue = this.internalMap.get(key);
//error on line above Type Mismatch: can not convert Object to ApplicationUtils.rtnValue
String repID = mapValue.getRepID();
}
public static void recycleObjects(Object... args) {
for (Object o : args) {
if (o != null) {
if (o instanceof Base) {
try {
((Base) o).recycle();
} catch (Throwable t) {
// who cares?
}
}
}
}
}
}
对于get()方法,我处理这种情况的方式是创建一个正确数据类型为null的变量,在我的try / catch中设置该变量,最后返回该变量。 所以:
Object retVal = null;
try....
return retVal;
对于另一个错误,如果右键单击错误标记,则可能使您有机会将变量转换为rtnValue,因此:
mapValue = (rtnValue) this.internalMap.get(key)
如果您还不了解它,那么Head First Java是一本有用的书,它可以使我了解一些Java概念。 还值得从OpenNTF下载Domino Designer的FindBugs插件。 它将识别错误以及不良做法。 只需忽略“本地”包中的错误!
问题是存在一条不返回任何内容的执行路径
public Object get(String key) {
//error on Object get Method must return a result of type Object
try {
if (this.internalMap.containsKey(key)) { // false
return this.internalMap.get(key);
}
} catch (Exception e) {
System.out.println(e.toString());
rtnValue newMap = new rtnValue();
return newMap;
}
}
如果internalMap
不存在key
,则不抛出任何内容,则该方法不返回任何内容。 要解决此问题,请最后返回newMap
。
public Object get(String key) {
//error on Object get Method must return a result of type Object
try {
if (this.internalMap.containsKey(key)) {
return this.internalMap.get(key);
}
} catch (Exception e) {
System.out.println(e.toString());
}
rtnValue newMap = new rtnValue();
return newMap;
}
您可以内联返回值以保存分配(这是编译器将要执行的操作)。 我并没有这样做只是为了在示例中清楚说明。
但是,仍然在getAppRepID
方法中出现编译器错误。 您期望一个rtnValue
但是您发回一个Object
。 你必须投在那里。
处理此问题的适当方法是,如果您知道所有值都是给定类型的,请使用正确的类型创建映射。
您是否尝试过将internalMap设置为rtnValue实例的映射(如此)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.