Utilities for generic ArrayList : Generic Collections « Generics « Java Tutorial

import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.GregorianCalendar;

public class UtilsTester {
  public static void main(String[] args) throws InstantiationException,
      IllegalAccessException {
    ArrayList<String> ids = new ArrayList<String>();
    Utils.fill(ids, "default", 10);

    ArrayList<Shape> shapes = new ArrayList<Shape>();
    Utils.fill(shapes, new Rectangle(5, 10, 20, 30), 2);

    ArrayList<Polygon> polys = new ArrayList<Polygon>();
    Utils.fillWithDefaults(polys, Polygon.class, 10);
    Utils.append(shapes, polys, 2);

    ArrayList<GregorianCalendar> dates = new ArrayList<GregorianCalendar>();
    Utils.fillWithDefaults(dates, GregorianCalendar.class, 5);

class Utils {
  public static <E> void fill(ArrayList<E> a, E value, int count) {
    for (int i = 0; i < count; i++)

  public static <E, F extends E> void append(ArrayList<E> a, ArrayList<F> b,
      int count) {
    for (int i = 0; i < count && i < b.size(); i++)

  public static <E extends Comparable<? super E>> E getMax(ArrayList<E> a) {
    E max = a.get(0);
    for (int i = 1; i < a.size(); i++)
      if (a.get(i).compareTo(max) > 0)
        max = a.get(i);
    return max;

  public static <E> void fillWithDefaults(ArrayList<E> a,
      Class<? extends E> cl, int count) throws InstantiationException,
      IllegalAccessException {
    for (int i = 0; i < count; i++)

12.2.Generic Collections
12.2.1.Generics and Collections: ArrayList
12.2.2.Arrays: Storing class objects in Array as data items
12.2.3.Using Generic Comparable interface
12.2.4.A generic first-in, first-out bounded collection of objects
12.2.5.A list declared to hold objects of a type T can also hold objects that extend from T.
12.2.6.Utilities for generic ArrayList
12.2.7.Your own tree with generic user object
12.2.8.Generic to list
12.2.9.Create a typesafe copy of a raw list.
12.2.10.Create a typesafe copy of a raw map.
12.2.11.Create a typesafe filter of an unchecked iterator.
12.2.12.Create a typesafe view over an underlying raw set.
12.2.13.Create a typesafe view over an underlying raw map.
12.2.14.Create a typesafe filter of an unchecked enumeration.