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()'

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Ella
  • 47
  • 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
  • 387,369
  • 54
  • 702
  • 768
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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140