Java List Cast cast(List list, Class klass)

Here you can find the source of cast(List list, Class klass)

Description

Verifies that the passed list contains only elements of the given type, and returns it as a parameterized type.

License

Apache License

Declaration

@SuppressWarnings("unchecked")
public static <T> List<T> cast(List<?> list, Class<T> klass) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.List;

import java.util.Set;

public class Main {
    /**/*from w  w  w. j  ava 2  s.com*/
     *  Verifies that the passed list contains only elements of the given
     *  type, and returns it as a parameterized type. Throws if any element
     *  is a different type.
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> cast(List<?> list, Class<T> klass) {
        for (Object obj : list) {
            klass.cast(obj);
        }
        return (List<T>) list;
    }

    /**
     *  Verifies that the passed set contains only elements of the given
     *  type, and returns it as a parameterized type. Throws if any element
     *  is a different type.
     */
    @SuppressWarnings("unchecked")
    public static <T> Set<T> cast(Set<?> set, Class<T> klass) {
        for (Object obj : set) {
            klass.cast(obj);
        }
        return (Set<T>) set;
    }
}

Related

  1. cast(final Collection list)
  2. cast(final List list)
  3. cast(List list)
  4. cast(List list)
  5. cast(List list)
  6. cast(List from)
  7. CAST_LIST(Class className, Object obj)
  8. castList(Class clazz, Collection c)
  9. castList(Class clazz, Collection c)