Announcement

Collapse
No announcement yet.

Arraylist.contains not working.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Arraylist.contains not working.

    I've been trying to use the contains function on an arrayList that contains objects. Will that not work with an object?

    Code:
       //Provider Object constructor
    function provider(npi,org,seg,lname,fname)
    {
    this.NPI = npi;
    this.ORG = org;
    this.SEG = seg
    this.LNAME = lname;
    this.FNAME = fname;
    }
    
    for(j = msg['OBR'][i]['OBR.16'].length()-1;j>=0;j--)
    {
    
    if(msg['OBR'][i]['OBR.16'][j]['OBR.16.1'].toString() != '')
    {
    id = msg['OBR'][i]['OBR.16'][j]['OBR.16.1'].toString();
    lname = msg['OBR'][i]['OBR.16'][j]['OBR.16.2'].toString();
    fname = msg['OBR'][i]['OBR.16'][j]['OBR.16.3'].toString();
    
    var objProv = getNPI(id,'OBR16',lname,fname);
    [B]if(!provList.contains(objProv))
         provList.add(objProv);[/B]
    }
    }
    I can see items in the provLIst but it still puts in duplicates. Guess I'm not clear what contains() actually checks. Each property of the objects?

  • #2
    Not sure the need for array list but that code didn't validate for me.

    By chance, if it's just a need to load an array w/objects then retrieve specific array element(s) by keying on object properties, a basic array and the use of array prototypes may be an option.
    Code:
    var objArr = [{NPI:123,ORG:'ORG1',SEG:'SEG1',lname:'JOHN',fname:'DOCTOR'},{NPI:321,ORG:'ORG2',SEG:'SEG2',lname:'NANCY',fname:'NURSE'}]
    $c('objNurse',objArr.find(obj => obj.NPI == 321))
    $c('objDoctor',objArr.find(obj => obj.fname == 'DOCTOR'))
    Hope this helps!
    Last edited by jkrebs; 07-26-2021, 04:35 AM.

    Comment


    • #3
      The javadoc explains how List.contains works. https://docs.oracle.com/javase/8/doc...a.lang.Object-

      boolean contains(Object o)

      Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
      When you compare two javascript object references to each other, they are never equal unless they point to the same object (i.e., changing a property of one object also affects the other.)

      I would follow jkrebs suggestion and use a javascript array instead of a java ArrayList. This will give you a lot more control.

      Comment

      Working...
      X