Adam Adam - 2 months ago 16
Java Question

How to create a mutable array list containing immutable objects, Java

I have objects representing columns in a table that I want to declare as final. Some of these columns represent primary keys in my table, and for those columns I want to add them to an array list that gets passed into a function.

How can I declare a mutable list (one that I can add primary key columns to) that contains immutable objects (the column objects themselves)?

Answer
public final class Column {

private final Integer a;
private final String  b;

public Column(Integer a, String b) {
    this.a = a;
    this.b = b;
}

public Integer getA() {
    return a;
}

public String getB() {
    return b;
}

}

Create column class in this fashion and then add the object to ArrayList. Check this out : http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html