Java Collection to Array toArray(final Collection coll, byte[] array)

Here you can find the source of toArray(final Collection coll, byte[] array)

Description

to Array

License

Open Source License

Declaration

public static byte[] toArray(final Collection<Byte> coll, byte[] array) 

Method Source Code


//package com.java2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static byte[] toArray(final Collection<Byte> coll, byte[] array) {
        if (array.length < coll.size()) {
            array = new byte[coll.size()];
        }//ww w . ja  v  a2s  .c  o m
        final Iterator<Byte> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().byteValue();
        }
        return array;
    }

    public static short[] toArray(final Collection<Short> coll, short[] array) {
        if (array.length < coll.size()) {
            array = new short[coll.size()];
        }
        final Iterator<Short> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().shortValue();
        }
        return array;
    }

    public static int[] toArray(final Collection<Integer> coll, int[] array) {
        if (array.length < coll.size()) {
            array = new int[coll.size()];
        }
        final Iterator<Integer> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().intValue();
        }
        return array;
    }

    public static long[] toArray(final Collection<Long> coll, long[] array) {
        if (array.length < coll.size()) {
            array = new long[coll.size()];
        }
        final Iterator<Long> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().longValue();
        }
        return array;
    }

    public static float[] toArray(final Collection<Float> coll, float[] array) {
        if (array.length < coll.size()) {
            array = new float[coll.size()];
        }
        final Iterator<Float> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().floatValue();
        }
        return array;
    }

    public static double[] toArray(final Collection<Double> coll, double[] array) {
        if (array.length < coll.size()) {
            array = new double[coll.size()];
        }
        final Iterator<Double> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().doubleValue();
        }
        return array;
    }

    public static boolean[] toArray(final Collection<Boolean> coll, boolean[] array) {
        if (array.length < coll.size()) {
            array = new boolean[coll.size()];
        }
        final Iterator<Boolean> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().booleanValue();
        }
        return array;
    }

    public static char[] toArray(final Collection<Character> coll, char[] array) {
        if (array.length < coll.size()) {
            array = new char[coll.size()];
        }
        final Iterator<Character> it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            array[i++] = it.next().charValue();
        }
        return array;
    }
}

Related

  1. toArray(Collection values)
  2. toArray(Collection c, T[] sample)
  3. toArray(Collection collection)
  4. toArray(Collection list, Object[] type)
  5. toArray(final Collection c)
  6. toArray(final Collection collection)
  7. toArray(final Collection collection)
  8. toArray(java.util.Collection collection)
  9. toArray(T arrayOrCollection)