• JAVA ArrayUtils 数组工具类


    package com.sicdt.library.core.utils;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    
    /**
     * 
     * <br>类 名: 集合,数组工具 
     * <br>描 述: 描述类完成的主要功能 
     * <br>作 者: shizhenwei 
     * <br>创 建: 2017年5月15日 
     * <br>版 本: v0.0.2 
     * <br>
     * <br>历 史: (版本) 作者 时间 注释
     */
    public class ArrayUtils {
    
        /**
         * 判断是否为空
         * @param collection
         * @return
         */
        public static boolean isEmpty(Collection<?> collection){
            return collection == null || collection.isEmpty();
        }
        
        /**
         * 判断是否为空
         * @param map
         * @return
         */
        public static boolean isEmpty(Map<?, ?> map){
            return map == null || map.isEmpty();
        }
        
        /**
         * 判断是否为空
         * @param array
         * @return
         */
        public static boolean isEmpty(Object[] array){
            return array == null || array.length == 0;
        }
        
        /**
         * 判断是否为空
         * @param array
         * @return
         */
        public static boolean isEmpty(List<Object> array){
            return array == null || array.size() == 0;
        }
        
        public static boolean isArray(Object object) {
            if(object == null){
                return false;
            }
            return object.getClass().isArray();
        }
        
        public static boolean isCollection(Object object) {
            return object instanceof Collection;
        }
        
        @SuppressWarnings("unchecked")
        public static <T> T[] objectToArray(Object obj) {
            if(obj == null){
                return (T[])new Object[0];
            }
            if(isArray(obj)){
                return (T[])obj;
            }else if(isCollection(obj)){
                return (T[]) ((Collection<T>)obj).toArray();
            }
            return (T[]) new Object[]{obj};
        }
        
        @SuppressWarnings("unchecked")
        public static <T> List<T> objectToList(Object obj){
            if(obj == null){
                return Collections.emptyList();
            }
            if(isArray(obj)){
                return Arrays.asList((T[])obj);
            }else if(isCollection(obj)){
                return new ArrayList<T>((Collection<T>)obj);
            }
            List<T> list = new ArrayList<T>();
            list.add((T) obj);
            return list;
        }
        
        public static int size(Object obj){
            if(obj == null){
                return 0;
            }
            if(isArray(obj)){
                return ((Object[])obj).length;
            }
            if(isCollection(obj)){
                return ((Collection<?>)obj).size();
            }
            return -1;
        }
        
        public static <T> boolean contains(T[] array, Object obj){
            return Arrays.asList(array).contains(obj);
        }
    }
  • 相关阅读:
    10 个雷人的注释,就怕你不敢用!
    Java 14 之模式匹配,非常赞的一个新特性!
    poj 3661 Running(区间dp)
    LightOJ
    hdu 5540 Secrete Master Plan(水)
    hdu 5584 LCM Walk(数学推导公式,规律)
    hdu 5583 Kingdom of Black and White(模拟,技巧)
    hdu 5578 Friendship of Frog(multiset的应用)
    hdu 5586 Sum(dp+技巧)
    hdu 5585 Numbers
  • 原文地址:https://www.cnblogs.com/zwcry/p/8483182.html
Copyright © 2020-2023  润新知