1
  public Email myMethod(Function<MyObject, String>... functions) { ...  }

I created a list of functions and I want to passed to myMethod:

     List<Function<MyObject, String>>  functions= new ArrayList<>();
        if (condition1) {
            functions.add(myObject->myObject.getStringX());
        } else {
            functions.add(myObject->myObject.getStringY());
        }
        myMethod(functions);//Does not compile, I must find a solution here 
        to convert my list of functions to an array that can be accepted as 
        myMethod argument
Michael
  • 41,989
  • 11
  • 82
  • 128
Imar
  • 499
  • 6
  • 9

2 Answers2

1

It's an array of functions, so create an array of such functions and pass it.

Or you can call it with:

Function<MyObject, String>[] array = new Function[functions.size()];
functions.toArray(array);
myMethod(array);

Just notice that you can not create a generic array, but you can declare one as such.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • I tried this solution but I have a compilation error: T[ ] is not a functional interface. I tried also this code: `myMethod(new Function[functions.size()]); ` But still not work – Imar Sep 04 '18 at 12:51
  • @Imar no idea where you got that from. did you try this with `javac`? – Eugene Sep 04 '18 at 12:52
  • You need the stream: `...locations.stream().toArray(...` – MaanooAk Sep 04 '18 at 12:52
  • 1
    @MaanooAk oh shoot, I have java-11 as the compiler, and it resolves to `Collection::toArray` that was added in 11. My bad – Eugene Sep 04 '18 at 12:53
  • 1
    `functions.toArray(array);` return type not accounted? – Naman Sep 04 '18 at 12:57
  • @nullpointer *If the list fits in the specified array, it is returned therein* `List list = List.of(1, 2, 3, 4); Integer[] arr = new Integer[list.size()]; Integer[] newArr = list.toArray(arr); System.out.println(Arrays.toString(arr)); System.out.println(arr == newArr);` – Eugene Sep 04 '18 at 13:00
  • @nullpointer [see this also](https://stackoverflow.com/questions/51902362/collections-emptylist-singleton-singletonlist-list-set-toarray) – Eugene Sep 04 '18 at 13:45
  • @Eugene yup, I've gone through that thread and Stuart's explanation via another linked question I guess. Thanks for sharing though. – Naman Sep 04 '18 at 13:54
1

A cleaner code I believe could've been

Function [] functionsArray = new Function[functions.size()];
for (int i=0;i< functions.size();i++) {
    functionsArray[i] = functions.get(i);
}
myMethod(functionsArray); // unchecked assignment here ofcourse

which then my IDE suggests me to write as

myMethod(functions.toArray(new Function[0]));
Naman
  • 27,789
  • 26
  • 218
  • 353
  • “cleaner” in which regard? A clean solution would be to implement the target method to accept a `List>` as you can convert an array to a `List` easily and efficient via `Arrays.asList`, while the opposite direction incorporates copying overhead and unchecked operations… – Holger Sep 04 '18 at 14:50
  • @HolgerI meant simplicity of the code, but I do borrow the point that the argument type change in the target method contract is a better way to deal with. – Naman Sep 04 '18 at 15:04
  • Can be improved by using: `Arrays.setAll(functionsArray, functions::get);` – Lino Sep 04 '18 at 15:07