feat: convert list to map

This commit is contained in:
easepan
2020-05-19 15:07:48 +08:00
parent 3c6b7fdc82
commit d0644331bc
2 changed files with 104 additions and 2 deletions

View File

@@ -3,8 +3,7 @@ package cn.hutool.core.collection;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Map;
import java.util.*;
/**
* {@link IterUtil} 单元测试
@@ -38,6 +37,31 @@ public class IterUtilTest {
Assert.assertEquals("\"1\":\"2\":\"3\":\"4\"", join2);
}
@Test
public void testToListMap() {
Map<String, List<String>> expectedMap = new HashMap<>();
expectedMap.put("a", Collections.singletonList("and"));
expectedMap.put("b", Arrays.asList("brave", "back"));
Map<String, List<String>> testMap = IterUtil.toListMap(Arrays.asList("and", "brave", "back"),
v -> v.substring(0, 1));
Assert.assertEquals(testMap, expectedMap);
}
@Test
public void testToMap() {
Map<String, Car> expectedMap = new HashMap<>();
Car bmw = new Car("123", "bmw");
expectedMap.put("123", bmw);
Car benz = new Car("456", "benz");
expectedMap.put("456", benz);
Map<String, Car> testMap = IterUtil.toMap(Arrays.asList(bmw, benz), Car::getCarNumber);
Assert.assertEquals(expectedMap, testMap);
}
public static class Car {
private String carNumber;
private String carName;