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
  • 51,266
  • 13
  • 85
  • 129
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
  • 374,785
  • 51
  • 663
  • 734
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
  • 51,266
  • 13
  • 85
  • 129