cast Collection element by class - Java java.util

Java examples for java.util:Collection Element

Description

cast Collection element by class

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "java2s.com");
        Class clazz = String.class;
        System.out.println(cast(collection, clazz));
    }//from ww  w  .j  a  v a 2  s  .  c  o  m

    public static <T> Collection<T> cast(Collection<? super T> collection,
            Class<T> clazz) {
        return (Collection<T>) collection;
    }
}

Related Tutorials