Skip to content

Commit 00c9875

Browse files
committed
Consistent MultiValueMap behavior for empty list values
Closes gh-25227
1 parent 2010956 commit 00c9875

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

spring-core/src/main/java/org/springframework/util/CollectionUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -418,7 +418,7 @@ public void add(K key, V value) {
418418
@Override
419419
public V getFirst(K key) {
420420
List<V> values = this.map.get(key);
421-
return (values != null ? values.get(0) : null);
421+
return (values != null && !values.isEmpty() ? values.get(0) : null);
422422
}
423423

424424
@Override
@@ -439,7 +439,10 @@ public void setAll(Map<K, V> values) {
439439
public Map<K, V> toSingleValueMap() {
440440
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<K,V>(this.map.size());
441441
for (Entry<K, List<V>> entry : map.entrySet()) {
442-
singleValueMap.put(entry.getKey(), entry.getValue().get(0));
442+
List<V> values = entry.getValue();
443+
if (values != null && !values.isEmpty()) {
444+
singleValueMap.put(entry.getKey(), values.get(0));
445+
}
443446
}
444447
return singleValueMap;
445448
}
@@ -506,10 +509,7 @@ public Set<Entry<K, List<V>>> entrySet() {
506509

507510
@Override
508511
public boolean equals(Object other) {
509-
if (this == other) {
510-
return true;
511-
}
512-
return this.map.equals(other);
512+
return (this == other || this.map.equals(other));
513513
}
514514

515515
@Override

spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -216,8 +216,8 @@ public LinkedCaseInsensitiveMap<V> clone() {
216216
}
217217

218218
@Override
219-
public boolean equals(Object obj) {
220-
return this.targetMap.equals(obj);
219+
public boolean equals(Object other) {
220+
return (this == other || this.targetMap.equals(other));
221221
}
222222

223223
@Override

spring-core/src/main/java/org/springframework/util/LinkedMultiValueMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -208,8 +208,8 @@ public LinkedMultiValueMap<K, V> clone() {
208208
}
209209

210210
@Override
211-
public boolean equals(Object obj) {
212-
return this.targetMap.equals(obj);
211+
public boolean equals(Object other) {
212+
return (this == other || this.targetMap.equals(other));
213213
}
214214

215215
@Override

0 commit comments

Comments
 (0)