15

List包含对象类型,但我需要检查该对象是否属于类型AB

A a = new A();
B b = new B();

List<A> aL = new ArrayList<A>();
List<B> bL = new ArrayList<B>(); 

如何检查是否List包含A对象或B对象?

这是代码:

    SegmentDetailInfo segmentDetailInfo  = new SegmentDetailInfo();
    segmentDetailInfo.setSeg_Id("1");

    SegReqInfoBean segReqInfoBean  = new SegReqInfoBean();
    segReqInfoBean.setPageName("homepage");

    List<SegmentDetailInfo> rhsList1 = new ArrayList<SegmentDetailInfo>();
    rhsList1.add(segmentDetailInfo);

    List<SegReqInfoBean> rhsList2 = new ArrayList<SegReqInfoBean>();
    rhsList2.add(segReqInfoBean);

    doProspecListCompareCheck(rhsList1);
    doProspecListCompareCheck(rhsList2);

}

private static void doProspecListCompareCheck(Object rhsList) {
     if (rhsList instanceof List<SegmentDetailInfo>) //wrong Check
         //DoTHIS
     else if(rhsList instanceof List<SegReqInfoBean>)   //wrong Check       
         //Do THIS       

}

==================================================== ======

    SegmentDetailInfo segmentDetailInfo  = new SegmentDetailInfo();
    segmentDetailInfo.setSeg_Id("1");

    SegReqInfoBean segReqInfoBean1  = new SegReqInfoBean();
    segReqInfoBean1.setPageName("Home");

    List<SegmentDetailInfo> rhsList1 = new ArrayList<SegmentDetailInfo>();
    rhsList1.add(segmentDetailInfo);

    List<SegReqInfoBean> rhsList2 = new ArrayList<SegReqInfoBean>();      
    rhsList2.add(segReqInfoBean1);

    String Homepage="homepage";

    doProspecListCompareCheck(Homepage);
    doProspecListCompareCheck(rhsList2);
    doProspecListCompareCheck(rhsList2);


private static void doProspecListCompareCheck(Object rhsListObj) {
         List<String> rhsStrList = new ArrayList<String>();
         List<SegReqInfoBean> segReqInfoBeanList = new ArrayList<SegReqInfoBean>();
         List<SegmentDetailInfo> segmentDetailInfoList = new ArrayList<SegmentDetailInfo>();


     if (rhsListObj != null && rhsListObj instanceof List) {

         if (((List<SegmentDetailInfo>) rhsListObj).get(0) instanceof SegmentDetailInfo){

                System.out.println("SegmentDetailInfo loading");
                segmentDetailInfoList = (List<SegmentDetailInfo>) rhsListObj;     
        }
         else if(((List<SegReqInfoBean>) rhsListObj).get(0) instanceof SegReqInfoBean){

                System.out.println("SegReqInfoBean loading");   
                segReqInfoBeanList = (List<SegReqInfoBean>) rhsListObj;
         }          

     }else if ( rhsListObj != null && rhsListObj instanceof String) {

         rhsStrList.add(rhsListObj.toString());

     }

}
4

4 回答 4

25

您可以做到这一点的一种方法是先Object在以下内容中进行比较List

private static void doProspecListCompareCheck(List rhsList)
{
    if(rhsList != null && !rhsList.isEmpty())
    {
        if (rhsList.get(0) instanceof SegReqInfoBean)
        {

        }
        else if(rhsList.get(0) instanceof SegmentDetailInfo)    
        {

        }
    }
}
于 2013-05-25T00:52:56.103 回答
9

泛型仅提供编译时检查。在运行时,它们完全消失了。这称为类型擦除。所以在运行时,您的代码如下所示:

    List rhsList1 = new ArrayList();
    rhsList1.add(segmentDetailInfo);

    List rhsList2 = new ArrayList();
    rhsList2.add(segReqInfoBean);

    doProspecListCompareCheck(rhsList1);
    doProspecListCompareCheck(rhsList2);

}
private static void doProspecListCompareCheck(Object rhsList) {


     if (rhsList instanceof List) //wrong Check
         //DoTHIS
     else if(rhsList instanceof List)   //wrong Check       
         //Do THIS       

}

通过泛型参数区分两个泛型对象在 Java 中根本无法做到。

于 2013-05-25T00:50:06.390 回答
0

如果您知道列表非空,您可以执行 rhsList.get(0) instanceof SegReqInfobean

如果列表可能为空,您可以从插入正确类型的对象开始,然后记住索引 0 存储了一个虚拟对象,因此在处理之前将其删除(或者只是开始处理索引 0 处的列表)。泛型只是编译时的便利。正如您所发现的,您不能在运行时使用泛型类型。

于 2013-05-25T01:12:42.600 回答
0
for (Object aList : list) {
    Class cls = aList.getClass();
    System.out.println("The type of the object is: " + cls.getName());
}

这似乎对我有用。这不仅会检测任何索引是否属于 A 或 B 类型,还会检测每种类型。

于 2018-12-28T20:35:09.020 回答