1
Map<String, List<String>> parameters;

Map<String, String[]> collect = parameters.entrySet().stream()
                .collect(Collectors.toMap(entry-> entry.getKey(),entry -> entry.getValue().toArray()));

I'm getting compiler error cannot resolve method 'getKey()'

YCF_L
  • 49,027
  • 13
  • 75
  • 115
Ella
  • 67
  • 6

2 Answers2

3

You should create an array of the correct type (i.e. a String[] and not an Object[]):

Map<String, String[]> collect = 
    parameters.entrySet()
              .stream()
              .collect(Collectors.toMap(Map.Entry::getKey,
                                        entry -> entry.getValue().toArray(new String[0])));
Eran
  • 359,724
  • 45
  • 626
  • 694
3

You have to use :

.toArray(String[]::new)

Instead of just :

.toArray()

because this one return Object[] not a String[]

As discussed in the comments my solution can be valid from Java11

YCF_L
  • 49,027
  • 13
  • 75
  • 115