public class Headers extends Object implements Map<String,List<String>>
Map
<String
, List
<String
>>. The keys are case-insensitive Strings representing the header names and the value associated with each key is a List
<String
> with one element for each occurrence of the header name in the request or response. For example, if a response header instance contains one key "HeaderName" with two values "value1 and value2" then this object is output as two header lines:
All the normalHeaderName: value1 HeaderName: value2
Map
methods are provided, but the following additional convenience methods are most likely to be used: getFirst(String)
returns a single valued header or the first value of a multi-valued header. add(String,String)
adds the given header value to the list for the given key. set(String,String)
sets the given header field to the single value given overwriting any existing values in the value list. An instance of Headers
is either mutable or immutable. A mutable headers allows to add, remove, or modify header names and values, e.g. the instance returned by HttpExchange.getResponseHeaders()
. An immutable headers disallows any modification to header names or values, e.g. the instance returned by HttpExchange.getRequestHeaders()
. The mutator methods for an immutable headers instance unconditionally throw UnsupportedOperationException
.
All methods in this class reject null
values for keys and values. null
keys will never be present in HTTP request or response headers.
Constructor | Description |
---|---|
Headers() |
Creates an empty instance of Headers . |
Headers |
Creates a mutable Headers from the given headers with the same header names and values. |
Modifier and Type | Method | Description |
---|---|---|
void |
add |
Adds the given value to the list of headers for the given key . |
void |
clear() |
Removes all of the mappings from this map (optional operation). |
boolean |
containsKey |
Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue |
Returns true if this map maps one or more keys to the specified value. |
Set |
entrySet() |
Returns a Set view of the mappings contained in this map. |
boolean |
equals |
Indicates whether some other object is "equal to" this one. |
List |
get |
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
String |
getFirst |
Returns the first value from the List of String values for the given key , or null if no mapping for the key exists. |
int |
hashCode() |
Returns a hash code value for the object. |
boolean |
isEmpty() |
Returns true if this map contains no key-value mappings. |
Set |
keySet() |
Returns a Set view of the keys contained in this map. |
static Headers |
of |
Returns an immutable Headers with the given name value pairs as its set of headers. |
static Headers |
of |
Returns an immutable Headers from the given headers with the same header names and values. |
List |
put |
Associates the specified value with the specified key in this map (optional operation). |
void |
putAll |
Copies all of the mappings from the specified map to this map (optional operation). |
List |
remove |
Removes the mapping for a key from this map if it is present (optional operation). |
void |
set |
Sets the given value as the sole header value for the given key . |
int |
size() |
Returns the number of key-value mappings in this map. |
Collection |
values() |
Returns a Collection view of the values contained in this map. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public Headers()
Headers
.public Headers(Map<String,List<String>> headers)
Headers
from the given headers
with the same header names and values.headers
- a map of header names and valuesNullPointerException
- if headers
or any of its names or values are null, or if any value contains null.public int size()
Map
Integer.MAX_VALUE
elements, returns Integer.MAX_VALUE
.public boolean isEmpty()
Map
true
if this map contains no key-value mappings.public boolean containsKey(Object key)
Map
true
if this map contains a mapping for the specified key. More formally, returns true
if and only if this map contains a mapping for a key k
such that Objects.equals(key, k)
. (There can be at most one such mapping.)containsKey
in interface Map<String,List<String>>
key
- key whose presence in this map is to be testedtrue
if this map contains a mapping for the specified keypublic boolean containsValue(Object value)
Map
true
if this map maps one or more keys to the specified value. More formally, returns true
if and only if this map contains at least one mapping to a value v
such that Objects.equals(value, v)
. This operation will probably require time linear in the map size for most implementations of the Map
interface.containsValue
in interface Map<String,List<String>>
value
- value whose presence in this map is to be testedtrue
if this map maps one or more keys to the specified valuepublic List<String> get(Object key)
Map
null
if this map contains no mapping for the key. More formally, if this map contains a mapping from a key k
to a value v
such that Objects.equals(key, k)
, then this method returns v
; otherwise it returns null
. (There can be at most one such mapping.)
If this map permits null values, then a return value of null
does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null
. The containsKey
operation may be used to distinguish these two cases.
public String getFirst(String key)
List
of String
values for the given key
, or null
if no mapping for the key
exists.key
- the key to search forString
value associated with the key, or null
if no mapping for the key existspublic List<String> put(String key, List<String> value)
Map
m
is said to contain a mapping for a key k
if and only if m.containsKey(k)
would return true
.)put
in interface Map<String,List<String>>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keykey
, or null
if there was no mapping for key
. (A null
return can also indicate that the map previously associated null
with key
, if the implementation supports null
values.)public void add(String key, String value)
value
to the list of headers for the given key
. If the mapping does not already exist, then it is created.key
- the header namevalue
- the value to add to the headerpublic void set(String key, String value)
value
as the sole header value for the given key
. If the mapping does not already exist, then it is created.key
- the header namevalue
- the header value to setpublic List<String> remove(Object key)
Map
k
to value v
such that Objects.equals(key, k)
, that mapping is removed. (The map can contain at most one such mapping.) Returns the value to which this map previously associated the key, or null
if the map contained no mapping for the key.
If this map permits null values, then a return value of null
does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null
.
The map will not contain a mapping for the specified key once the call returns.
public void putAll(Map<? extends String,? extends List<String>> t)
Map
put(k, v)
on this map once for each mapping from key k
to value v
in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress. If the specified map has a defined encounter order, processing of its mappings generally occurs in that order.public void clear()
Map
public Set<String> keySet()
Map
Set
view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove
operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove
, Set.remove
, removeAll
, retainAll
, and clear
operations. It does not support the add
or addAll
operations.public Collection<List<String>> values()
Map
Collection
view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove
operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove
, Collection.remove
, removeAll
, retainAll
and clear
operations. It does not support the add
or addAll
operations.public Set<Map.Entry<String,List<String>>> entrySet()
Map
Set
view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove
operation, or through the setValue
operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove
, Set.remove
, removeAll
, retainAll
and clear
operations. It does not support the add
or addAll
operations.public boolean equals(Object o)
Object
The equals
method implements an equivalence relation on non-null object references:
x
, x.equals(x)
should return true
. x
and y
, x.equals(y)
should return true
if and only if y.equals(x)
returns true
. x
, y
, and z
, if x.equals(y)
returns true
and y.equals(z)
returns true
, then x.equals(z)
should return true
. x
and y
, multiple invocations of x.equals(y)
consistently return true
or consistently return false
, provided no information used in equals
comparisons on the objects is modified. x
, x.equals(null)
should return false
. An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
public int hashCode()
Object
HashMap
. The general contract of hashCode
is:
hashCode
method must consistently return the same integer, provided no information used in equals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. equals
method, then calling the
hashCode
method on each of the two objects must produce the same integer result. equals
method, then calling the hashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. public static Headers of(String... headers)
Headers
with the given name value pairs as its set of headers. The supplied String
instances must alternate as header names and header values. To add several values to the same name, the same name must be supplied with each new value. If the supplied headers
is empty, then an empty Headers
is returned.
headers
- the list of name value pairsNullPointerException
- if headers
or any of its elements are null.IllegalArgumentException
- if the number of supplied strings is odd.public static Headers of(Map<String,List<String>> headers)
Headers
from the given headers
with the same header names and values.headers
- a map of header names and valuesNullPointerException
- if headers
or any of its names or values are null, or if any value contains null.
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/jdk.httpserver/com/sun/net/httpserver/Headers.html