Java Collection to Array convertToArray(Collection collection)

Here you can find the source of convertToArray(Collection collection)

Description

convert To Array

License

Open Source License

Declaration

public static Object convertToArray(Collection collection) 

Method Source Code


//package com.java2s;
/*//from  ww  w .  j av  a 2 s.  c o m
 * BackwardCompatibilityHelper.java
 *
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2016 "Tigase, Inc." <office@tigase.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    public static Object convertToArray(Collection collection) {
        Iterator iter = collection.iterator();
        if (!iter.hasNext())
            return null;

        Class objCls = iter.next().getClass();
        if (objCls == Integer.class) {
            return convertToIntArray(collection);
        } else if (objCls == Long.class) {
            return convertToLongArray(collection);
        } else if (objCls == Double.class) {
            return convertToDoubleArray(collection);
        } else if (objCls == Float.class) {
            return convertToFloatArray(collection);
        } else if (objCls == Boolean.class) {
            return convertToBoolArray(collection);
        } else if (objCls == String.class) {
            return convertToStringArray(collection);
        }
        return null;
    }

    public static Object convertToIntArray(Collection col) {
        int[] arr = new int[col.size()];
        int pos = 0;
        Iterator iter = col.iterator();
        while (iter.hasNext()) {
            Number v = (Number) iter.next();
            arr[pos++] = v.intValue();
        }
        return arr;
    }

    public static Object convertToLongArray(Collection col) {
        long[] arr = new long[col.size()];
        int pos = 0;
        Iterator iter = col.iterator();
        while (iter.hasNext()) {
            Number v = (Number) iter.next();
            arr[pos++] = v.longValue();
        }
        return arr;
    }

    public static Object convertToDoubleArray(Collection col) {
        double[] arr = new double[col.size()];
        int pos = 0;
        Iterator iter = col.iterator();
        while (iter.hasNext()) {
            Number v = (Number) iter.next();
            arr[pos++] = v.doubleValue();
        }
        return arr;
    }

    public static Object convertToFloatArray(Collection col) {
        float[] arr = new float[col.size()];
        int pos = 0;
        Iterator iter = col.iterator();
        while (iter.hasNext()) {
            Number v = (Number) iter.next();
            arr[pos++] = v.floatValue();
        }
        return arr;
    }

    public static Object convertToBoolArray(Collection col) {
        boolean[] arr = new boolean[col.size()];
        int pos = 0;
        Iterator iter = col.iterator();
        while (iter.hasNext()) {
            Boolean v = (Boolean) iter.next();
            arr[pos++] = v.booleanValue();
        }
        return arr;
    }

    public static Object convertToStringArray(Collection col) {
        String[] arr = new String[col.size()];
        int pos = 0;
        Iterator iter = col.iterator();
        while (iter.hasNext()) {
            String v = (String) iter.next();
            arr[pos++] = v;
        }
        return arr;
    }
}

Related

  1. collectionToArray(Collection collection)
  2. collectionToIntArray(Collection c)
  3. collectionToIntArray(final Collection numbers)
  4. collectionToObjectArray(Collection c)
  5. collectionToObjectArray(final Collection _list)
  6. convertToArray(Collection input)
  7. convertToArray(Collection items)
  8. toArray(Collection c)
  9. toArray(Collection coll)