首页 > 技术文章 > Java开发 小工具累计

codedreams 2018-11-13 20:58 原文

array to list

Integer[] spam = new Integer[] { 1, 2, 3 };
List<Integer> rlt = Arrays.asList(spam);

Object[] to list<T>

List<Object> objects = getObjects();
List<CustomClass> customObjects = myFilter(objects, CustomClass.class);

static <E> List<E> myFilter(List<?> lst, Class<E> cls) {
    List<E> result = new ArrayList<E>();
    for (Object obj : lst) {
        if (cls.isInstance(obj))
            result.add(cls.cast(obj));
    }
    return result;
}

 

推荐阅读