简体   繁体   中英

java implementation (list with different data types)

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?

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>();

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

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

您可以使用HashMap

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM