一些小工具方法,能将容器转换成指定的数组类型有使用泛型
来源:互联网 发布:网络上流行的英文歌曲 编辑:程序博客网 时间:2024/06/06 17:26
如果大家有更好的方法,请回帖
/** *一个小工具类 *@author Hf *@mailto:huangfei8087@163.com * 在编程的过程中很多时候都希望能将 容器直接转成成为指定的数据,比如List<Integer>ids = new ArrayList<Integer>();则每次都需要手动创建一个Integer数组,然后在赋值。 collectionToArray 这个方法就实现了这个功能,可直接转换成为Integer数组*/public class Helper{/** *判断去空的方法 *@author Hf *@mailto:huangfei8087@163.com **/public static boolean isNull(Object value){if(value == null){return true ; }if(value.getClass().isArray()){if(Array.getLength(value) == 0){return true ; }}if(value instanceof Collection<?>){Collection<?> collection = (Collection<?>) value ;if(collection.isEmpty()){return true ;}}else if(value instanceof Map<?, ?>){Map<?, ?> map = (Map<?, ?>) value ;if(map.isEmpty()){return true ;}}else if(value instanceof String){String string = (String) value ;return isNull(string) ;}return false; }/** * 判断String类型是否为空 *@author Hf *@mailto:huangfei8087@163.com * */public static boolean isNull(String value){if(value == null){return true ;}if("".equals(value.trim())){return true ;}return false ;}/** *去掉数组中为NULL 的字段 *@author Hf *@mailto:huangfei8087@163.com * */public static <T> List<T> checkNull(T[] objs){if(isNull(objs)){return null;}List<T> objList = new ArrayList<T>(); for(T obj : objs){if(!isNull(obj)){objList.add(obj) ; }}if(isNull(objList)){return null;}return objList ; }/** *将容器转换成为数组 *@author Hf *@mailto:huangfei8087@163.com * */public static <T> T[] collectionToArray(Collection<T> coll){ T[] ts = null ;try {if(coll == null || coll.isEmpty()){return ts;}ts = collectionToArray(coll , null ) ;} catch (Exception e) {try {ts = collectionToArray(coll , Object.class ) ; } catch (Exception e2) { e2.printStackTrace( ) ; }}return ts ; }/** * 将容器转换成为指定数组 *@author Hf *@mailto:huangfei8087@163.com * */ public static <T> T[] collectionToArray(Collection<T> coll , Class<?> clazz){Iterator<T> iterator = coll.iterator() ;T[] ts = null ;int x=0 ;while(iterator.hasNext()){T tempT = iterator.next() ; if(x == 0){ts = (T[])Array.newInstance(clazz != null ? clazz : tempT.getClass() , coll.size()) ;}ts[x++] = tempT ; }return ts ; }}
个人主页:http://my.csdn.net/hfmbook
- 一些小工具方法,能将容器转换成指定的数组类型有使用泛型
- 将html代码转换成js字符串的小工具
- java类型转换小工具
- Linux程序设计的一些小工具使用
- 将C++转换成VB,C#语言小工具介绍
- 小工具的使用
- 一些不错的小工具
- 找工作的一些小工具
- 一些有用的-------小工具
- php小工具类php下将XML转换为数组
- rust 用指针类型转换的方法将u8数组(或slice)转换成u32
- 完成txt转换成pdf的小工具
- excel表格数据转换成json数据的小工具
- 【小工具】仿站小工具的使用及下载
- 将object类型转换成string类型的数组
- 一些非常好的小工具介绍
- android一些很有用的小工具
- android系统的 一些小工具类
- VS中Tab Control用法
- OpenCV1.1无法读视频文件的解决方法
- chrome UI 学习笔记3--国际化 (转)
- Linux curses库使用
- 数组排序 --- 庞果
- 一些小工具方法,能将容器转换成指定的数组类型有使用泛型
- Log4cpp使用
- jQuery验证控件jquery.validate.js使用说明+中文API
- windows server2008r2 下载
- 设计模式之原型模式
- Linux_寄存器的读取
- Win8右键新建菜单顺序更改教程
- 在redhat5.8 上安装oracle10g
- POJ1004