Skip to content

MultiValueMapAdapter.getFirst fails with IndexOutOfBoundsException in case of empty List #25140

Closed
@vlavasa

Description

@vlavasa

If You create MultiValueMap via MultiValueMapAdapter, and value list is empty, call of method getFirst fail on IndexOutOfBounds exception:

private static class MultiValueMapAdapter<K, V> implements MultiValueMap<K, V>, Serializable {

		private final Map<K, List<V>> map;

		public MultiValueMapAdapter(Map<K, List<V>> map) {
			Assert.notNull(map, "'map' must not be null");
			this.map = map;
		}

		@Override
		@Nullable
		public V getFirst(K key) {
			List<V> values = this.map.get(key);
			return (values != null ? values.get(0) : null);
		}

Good solution is:

return (values != null && !values.isEmpty() ? values.get(0) : null);

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions