java implementation (list with different data types)
Solution 1
You could create a class that represents a column, and then represent a row as a list of columns:
public class Column {
private String name;
private Class clazz;
private Object value;
public Column(String name, Class clazz, Object value) {
this.name = name;
this.clazz = clazz;
this.value = value;
}
// ...
}
Your data would then be:
List<Column> row = new ArrayList<Column>();
row.add(new Column("Name", String.class, "Oskar"));
row.add(new Column("Age", Integer.class, 28));
row.add(new Column("weight", Double.class, 75.5));
row.add(new Column("weightUnit", String.class, "kilogram"));
However, it looks like you're trying to create a database, so you might want to have a Table
class which would have a List<ColumnDescriptor>
, where ColumnDescriptor
would be like the above Column
class, only without the value -- just the name and type. And then perhaps have a TableRow
class that would have a List<Object>
of all the values. When you add a row to a table, you would want to validate that the values are compatible with the types of the columns of that table.
You might want to look at the way JDBC
is designed for inspiration. Supporting all the needed generality, functionality, and making it all type-safe is a lot of work. Unless you really need this for an ORM
tool, custom reporting solution, or an actual database, you might want to rethink the design and ask if this much generality is necessary. ("Do the simplest thing that could possibly work.")
Solution 2
Encapsulate the information within an object, and then have a list of said object. For instance,
public final class TableRow{
private final String name;
private final int age;
private final double weight;
private final String weightUnit;
public TableRow(String name, int age, double weight, String weightUnit){
...
}
// include accessors
}
And then the list declaration in the other class would like such,
List<TableRow> list = new ArrayList<TableRow>();
Antje Janosch
Updated on July 21, 2022Comments
-
Antje Janosch almost 2 years
I'm still not very much used to Java. I have a class which should contain a List. One could imagine this list representing a row in a table (column name, value of certain type):
Name - "Oskar", Age - 28, weight - 75.5, weightUnit - "kilogram", ...
As the content of the list should be flexible, I cannot use class member variables. Basically, the programmer is blind to the content. But then, at some point - I have to extract all entries with double values. How would you implement something like this?
-
Antje Janosch over 12 yearsbut how would it look like? and how would I get the information of which type the object is? can you provide some example code?
-
Antje Janosch over 12 yearsas I said, I don't know in advance about the content. The end user will decide about name and type of any entry. He might wish to store information about humming birds instead of age and weight of a human beeing...
-
mre over 12 yearsHmm..confusing. Sounds like a poor design to me.
-
Antje Janosch over 12 yearsnot sure, how this could help... ? what would the hash map contain?
-
Antje Janosch over 12 yearshmm, it's just like providing an empty excel-sheet - you won't know before what the user will put inside but you have to handle it in the right way depending on the data type....
-
Mansuro over 12 yearsthe hashmap would contain for example Name as key and Oscar as value, etc
-
Antje Janosch over 12 yearsbut then every value would be string? The key being a string is fine but the value should be of different possible types.
-
Antje Janosch over 12 yearsThanks a lot! This is the solution I was looking for. But I'll also think about whether there is any less general way. I did not expect this task to be so difficult.
-
David Conrad over 12 yearsI suppose a
Map<String, Object>
might work. And then aList
of those, for the row.