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.List;

import java.util.stream.Collectors;

public class Main {
    public static <T> List<T> ofType(List<Object> list, Class<T> targetClass) {
        List<T> result = new ArrayList<T>();

        for (Object object : list) {
            boolean isTargetClass = object.getClass().equals(targetClass);
            if (isTargetClass) {
                result.add(targetClass.cast(object));
            }
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    public static <TargetType, CurrentType> List<TargetType> cast(List<CurrentType> list,
            Class<TargetType> targetClass) {
        return list.stream().map(e -> (TargetType) e).collect(Collectors.toList());
    }
}