Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.*;

public class Main {
    /**
     * Returns all objects of given type and its subtypes
     * @param clazz The class you want to return all of the instances of.
     * @param objects The collection you'd like to search.
     * @return A List of all of the instances of a specific Class found in a Collection.
     */
    public static List getAllInstances(Class clazz, Collection objects) {
        List allInstances = new ArrayList();
        if (objects != null && clazz != null) {
            Iterator objectsIterator = objects.iterator();
            while (objectsIterator.hasNext()) {
                Object instance = objectsIterator.next();
                if (instance != null) {
                    if (clazz.isAssignableFrom(instance.getClass())) {
                        allInstances.add(instance);
                    }
                }
            }
        }
        return allInstances;
    }
}