java implementation (list with different data types)

10,855

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>();
Share:
10,855
Antje Janosch
Author by

Antje Janosch

Updated on July 21, 2022

Comments

  • Antje Janosch
    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
    Antje Janosch over 12 years
    but 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
    Antje Janosch over 12 years
    as 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
    mre over 12 years
    Hmm..confusing. Sounds like a poor design to me.
  • Antje Janosch
    Antje Janosch over 12 years
    not sure, how this could help... ? what would the hash map contain?
  • Antje Janosch
    Antje Janosch over 12 years
    hmm, 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
    Mansuro over 12 years
    the hashmap would contain for example Name as key and Oscar as value, etc
  • Antje Janosch
    Antje Janosch over 12 years
    but then every value would be string? The key being a string is fine but the value should be of different possible types.
  • Antje Janosch
    Antje Janosch over 12 years
    Thanks 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
    David Conrad over 12 years
    I suppose a Map<String, Object> might work. And then a List of those, for the row.