2

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?

4 Answers 4

3

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>();
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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...
Hmm..confusing. Sounds like a poor design to me.
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....
2

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.")

1 Comment

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.
0

You can use a HashMap

 Map<String, String> m = new HashMap<String, String>();

4 Comments

not sure, how this could help... ? what would the hash map contain?
the hashmap would contain for example Name as key and Oscar as value, etc
but then every value would be string? The key being a string is fine but the value should be of different possible types.
I suppose a Map<String, Object> might work. And then a List of those, for the row.
0

If you don't want to create a class. Try something like this.

ArrayList<List> arrayList = new ArrayList<>();
arrayList.add(Arrays.asList("String 1", 2011.57, 5));
arrayList.add(Arrays.asList("String 2", 201.01, 16));
arrayList.add(Arrays.asList("String 3", 2012, NULL));
arrayList.add(Arrays.asList("String 4", 20.1, 19));

for (List list : arrayList) {
    System.out.println(
             list.get(0) + " " + list.get(1) + " " + list.get(2));
}

In the above example. You can create an ArrayList which stores a Collection of List with {String, Double, Integer} datatypes, respectively.

NOTE: It's bad practice in Java to create a list of different types

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.