Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves gh-25140 #25155

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -482,9 +482,8 @@ public MultiValueMapAdapter(Map<K, List<V>> map) {

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

@Override
Expand Down Expand Up @@ -521,7 +520,7 @@ public void setAll(Map<K, V> values) {
@Override
public Map<K, V> toSingleValueMap() {
LinkedHashMap<K, V> singleValueMap = new LinkedHashMap<>(this.map.size());
this.map.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
this.map.forEach((key, value) -> singleValueMap.put(key, firstElement(value)));
return singleValueMap;
}

Expand Down Expand Up @@ -603,5 +602,4 @@ public String toString() {
return this.map.toString();
}
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.Properties;
import java.util.Set;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -210,6 +211,20 @@ void hasUniqueObject() {
assertThat(CollectionUtils.hasUniqueObject(list)).isFalse();
}

@Test
void noArrayIndexOutOfBoundsException() {
HashMap<String, List<Object>> mostlyEmptyMap = new HashMap<>();
mostlyEmptyMap.put("test", new ArrayList<>());

MultiValueMap<String, Object> multiValueMap = CollectionUtils.toMultiValueMap(mostlyEmptyMap);
Assertions.assertAll(
() -> assertThat(multiValueMap.getFirst(null)).isNull(),
() -> assertThat(multiValueMap.getFirst("test")).isNull(),
() -> assertThat(multiValueMap.toSingleValueMap().get(null)).isNull(),
() -> assertThat(multiValueMap.toSingleValueMap().get("test")).isNull()
);
}


private static final class Instance {

Expand Down