polymorphism with object ArrayList I've an ArrayList of Object that could contain String, Date and Long. The Object list is nearly mandatory. (I obtain this list through reflection, reading all the member variables of an unknown class.)

    ArrayList &lt;Object&gt; values = new ArrayList&lt;Object&gt;();


I build dynamically a query for Alfresco cmis from it:

        addObjectToQueryStatement(qs, i+1, values.get(i));


I came only with this idea:

private void addObjectToQueryStatement(QueryStatement qs, int parameterIndex, Object value)
{
    if (value instanceof String)
    {
        qs.setString(parameterIndex, (String) value);
    }
    else if (value instanceof Date)
    {
        qs.setDateTime(parameterIndex, (Date)value);
    }
    else if (value instanceof Long)
    {
        qs.setNumber(parameterIndex, (Long)value);
    }
}


Does it exist a better and more elegant implementation?
This is missing the point of polymorphism. Its great strength is that you don't need to know or care about the type. With your code, you still need to check type using the instanceof operator. 

One way to resolve this is using a variation on the command pattern. First, define a common interface (I used an abstract class) that all three implementations can inherit from. Let call it QueryParameter:

abstract class QueryParameter {
    protected final int index;

    public QueryParameter(int index) {
        this.index = index;
    }

    public abstract void addTo(QueryStatement queryStatement);

}


Now you define three implementations. Here is the String version:

class StringParameter extends QueryParameter {
    private final String value;

    public StringParameter(String value, int index) {
        super(index);
        this.value = value;
    }

    @Override
    public void addTo(QueryStatement queryStatement) {
        queryStatement.setString(index, value);
    }

}


Now, you may construct an arbitrary number of parameters and save them in a collection. Once you've got your QueryStatement ready, simply call the addTo() methods of every parameter in no particular order and you're done!

final Set&lt;QueryParameter&gt; params = new HashSet&lt;&gt;();
//Add bunch of parameters.
params.stream().forEach((param) -&gt; param.addTo(queryStatement));


Of course there are many ways to go about doing this, my example is not necessarily the best fit for you.