This discussion is archived
5 Replies Latest reply: Jan 15, 2013 6:22 AM by 841361 RSS

How to check long value is list using contains?

shivasha Newbie
Currently Being Moderated
Hi,

List<PType> reqIdList =dao.getIdList();

for(PType pType: reqIdList ){
boolean grpId= reqIdList .contains(pType.getGrpId());
}

It is always return false, even it contains same id.
How to check the long value which is available in list or not?

Thanks.
  • 1. Re: How to check long value is list using contains?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    after 200+ posts you should have learned how to use
     tags...
    
    shivasha wrote:
    List<PType> reqIdList =dao.getIdList();
    for(PType pType: reqIdList ){
    boolean grpId= reqIdList .contains(pType.getGrpId());
    }
    It is always return false, even it contains same id.
    How to check the long value which is available in list or not?
    I the first place I'd do this check in the dao. Because the dao could convert this in a where clause so that only needed data gets transfered from the database... Did you check twice that the datatype is long (more precise <b>L</b>ong) in the List and returned by <tt>pType.getGrpId()</tt>? bye TT                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  • 2. Re: How to check long value is list using contains?
    doremifasollatido Journeyer
    Currently Being Moderated
    You are searching a List<PType> for a Group ID (and it sounds like your Group ID is a "Long"). A List<PType> will never contain a Long.
    Note that you are iterating over reqIdList, and you also call contains on that same reqIdList. I think you need to check which variable should be containing the Long represented by pType.getGrpId().
  • 3. Re: How to check long value is list using contains?
    841361 Newbie
    Currently Being Moderated
    You should check the implemention of equals method in Ptype.
    when you call list.contains ..it compares the contained object in the list by their equals method.
    For your purpose the equals method of Ptype should use GrpId to differentiate bw two Ptype objects.
  • 4. Re: How to check long value is list using contains?
    jtahlborn Expert
    Currently Being Moderated
    40044004 wrote:
    You should check the implemention of equals method in Ptype.
    when you call list.contains ..it compares the contained object in the list by their equals method.
    For your purpose the equals method of Ptype should use GrpId to differentiate bw two Ptype objects.
    while that may be a good implementation of equals in this scenario, it won't help the OP with this particular problem one bit.
  • 5. Re: How to check long value is list using contains?
    841361 Newbie
    Currently Being Moderated
    >
    while that may be a good implementation of equals in this scenario, it won't help the OP with this particular problem one bit.
    >


    Thanks for the comments but I was just pointing towards the working of *.contains()* method of list that OP is using.

    Thanks

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points