How to pass an ArrayList to a varargs method parameter?

JavaVariadic Functions

Java Problem Overview


Basically I have an ArrayList of locations:

ArrayList<WorldLocation> locations = new ArrayList<WorldLocation>();

below this I call the following method:

.getMap();

the parameters in the getMap() method are:

getMap(WorldLocation... locations)

The problem I'm having is I'm not sure how to pass in the WHOLE list of locations into that method.

I've tried

.getMap(locations.toArray())

but getMap doesn't accept that because it doesn't accept Objects[].

Now if I use

.getMap(locations.get(0));

it will work perfectly... but I need to somehow pass in ALL of the locations... I could of course make keep adding locations.get(1), locations.get(2) etc. but the size of the array varies. I'm just not use to the whole concept of an ArrayList

What would be the easiest way to go about this? I feel like I'm just not thinking straight right now.

Java Solutions


Solution 1 - Java

Source article: Passing a list as an argument to a vararg method


Use the toArray(T[] arr) method.

.getMap(locations.toArray(new WorldLocation[0]))

Here's a complete example:

public static void method(String... strs) {
    for (String s : strs)
        System.out.println(s);
}

...
    List<String> strs = new ArrayList<String>();
    strs.add("hello");
    strs.add("world");
    
    method(strs.toArray(new String[0]));
    //     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

Solution 2 - Java

In Java 8:

List<WorldLocation> locations = new ArrayList<>();

.getMap(locations.stream().toArray(WorldLocation[]::new));

Solution 3 - Java

A shorter version of the accepted answer using Guava:

.getMap(Iterables.toArray(locations, WorldLocation.class));

can be shortened further by statically importing toArray:

import static com.google.common.collect.toArray;
// ...

    .getMap(toArray(locations, WorldLocation.class));

Solution 4 - Java

You can do:

getMap(locations.toArray(new WorldLocation[locations.size()]));

or

getMap(locations.toArray(new WorldLocation[0]));

or

getMap(new WorldLocation[locations.size()]);

@SuppressWarnings("unchecked") is needed to remove the ide warning.

Solution 5 - Java

Though it is marked as resolved here my KOTLIN RESOLUTION

fun log(properties: Map<String, Any>) {
    val propertyPairsList = properties.map { Pair(it.key, it.value) }
    val bundle = bundleOf(*propertyPairsList.toTypedArray())
}

bundleOf has vararg parameter

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMJ93View Question on Stackoverflow
Solution 1 - JavaaioobeView Answer on Stackoverflow
Solution 2 - JavajmhostaletView Answer on Stackoverflow
Solution 3 - JavatkruseView Answer on Stackoverflow
Solution 4 - JavaImarView Answer on Stackoverflow
Solution 5 - JavaNikhilesh PatveView Answer on Stackoverflow