Convert collection to array

In contrast to converting an array to list, this example will convert a collection to an array while using java 8, guava and apache libraries.

Straight up Java

Objects

@Test
public void convert_collection_of_strings_to_array_with_java () {

    List<String> searchEnginesAsList = Lists.newArrayList(
        "Google", "Bing", "Yahoo", "Ask", "AOL"); 
    
    String[] searchEngines = searchEnginesAsList
        .toArray(new String[searchEnginesAsList.size()]);
    
    logger.info(Arrays.toString(searchEngines));
    
    assertEquals(5, searchEngines.length);
    
}

Output

[Google, Bing, Yahoo, Ask, AOL]

Primitives

@Test
public void convert_collection_of_primitives_to_array_with_java () {
    
    List<Double> searchEngineMarketShare = Lists.newArrayList();
    searchEngineMarketShare.add(67.1);
    searchEngineMarketShare.add(16.9);
    searchEngineMarketShare.add(11.8);
    searchEngineMarketShare.add(2.7);
    searchEngineMarketShare.add(1.6);
    
    double [] searchEngineMarketShareArray = new double[searchEngineMarketShare.size()];
    for (int x=0; x <= searchEngineMarketShare.size() - 1; x ++) {
        searchEngineMarketShareArray[x] = searchEngineMarketShare.get(x);
    }
    
    logger.info(Arrays.toString(searchEngineMarketShareArray));

    assertEquals(5, searchEngineMarketShareArray.length);
}

Output

[67.1, 16.9, 11.8, 2.7, 1.6]

Java 8

In java 8, a common operation of java.util.stream is to convert elements to a primitive stream of type Intstream, DoubleStream or LongStream which can accomplished by calling a specialized method stream.mapToInt, stream.mapToDouble and stream.mapToLong respectively. Here, the mapToDouble method is called that returns a DoubleStream and applies the Double.doubleValue to each element returning an array of doubles.

Primitives

@Test
public void convert_collection_of_primitives_to_array_with_java8 () {
    
    List<Double> searchEngineMarketShare = Lists.newArrayList();
    searchEngineMarketShare.add(67.1);
    searchEngineMarketShare.add(16.9);
    searchEngineMarketShare.add(11.8);
    searchEngineMarketShare.add(2.7);
    searchEngineMarketShare.add(1.6);

    double [] searchEngineMarketShareArray = searchEngineMarketShare
            .stream()
            .mapToDouble(Double::doubleValue)
            .toArray();
    
    logger.info(Arrays.toString(searchEngineMarketShareArray));

    assertEquals(5, searchEngineMarketShareArray.length);
}

Output

[67.1, 16.9, 11.8, 2.7, 1.6]

Google Guava

Guava's Doubles class provides static utility methods that pertain to double primitives that are not already found in either Double or Arrays. By calling the Doubles.toArray we convert each value to a double and then return an array containing the values of searchEngineMarketShare.

Primitives

@Test
public void convert_collection_of_objects_to_primitive_array_with_guava () {
    
    List<Double> searchEngineMarketShare = Lists.newArrayList();
    searchEngineMarketShare.add(67.1);
    searchEngineMarketShare.add(16.9);
    searchEngineMarketShare.add(11.8);
    searchEngineMarketShare.add(2.7);
    searchEngineMarketShare.add(1.6);

    double[] searchEngineMarketShareArray = Doubles.toArray(searchEngineMarketShare);
    
    logger.info(Arrays.toString(searchEngineMarketShareArray));
    
    assertEquals(5, searchEngineMarketShareArray.length);
    
}

Output

[67.1, 16.9, 11.8, 2.7, 1.6]

Apache Commons

Using apache commons we first convert the list to a Double array and then call ArrayUtils.toPrimitive to convert to a primitive array.

Primitives

@Test
public void convert_collection_of_objects_to_array_with_apachecommons () {

    List<Double> searchEngineMarketShare = Lists.newArrayList();
    searchEngineMarketShare.add(67.1);
    searchEngineMarketShare.add(16.9);
    searchEngineMarketShare.add(11.8);
    searchEngineMarketShare.add(2.7);
    searchEngineMarketShare.add(1.6);

    Double[] searchEngineMarketShareDoubleArray = searchEngineMarketShare
            .toArray(new Double[searchEngineMarketShare.size()]);
    
    double[] searchEngineMarketShareArray = 
            ArrayUtils.toPrimitive(searchEngineMarketShareDoubleArray);

    logger.info(Arrays.toString(searchEngineMarketShareArray));
    
    assertEquals(5, searchEngineMarketShareArray.length);
}

Output

[67.1, 16.9, 11.8, 2.7, 1.6]