Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    /**
     * filter list of random types and only return only the matching type
     *
     * @param <T>
     * @param clazz
     * @param values
     * @return list of <T>
     */
    public static <T> ArrayList<T> filterType(Class<T> clazz, Iterable<?> values) {
        ArrayList<T> list = new ArrayList<T>();
        for (Object o : values) {
            if (clazz.isInstance(o)) {
                list.add(clazz.cast(o));
            }
        }
        return list;
    }

    /**
     * filter list of random types and only return only the matching type
     *
     * @param <T>
     * @param clazz
     * @param values
     * @return list of <T>
     */
    public static <T> ArrayList<T> filterType(Class<T> clazz, Object[] values) {
        return filterType(clazz, Arrays.asList(values));
    }
}