Java tutorial
//package com.java2s; import java.lang.reflect.ParameterizedType; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; public class Main { public static <T> List<T> getRandomCollection(Class<T> collectionType) { int collectionSize = new Random().nextInt(10); List<T> collection = new ArrayList<T>(); for (int i = 0; i < collectionSize; i++) { Object obj = getRandomValue(collectionType); if (obj != null && obj.getClass().isAssignableFrom(collectionType)) { collection.add((T) obj); } } return collection; } private static Object getRandomValue(Class<?> type) { if (type.isAssignableFrom(Double.class)) { return new Random().nextDouble(); } if (type.isAssignableFrom(Short.class)) { return (short) new Random().nextInt(); } if (type.isAssignableFrom(Long.class)) { return new Random().nextLong(); } if (type.isAssignableFrom(Integer.class)) { return new Random().nextInt(); } if (type.isAssignableFrom(String.class)) { return "" + new Random().nextInt(); } if (type.isAssignableFrom(Boolean.class)) { return new Random().nextBoolean(); } if (type.isAssignableFrom(Date.class)) { return new Date(new Random().nextLong()); } if (type.isAssignableFrom(List.class)) { Class<?> listType = (Class<?>) ((ParameterizedType) type.getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; return getRandomCollection(listType); } return null; } }