Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag for United States of America

asked on

Duplicates in a ArrayList

Right now my code is building a ArrayList.

this is how I am adding the elements
rds.add(new RevDataset(tcRevision,selectedDataset));

I need to somehow check and make sure that the element being placed in the ArrayList is not already there.  

I am not sure how to check for that.
Avatar of for_yan
for_yan
Flag of United States of America image


if(!ar.contains(element))ar.add(element);

BUT!

You need to make sure that you defined eqiuals() method for your type element for this to work
when writing equals() method you need to think when you consider your eleemnts to be equal and implement it
in the body of your
RevDataset
class
 
It could be something like that:

public class RevDataset{

TCComponent rev;
....


public boolean equals(Object o){
RevDataset rd = (RevDataset) o;
if(rd.getRevision().equals(rev)) return true;
else return false;

}



}

This would mean that out of two pieces of Revdataset when comparining
equality you care only about equality of revision compomnent

and of course I assume that they defined equals() method for TCComponent

Once you define this method then
method contains() will work for you

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkteater

ASKER

Where would I write the Pair Class  - Would I just put in class file that is adding the elements?
No, look, I showed you Pair class just as an example - it is analogy to your stuff
You don't need andy Pair class

You already have
RevDataset class
and your elments are instances of RevDataset

Now define equals(Object ) method in that RevDataset class
and theh for your ArrayList contains() method would work
 
I am still trying to figure out how to incorperate your suggestion in to my existing code.  Would the pair class replace my RevDataset class?
OK I think I understand now
No, No,, No - Pair class is a simple example how it would work - has nothing tom do with your code.
Sorry I posted  it.

Post the code for your RevDataset , but mainly decide when two instances of Revdataset are consdiere
equal
OK I am lost again :)

rds.add(new RevDataset(tcRevision,selectedDataset));
RevDatase pp = new  RevDataset(tcRevision,selectedDataset);
if(rds.contains(pp))System.out.println("contains");

Is this how the add method should be now?

public boolean equals(Object o){
     RevDatase p = (RevDatase) o;
       if(tcRevision == p.getRev())return true;
       else return false;

Does this look right and what is o
I guess my other question is if there is a match how does the code skip adding that element to the ArryList

No it should be:
if(!rds.contains(pp))rds.add(pp);

so it will check and if it does not contain this elment   rds.contains() will return false and ! rds.contains()  will be true, and
so it will add only if it does not contain this elment - thats what you wnated?
package com.lexmark.ediua.dialogs;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentDataset;
import com.teamcenter.rac.kernel.TCComponentItemRevision;

public class RevDataset {
	
	TCComponentItemRevision rev;
	TCComponentDataset componentdataset;
		
   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                              Constructor                             //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////
   public RevDataset(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
      rev = tcRevision;
      componentdataset = selectedDataset;
   }// end Constructor
	
   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                              getDataset()                            //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////
   public TCComponent getDataset() {
      return componentdataset;
   }// end getDataset()

   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                              getRev()                                //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////
   public TCComponent getRev() {
      return rev;
   }//end getRev()

Open in new window


 
public class EdiSelection {
	
	static ArrayList<RevDataset> rds;  
	
   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                              Constructor                             //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////
	public EdiSelection() {
	   rds = new ArrayList<RevDataset>();
	}// end Constructor

   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                            add()                                     //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////
   public static void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset){
      rds.add(new RevDataset(tcRevision,selectedDataset));
	  //System.out.println("Key :" + tcRevision.toString() + " Value : " + selectedDataset.toString() + " \n");
   }// end add

   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                        getList()                                     //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////	
   public static ArrayList<RevDataset> getList(){
      return rds;	
   }// end GetList
   
   //////////////////////////////////////////////////////////////////////////
   //                                                                      //
   //                        clearList()                                     //
   //                                                                      //
   //////////////////////////////////////////////////////////////////////////	
   public static ArrayList<RevDataset> clearList(){
      rds.clear();
	  return rds;	
   }// end GetList

} // end class EdiSelection

Open in new window



public boolean equals(Object o){
     RevDatase p = (RevDatase) o;
       if(tcRevision == p.getRev())return true;
       else return false;
}

Thisseesm correct - if you care abiout equality of only tcRevision and no matter what is the relation
of thes second piece of this instance
No I need to check both

tcRevision  is an object ? Not number or String?

so you cannot compare them with ==

you can only compare them as

   if(tcRevision.equals(p.getRev()))

but you need to check if you class TCComponent has edefined
equals() method


what is "o":

public boolean equals(Object o){

"o" is a parameter like any method has a paramtere

if you declare method you name your paraneters in the decalrtion togerther
with ther types

o is aparameter of type Object which takes this mthod equals
If you need to check both then cvhekc both and
put && betwenn them and
bothe of them should yse equals() not ==

== is only for primitive types

Struing of ocuse also requers equals() (made a wrong statement above)

only int,. double etc you may compare as ==
something like that:

public boolean equals(Object o){
     RevDataset p = (RevDataset) o;
       if(rev.equals(p.getRev()) && componentdataset.equals(p.getDataset()) return true;
       else return false;
}
if(tcRevision.equals(p.getRev())&&(selectedDataset.equals(p.getDataset())

Does that look right to check them both?
Got that method added to revDataset
yes, it is,  but the name of your instabce variable is not tcRevision, it is rev

and not electedDataset but componentdataset

TCComponentItemRevision rev;
      TCComponentDataset componentdataset;

so look at my code above
It Works!!!!!!!!!!!!!

thanks for much for your time
You are welcome