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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM