Java常用工具类

摘要 工具类是Java开发过程中使用广泛,而且还能提高开发效率的一种方法。

集合操作工具类: 1.判断是否为空:isEmpty() 2.判断是否非空:isNotEmpty()

import org.apache.commons.collections.CollectionUtils;
         import java.util.Collection;
              public class CollectionUtil {
                  /**
                   * 判断集合是否非空
                   */
                  public static boolean isNotEmpty(Collection<?> collection) {
                      return CollectionUtils.isNotEmpty(collection);
                  }

                  /**
                   * 判断集合是否为空
                   */
                  public static boolean isEmpty(Collection<?> collection) {
                      return CollectionUtils.isEmpty(collection);
                  }
              }

类加载器工具类: 1.获取类加载器:Thread.currentThread().getContextClassLoader() 2.获取类路径: getClassPath() 3.加载类文件 4.异常处理 5.判断类型码

import java.net.URL;
            public class ClassUtil {
                /**
                 * 获取类加载器:<code>Thread.currentThread().getContextClassLoader()</code>
                 */
                public static ClassLoader getClassLoader() {
                    return Thread.currentThread().getContextClassLoader();
                }

                /**
                 * 获取类路径
                 */
                public static String getClassPath() {
                    String classpath = "";
                    URL resource = getClassLoader().getResource("");
                    if (resource != null) {
                        classpath = resource.getPath();
                    }
                    return classpath;
                }

                /**
                 * 加载类(将自动初始化)
                 */
                public static Class<?> loadClass(String className) {
                    return loadClass(className, true);
                }

                /**
                 * 加载类
                 */
                public static Class<?> loadClass(String className, boolean isInitialized) {
                    try {
                        return Class.forName(className, isInitialized, getClassLoader());
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException("加载类出错:" + e.getMessage(), e);
                    }
                }

                /**
                 * 是否为 int 类型(包括 Integer 类型)
                 */
                public static boolean isInt(Class<?> type) {
                    return type.equals(int.class) || type.equals(Integer.class);
                }

                /**
                 * 是否为 long 类型(包括 Long 类型)
                 */
                public static boolean isLong(Class<?> type) {
                    return type.equals(long.class) || type.equals(Long.class);
                }

                /**
                 * 是否为 double 类型(包括 Double 类型)
                 */
                public static boolean isDouble(Class<?> type) {
                    return type.equals(double.class) || type.equals(Double.class);
                }

                /**
                 * 是否为 String 类型
                 */
                public static boolean isString(Class<?> type) {
                    return type.equals(String.class);
                }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值