mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
CollUtil参照Map.putIfAbsent,新增集合的addIfAbsent方法
This commit is contained in:
@@ -7,6 +7,9 @@ import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -745,6 +748,24 @@ public class CollUtilTest {
|
||||
Assert.assertEquals("d", map.get("keyd"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addIfAbsentTest() {
|
||||
// 为false的情况
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(null, null));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(CollUtil.newArrayList(), null));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(null, "123"));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(CollUtil.newArrayList("123"), "123"));
|
||||
Assert.assertFalse(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)),
|
||||
new Animal("jack", 20)));
|
||||
|
||||
// 正常情况
|
||||
Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList("456"), "123"));
|
||||
Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)),
|
||||
new Dog("jack", 20)));
|
||||
Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)),
|
||||
new Animal("tom", 20)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapToMapTest(){
|
||||
final HashMap<String, String> oldMap = new HashMap<>();
|
||||
@@ -951,4 +972,22 @@ public class CollUtilTest {
|
||||
private String gender;
|
||||
private Integer id;
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
static class Animal {
|
||||
private String name;
|
||||
private Integer age;
|
||||
}
|
||||
|
||||
@ToString(callSuper = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
static class Dog extends Animal {
|
||||
|
||||
public Dog(String name, Integer age) {
|
||||
super(name, age);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user