public static Map<String,List<String>> toMultimap(Headers headers,String valueForNullKey){
  Map<String,List<String>> result=new TreeMap<>(FIELD_NAME_COMPARATOR);
  for (int i=0, size=headers.size(); i < size; i++) {
    String fieldName=headers.name(i);
    String value=headers.value(i);
    List<String> allValues=new ArrayList<>();
    List<String> otherValues=result.get(fieldName);
    if (otherValues != null) {
      allValues.addAll(otherValues);
    }
    allValues.add(value);
    result.put(fieldName,Collections.unmodifiableList(allValues));
  }
  if (valueForNullKey != null) {
    result.put(null,Collections.unmodifiableList(Collections.singletonList(valueForNullKey)));
  }
  return Collections.unmodifiableMap(result);
}

public static Map < String , List < String > > toMultimap ( Headers headers , String valueForNullKey ) { Map < String , List < String > > result = new TreeMap < > ( FIELD_NAME_COMPARATOR ) ; for ( int i = 0 , size = headers . size ( ) ; i < size ; i ++ ) { String fieldName = headers . name ( i ) ; String value = headers . value ( i ) ; List < String > allValues = new ArrayList < > ( ) ; List < String > otherValues = result . get ( fieldName ) ; if ( otherValues != null ) { allValues . addAll ( otherValues ) ; } allValues . add ( value ) ; result . put ( fieldName , Collections . unmodifiableList ( allValues ) ) ; } if ( valueForNullKey != null ) { result . put ( null , Collections . unmodifiableList ( Collections . singletonList ( valueForNullKey ) ) ) ; } return Collections . unmodifiableMap ( result ) ; }
returns an immutable map containing each field to its list of values .
