我想编写一个构造函数来设置HashMap的值。你能告诉我最好的方法吗?
立即写我正在使用switch语句调用基于HashMap键的方法,但我想知道是否有更好的替代方法。
仅供参考,在myItems类中,我实际上要设置25个变量。
public class MainClass{
BufferedReader br = new BufferedReader(new FileReader(file));
String[] datakey = br.readLine().split(";"); // getting header, 25 of them
HashMap<String,String> bookmap = new HashMap<String,String>();
String[] dataarr = line.split(";"); // getting values, 25 of them
int k = 0;
for(String d : datakey){
bookmap.put(d, dataarr[k++]); // Key-Value Pair
}
myItems item = new myItems(bookmap); // HOW TO WRITE THIS CONSTRUCTOR?
}
public class myItems {
String _id="";
String _name="";
String _genre="";
String _language="";
int _rating=0;
int _price=0;
...........................
...//25 OF THEM...
...........................
public myItems(HashMap<String,String> allrec){
Iterator<Map.Entry<String,String>> it = allrec.entrySet().iterator();
while(it.hasNext()){
Map.Entry pairs = (Map.Entry)it.next();
Switch(pairs.getKey()){
case "id":
setid(pairs.getValue());
break;
case "name":
setname(pairs.getValue());
break;
Deafult:
break;
}
}
}
public int getid(){
return this._id;
}
public String getname(){
return this._name;
}
..............................
..............................
..............................
public void setid(int id){
this._id = id;
}
public void setname(String name){
this._name = name;
}
..............................
..............................
..............................
}
答案 0 :(得分:1)
为什么不这样写下来就像这样:
public myItems(HashMap<String,String> allrec){
setid(allrec.get("id");
setname(allrec.get("name");
}
如果您不希望为任何属性分配空值,那么您应该检查Map是否返回null:
public myItems(HashMap<String,String> allrec){
String id = allrec.get(id);
if(id != null)
setid(id);
// ...
}
实际上还有另一种解决方案可以解决您的问题。为什么还要在属性中存储这些值呢?您可以将它们存储在Map本身中。你的构造函数将是这样的:
private Map<String, String> mAttributes;
public myItems(HashMap<String, String> allrec) {
mAttributes = allrec;
}
请注意,您应该考虑复制整个地图,而不是像上面那样存储参考。然后编写如下的set方法:
public void setId(String id) {
mAttributes.put("id", id);
}
你的get方法是这样的:
public String getId() {
return mAttributes.get("id");
}
答案 1 :(得分:0)
您可以使用 Reflection 。想象一下,有些领域有定位器(对于数据结构是好的,对类有疑问 - 以及可变性与不变性有什么关系?),其他领域没有。然后你可以这样做:
import java.lang.reflect.Method;
public void setAll(final Map<String, String> fieldMap) {
for (final Map.Entry<String, String> entry : fieldMap.entrySet())
setField(entry);
}
public void setField(final Map.Entry<String, String> entry) {
setField(entry.getKey(), entry.getValue());
}
public void setField(final String name, final String value) {
final Method fieldSetter = getFieldSetter(name);
if (fieldSetter != null) {
fieldSetter.invoke(this, value);
return;
}
final Field field = getField(name);
if (field != null) {
field.set(this, value);
return;
}
// Throw some exception
}
public Method getFieldSetter(final String fieldName) {
return getMethod(getSetterName(fieldName));
}
public static String getSetterName(final String fieldName) {
return "set" + upperCaseFirst(fieldName);
}
public static String upperCaseFirst(final String s) {
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
public Method getMethod(final String methodName) {
final Class<?> clazz = getClass();
try {
return clazz.getMethod(methodName, String.class);
} catch (final NoSuchMethodException ignore) { }
try {
return clazz.getDeclaredMethod(methodName, String.class);
} catch (final NoSuchMethodException ignore) { }
return null;
}
public Field getField(final String fieldName) {
final Class<?> clazz = getClass();
try {
return clazz.getField(fieldName);
} catch (final NoSuchFieldException ignore) { }
try {
return clazz.getDeclaredField(fieldName);
} catch (final NoSuchFieldException ignore) { }
return null;
}