Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing different types of elements in a List in Java

I'm trying to develop a general table loader which schema is known at runtime. This requires having a class which contains a list of different types of elements and supports various get and set method such as getInt(int index), asString(int index), asStringList(int index). The types of elements I consider are Integer, Double, String, and List<Integer>, List<Double> and List<String>. The actual type of each element is known in run time, and I will store them in a List describing its schema for further processing.

My question is: should I store such list of elements in List<Object> or List<? extends Object>? or is there better way to implement such class?

like image 636
keelar Avatar asked Jun 14 '13 21:06

keelar


People also ask

Can we store different data types in list in Java?

Yes we can store different/mixed types in a single array by using following two methods: Method 1: using Object array because all types in . net inherit from object type Ex: object[] array=new object[2];array[0]=102;array[1]="csharp";Method 2: Alternatively we can use ArrayList class present in System.

Can a list contains different types of elements Java?

You can add any Java object to a List . If the List is not typed, using Java Generics, then you can even mix objects of different types (classes) in the same List . Mixing objects of different types in the same List is not often done in practice, however.

Can a list contains different types of elements?

Answer. A list in Python CAN contain different types of data. Each item in the list is separated by a comma and the entire list is enclosed in square brackets [] .

Can list hold different types?

A list can only hold the items of the datatype of its declared datatype. We can declare a list of datatype Object and store items of different datatype. But after accessing the item we do need to cast to the right datatype.


1 Answers

Since the common ancestor of your classes is Object, and because List<? extends Object> does not make things any cleaner (after all, everything extends Object) it looks like List<Object> would be an OK choice.

However, such list would be a mixed bag: you would need to check run-time type of the object inside, and make decisions based on that. This is definitely not a good thing.

A better alternative would be creating your own class that implements operations on elements of the list the uniform way, and make one subclass for each subtype that implements these operations differently. This would let you treat the list in a uniform way, pushing the per-object differentiation into your wrappers.

public interface ItemWrapper {
    int calculateSomething();
}

public abstract class IntWrapper implements ItemWrapper {
    private int value;

    public IntWrapper(int v) {
      value=v; 
    }

    public int calculateSomething() {
      return value;
    }
}

public abstract class DoubleListWrapper implements ItemWrapper {
    private List<Double> list;

    public DoubleListWrapper (List<Double> lst) {
      list = lst; 
    }

    public int calculateSomething() {
        int res;
        for (Double d : list) {
            res += d;
        }

        return res;
    }
}
// ...and so on

Now you can make a list of ItemWrapper objects, and calculateSomething on them without checking their type:

List<ItemWrapper> myList = new ArrayList<ItemWrapper>();

for (ItemWrapper w : myList) {
    System.out.println(
      w.calculateSomething());
}
like image 126
Sergey Kalinichenko Avatar answered Oct 12 '22 23:10

Sergey Kalinichenko