Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check which type of object List<?> contains

Tags:

java

generics

List contains the object type, but I need to check if that object is of type A or B:

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

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

How can I check whether List contains A objects or B objects?

Here is the code:

    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());

     }

}
like image 606
Shivababa Avatar asked May 25 '13 00:05

Shivababa


People also ask

How can you tell what type of object is in ArrayList?

To check if ArrayList contains a specific object or element, use ArrayList. contains() method. You can call contains() method on the ArrayList, with the element passed as argument to the method. contains() method returns true if the object is present in the list, else the method returns false.

How do you check what type an object is?

Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.

How do you check if an object contains something?

The first way is to invoke object. hasOwnProperty(propName) . The method returns true if the propName exists inside object , and false otherwise. hasOwnProperty() searches only within the own properties of the object.


2 Answers

One way you can do it is by comparing first Object inside the 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)    
        {

        }
    }
}
like image 143
Alexandre Lavoie Avatar answered Oct 13 '22 09:10

Alexandre Lavoie


Generics only provide compile-time checks. At runtime, they are completely gone. This is known as type erasure. So at runtime, your code looks like this:

    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       

}

Distinguishing two generic objects by their generic parameter is simply not something you can do in Java.

like image 39
Jan Dörrenhaus Avatar answered Oct 13 '22 07:10

Jan Dörrenhaus