Here you can find the source of toArray(Collection
public static long[] toArray(Collection<Long> collection, Long defaultValue)
//package com.java2s; import java.util.Arrays; import java.util.Collection; public class Main { public static long[] toArray(Collection<Long> collection, Long defaultValue) { if (collection == null) return new long[0]; long[] ret = new long[collection.size()]; int count = 0; for (Long element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue;//from ww w. j a v a2 s . c om } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } public static int[] toArray(Collection<Integer> collection, Integer defaultValue) { if (collection == null) return new int[0]; int[] ret = new int[collection.size()]; int count = 0; for (Integer element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue; } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } public static char[] toArray(Collection<Character> collection, Character defaultValue) { if (collection == null) return new char[0]; char[] ret = new char[collection.size()]; int count = 0; for (Character element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue; } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } public static short[] toArray(Collection<Short> collection, Short defaultValue) { if (collection == null) return new short[0]; short[] ret = new short[collection.size()]; int count = 0; for (Short element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue; } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } public static byte[] toArray(Collection<Byte> collection, Byte defaultValue) { if (collection == null) return new byte[0]; byte[] ret = new byte[collection.size()]; int count = 0; for (Byte element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue; } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } public static boolean[] toArray(Collection<Boolean> collection, Boolean defaultValue) { if (collection == null) return new boolean[0]; boolean[] ret = new boolean[collection.size()]; int count = 0; for (Boolean element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue; } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } public static double[] toArray(Collection<Double> collection, Double defaultValue) { if (collection == null) return new double[0]; double[] ret = new double[collection.size()]; int count = 0; for (Double element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue; } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } public static float[] toArray(Collection<Float> collection, Float defaultValue) { if (collection == null) return new float[0]; float[] ret = new float[collection.size()]; int count = 0; for (Float element : collection) { if (element == null) { if (defaultValue == null) continue; element = defaultValue; } ret[count++] = element; } if (count == ret.length) { return ret; } return Arrays.copyOf(ret, count); } }