如何创建一个通用列表来存储任何类型的数据



我正在创建一个简单的面向列的数据库,用于学习目的。根据属性的类型,我想创建一个相同类型的列表,并在其中设置数据

public class Relation {
private Table table;
private Integer id;
private Map<String, List<?>> columns; // is this correct?
public Relation(Table table) {
this.table = table;
this.id = 1;
this.columns = new HashMap<>();
for (Attribute attribute: table.getAttributes()) {
if (attribute.getType().equalsIgnoreCase("String")) {
List<String> list = new ArrayList<String>();
columns.put(attribute.getName(), list);
}
if (attribute.getType().equalsIgnoreCase("Integer")) {
List<Integer> list = new ArrayList<Integer>();
columns.put(attribute.getName(), list);
}
}
}
public void setIntegerData(String colName, Integer val) {
this.columns.get(colName).add(val); // how to set this?
}
public void setStringData(String colName, String val) {
this.columns.get(colName).add(val);
}
}

我收到以下错误

/Users/pranay.sankpal/inmemsql/src/main/java/com/inmemsql/engine/Relation.java:32:34
java: no suitable method found for add(java.lang.Integer)
method java.util.Collection.add(capture#1 of ?) is not applicable
(argument mismatch; java.lang.Integer cannot be converted to capture#1 of ?)
method java.util.List.add(capture#1 of ?) is not applicable
(argument mismatch; java.lang.Integer cannot be converted to capture#1 of ?)

我会将映射的值作为对象列表。


public class Relation {

private Integer id;
private Map<String, List> columns; 
public Relation(){
columns = new HashMap<>();
columns.put("key1", new ArrayList());
columns.put("key2", new ArrayList());
}

public void setData(String colName, Object val) {
this.columns.get(colName).add(val); 
}
}

最新更新