get size of the object. - Android java.lang.reflect

Android examples for java.lang.reflect:Object Type

Description

get size of the object.

Demo Code


//package com.java2s;

import java.util.Collection;

import java.util.Map;

public class Main {
    /**/*from  w w  w.jav  a2  s  .  com*/
     * get size of the object. if type is collection or map, then it will return it's size.
     * if it's array, it will return length.
     * otherwise it will return -1.
     */
    public static int getSize(Object object) {
        if (object == null) {
            return 0;
        } else if (object instanceof Collection<?>) {
            return ((Collection<?>) object).size();
        } else if (object instanceof Map<?, ?>) {
            return ((Map<?, ?>) object).size();
        } else if (object instanceof Object[]) {
            return ((Object[]) object).length;
        } else if (object instanceof int[]) {
            return ((int[]) object).length;
        } else if (object instanceof long[]) {
            return ((long[]) object).length;
        } else if (object instanceof float[]) {
            return ((float[]) object).length;
        } else if (object instanceof double[]) {
            return ((double[]) object).length;
        } else if (object instanceof short[]) {
            return ((short[]) object).length;
        } else if (object instanceof byte[]) {
            return ((byte[]) object).length;
        } else if (object instanceof char[]) {
            return ((char[]) object).length;
        } else if (object instanceof boolean[]) {
            return ((boolean[]) object).length;
        } else {
            return -1;
        }
    }
}

Related Tutorials