Java List from Array arrayToStringList(Object array)

Here you can find the source of arrayToStringList(Object array)

Description

array To String List

License

Open Source License

Declaration

public static List<String> arrayToStringList(Object array) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Arrays;

import java.util.Collections;
import java.util.List;

public class Main {
    public static List<String> arrayToStringList(Object array) {
        if (array == null)
            return null;
        if (!array.getClass().isArray())
            return null;
        Class<?> componentType = array.getClass().getComponentType();
        if (componentType == null)
            return null;
        if (String.class.isAssignableFrom(componentType)) {
            List<String> ret = Arrays.asList((String[]) array);
            return ret;
        }/* www  .j  a  va  2  s.  c o m*/
        List<?> list = null;
        if (Object.class.isAssignableFrom(componentType)) {
            list = Arrays.asList((Object[]) array);
        } else if (long.class.isAssignableFrom(componentType)) {
            list = asList((long[]) array);
        } else if (int.class.isAssignableFrom(componentType)) {
            list = asList((int[]) array);
        } else if (char.class.isAssignableFrom(componentType)) {
            list = asList((char[]) array);
        } else if (short.class.isAssignableFrom(componentType)) {
            list = asList((short[]) array);
        } else if (byte.class.isAssignableFrom(componentType)) {
            list = asList((byte[]) array);
        } else if (boolean.class.isAssignableFrom(componentType)) {
            list = asList((boolean[]) array);
        } else if (double.class.isAssignableFrom(componentType)) {
            list = asList((double[]) array);
        } else if (float.class.isAssignableFrom(componentType)) {
            list = asList((float[]) array);
        }
        if (list == null) {
            return null;
        }
        List<String> ret = new ArrayList<String>(list.size());
        for (Object e : list) {
            if (e == null)
                continue;
            ret.add(String.valueOf(e));
        }
        return ret;
    }

    public static List<Long> asList(long... array) {
        if (array == null)
            return Collections.emptyList();
        List<Long> ret = new ArrayList<Long>(array.length);
        for (long element : array) {
            ret.add(element);
        }
        return ret;
    }

    public static List<Integer> asList(int... array) {
        if (array == null)
            return Collections.emptyList();
        List<Integer> ret = new ArrayList<Integer>(array.length);
        for (int element : array) {
            ret.add(element);
        }
        return ret;
    }

    public static List<Character> asList(char... array) {
        if (array == null)
            return Collections.emptyList();
        List<Character> ret = new ArrayList<Character>(array.length);
        for (char element : array) {
            ret.add(element);
        }
        return ret;
    }

    public static List<Short> asList(short... array) {
        if (array == null)
            return Collections.emptyList();
        List<Short> ret = new ArrayList<Short>(array.length);
        for (short element : array) {
            ret.add(element);
        }
        return ret;
    }

    public static List<Byte> asList(byte... array) {
        if (array == null)
            return Collections.emptyList();
        List<Byte> ret = new ArrayList<Byte>(array.length);
        for (byte element : array) {
            ret.add(element);
        }
        return ret;
    }

    public static List<Boolean> asList(boolean... array) {
        if (array == null)
            return Collections.emptyList();
        List<Boolean> ret = new ArrayList<Boolean>(array.length);
        for (boolean element : array) {
            ret.add(element);
        }
        return ret;
    }

    public static List<Double> asList(double... array) {
        if (array == null)
            return Collections.emptyList();
        List<Double> ret = new ArrayList<Double>(array.length);
        for (double element : array) {
            ret.add(element);
        }
        return ret;
    }

    public static List<Float> asList(float... array) {
        if (array == null)
            return Collections.emptyList();
        List<Float> ret = new ArrayList<Float>(array.length);
        for (float element : array) {
            ret.add(element);
        }
        return ret;
    }
}

Related

  1. arrayToList(T[] array)
  2. arrayToList(T[] obj, List newList)
  3. arrayToList(T[] source)
  4. arrayToListWithUpperCase(String[] arrValues)
  5. arrayToString(List arr)
  6. asList()
  7. asList(boolean[] array)
  8. asList(byte[] array)
  9. asList(Class... classesArray)