mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
add UniqueKeySet
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class UniqueKeySetTest {
|
||||
|
||||
@Test
|
||||
public void addTest(){
|
||||
Set<UniqueTestBean> set = new UniqueKeySet<>(UniqueTestBean::getId);
|
||||
set.add(new UniqueTestBean("id1", "张三", "地球"));
|
||||
set.add(new UniqueTestBean("id2", "李四", "火星"));
|
||||
// id重复,替换之前的元素
|
||||
set.add(new UniqueTestBean("id2", "王五", "木星"));
|
||||
|
||||
// 后两个ID重复
|
||||
Assert.assertEquals(2, set.size());
|
||||
|
||||
set.forEach(Console::log);
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
static class UniqueTestBean{
|
||||
private String id;
|
||||
private String name;
|
||||
private String address;
|
||||
}
|
||||
}
|
@@ -23,7 +23,7 @@ public class ReflectUtilTest {
|
||||
@Test
|
||||
public void getMethodsTest() {
|
||||
Method[] methods = ReflectUtil.getMethods(ExamInfoDict.class);
|
||||
Assert.assertEquals(22, methods.length);
|
||||
Assert.assertEquals(20, methods.length);
|
||||
|
||||
//过滤器测试
|
||||
methods = ReflectUtil.getMethods(ExamInfoDict.class, t -> Integer.class.equals(t.getReturnType()));
|
||||
@@ -35,7 +35,7 @@ public class ReflectUtilTest {
|
||||
//null过滤器测试
|
||||
methods = ReflectUtil.getMethods(ExamInfoDict.class, null);
|
||||
|
||||
Assert.assertEquals(22, methods.length);
|
||||
Assert.assertEquals(20, methods.length);
|
||||
final Method method2 = methods[0];
|
||||
Assert.assertNotNull(method2);
|
||||
}
|
||||
@@ -114,7 +114,7 @@ public class ReflectUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getMethodBenchTest(){
|
||||
public void getMethodBenchTest() {
|
||||
// 预热
|
||||
getMethodWithReturnTypeCheck(TestBenchClass.class, false, "getH");
|
||||
|
||||
@@ -171,14 +171,26 @@ public class ReflectUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMethodsFromClassExtends(){
|
||||
public void getMethodsFromClassExtends() {
|
||||
// 继承情况下,需解决方法去重问题
|
||||
final Method[] methods = ReflectUtil.getMethods(C2.class);
|
||||
Assert.assertEquals(2, methods.length);
|
||||
Method[] methods = ReflectUtil.getMethods(C2.class);
|
||||
Assert.assertEquals(15, methods.length);
|
||||
|
||||
// 排除Object中的方法
|
||||
// 3个方法包括类
|
||||
methods = ReflectUtil.getMethodsDirectly(C2.class, true, false);
|
||||
Assert.assertEquals(3, methods.length);
|
||||
|
||||
// getA属于本类
|
||||
Assert.assertEquals("public void cn.hutool.core.util.ReflectUtilTest$C2.getA()", methods[0].toString());
|
||||
// getB属于父类
|
||||
Assert.assertEquals("public void cn.hutool.core.util.ReflectUtilTest$C1.getB()", methods[1].toString());
|
||||
// getC属于接口中的默认方法
|
||||
Assert.assertEquals("public default void cn.hutool.core.util.ReflectUtilTest$TestInterface1.getC()", methods[2].toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMethodsFromInterfaceTest(){
|
||||
public void getMethodsFromInterfaceTest() {
|
||||
// 对于接口,直接调用Class.getMethods方法获取所有方法,因为接口都是public方法
|
||||
// 因此此处得到包括TestInterface1、TestInterface2、TestInterface3中一共4个方法
|
||||
final Method[] methods = ReflectUtil.getMethods(TestInterface3.class);
|
||||
@@ -189,25 +201,26 @@ public class ReflectUtilTest {
|
||||
Assert.assertArrayEquals(methods, publicMethods);
|
||||
}
|
||||
|
||||
interface TestInterface1{
|
||||
interface TestInterface1 {
|
||||
void getA();
|
||||
|
||||
void getB();
|
||||
|
||||
default void getC(){
|
||||
default void getC() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
interface TestInterface2 extends TestInterface1{
|
||||
interface TestInterface2 extends TestInterface1 {
|
||||
@Override
|
||||
void getB();
|
||||
}
|
||||
|
||||
interface TestInterface3 extends TestInterface2{
|
||||
interface TestInterface3 extends TestInterface2 {
|
||||
void get3();
|
||||
}
|
||||
|
||||
class C1 implements TestInterface2{
|
||||
class C1 implements TestInterface2 {
|
||||
|
||||
@Override
|
||||
public void getA() {
|
||||
@@ -220,7 +233,7 @@ public class ReflectUtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
class C2 extends C1{
|
||||
class C2 extends C1 {
|
||||
@Override
|
||||
public void getA() {
|
||||
|
||||
|
Reference in New Issue
Block a user