Cast a List<> in java?
<p>Hi is it possible to cast a List?</p>

<p>i have an abstract class that has a method that takes some sort of List&lt;>, iterate through it in a for loop getting each object in that list and calling the insertItem abstract method that is implemented by the sub class to basically pull out the proper data in the items and then finaly inserting them into a database table.</p>

<p>here is the super class method:</p>

<pre><code>protected void insertAllItemsToDb(List&lt;Object&gt; items, String table) {
        // open db and table

        database().beginTransaction();
        // clear all data from table
        clearTable(table);
        // call a insert statement to insert each column from an item
        for (Object object : items) {
            insertItem(object, table);
        }
        // close db
        database().endTransaction();
        database().close();
    }
</code></pre>

<p>In the subclass here is one of the override methods: Which i am able to cast the object fine here.</p>

<pre><code>  @Override
    protected void insertItem(Object object, String table) {

        CalendarEventItem item = (CalendarEventItem) object;
        eventItemValue = new ContentValues();

        eventItemValue.put(LABEL_EVENTS_TITLE, item.getEventTitle());
        eventItemValue.put(LABEL_EVENTS_LOCATION, item.getEventLocation());
        eventItemValue.put(LABEL_EVENTS_DATE, item.getEventStartTime()
                .getDate());
        eventItemValue.put(LABEL_EVENTS_TIME, item.getEventStartTime()
                .getTime());
        eventItemValue.put(LABEL_EVENTS_TIMEZONE, item.getEventStartTime()
                .getTimeZone());

        database.insert(TABLE_NAME_EVENTS, null, eventItemValue);

    }
</code></pre>

<p>i then call that method from the superclass using this:</p>

<pre><code>events =  (List&lt;CalendarEventItem&gt;) items;
        insertAllItemsToDb(events, TABLE_NAME_EVENTS);
</code></pre>

<p>But i recieve a compile error saying you cant cast it. Any ideas on how i can achieve this without having to duplicate the same steps and code you see inside the insertAllItemsToDb()</p>
