1

I was trying to get the response headers, which are in the form of MultivaluedMap<String, Object>.

The object is in the form of List<String>.

I want to store it in a Map<String, List<String>>.

Is there any direct way to do this?

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
Ashish
  • 21
  • 2

1 Answers1

0

To convert MultivaluedMap to Map<String,List<String>> you can do this :

First create a method that convert List<Object> to List<String> (MultivaluedMap::get return a List<Object>) and return a Map<String,List<String>> (to keep trace of key)

public static Map<String,List of String> convertListObjectToListofString(String k, List<Object> object) {
    
    List<String> list = object.stream()
            .flatMap(obj -> new ArrayList<String>((Collection<String>)obj).stream())
            .collect(Collectors.toList());
    
    Map<String, List<String>> map = new HashMap<String, List<String>>();

    map.put(k, list);
    
    return map;
}

After that you can stream you MultivaluedMap (.entrySet().stream()) and convert it to Map<String, List> using convertListObjectToListofString and re-stream the maps returned to collect all result in one map ( .flatMap(eS ->convertListObjectToListofString(eS.getKey(), eS.getValue()).entrySet().stream()) .collect(Collectors.toMap(eS->eS.getKey(), eS -> eS.getValue())); )

And here all the code :

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;

public class MapObjectToStringList {
    
    public static void main(String[] args) {
        
        List<String> list1 = new ArrayList<>(Arrays.asList("a","b","c"));
        List<String> list2 = new ArrayList<>(Arrays.asList("d","e","f"));
        List<String> list3 = new ArrayList<>(Arrays.asList("g","h","i"));
        List<String> list4 = new ArrayList<>(Arrays.asList("j","k","l"));
        
        MultivaluedMap<String, Object> map = new MultivaluedHashMap<String, Object>();
        
        map.add("aa", list1);
        map.add("bb", list2);
        map.add("cc", list3);
        map.add("dd", list4);
        
        Map<String, List<String>> collect = map.entrySet().stream()
                .flatMap(eS ->convertListObjectToListofString(eS.getKey(), eS.getValue()).entrySet().stream())
                .collect(Collectors.toMap(eS->eS.getKey(), eS -> eS.getValue()));
        
        System.out.println(collect);

    }
    
    public static Map<String,List<String>> convertListObjectToListofString(String k, List<Object> object) {
        
        List<String> list = object.stream()
                .flatMap(obj -> new ArrayList<String>((Collection<String>)obj).stream())
                .collect(Collectors.toList());;
        
        Map<String, List<String>> map = new HashMap<String, List<String>>();
    
        map.put(k, list);
        
        return map;
    }
}

PS:

This code run with java 8 and I add javax.ws.rs-api-2.0.jar to my project

JHDev
  • 974
  • 2
  • 26
  • 38