Solved

# Duplicates in a ArrayList

Posted on 2011-09-21
Medium Priority
282 Views
Right now my code is building a ArrayList.

this is how I am adding the elements

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.
0
Question by:jkteater
• 17
• 10

LVL 47

Expert Comment

ID: 36575408

0

LVL 47

Expert Comment

ID: 36575413

BUT!

You need to make sure that you defined eqiuals() method for your type element for this to work
0

LVL 47

Expert Comment

ID: 36575429
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

0

LVL 47

Expert Comment

ID: 36575455
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

0

LVL 47

Accepted Solution

for_yan earned 500 total points
ID: 36575553
This is a simple example of how it works with a simple class
called Pair which contains two integers,
but two instances of Pair are considsdrred
equal when the first integer is the same

So that;s why it prints "contains" wheh we cvchel
the existence of pair (5,7) after we inserted the pair (5,6)

It is important that method equals has strinctly thsi decaration:

public boolean equals(Object o)

``````public class CheckDuplicate{
public static void main(String[] args) {

ArrayList <Pair>all = new ArrayList<Pair>();
Pair pp = new Pair(5,7);
if(all.contains(pp))System.out.println("contains");
}
}

class Pair{
int x;

int y;
Pair(int x, int y){
this.x = x;
this.y = y;

}

int getX(){
return x;
}
int getY() {
return y;
}

public boolean equals(Object o){
Pair p = (Pair) o;
if(x == p.getX())return true;
else return false;

}

}
``````

Output:
``````contains
``````
0

Author Comment

ID: 36575972
Where would I write the Pair Class  - Would I just put in class file that is adding the elements?
0

LVL 47

Expert Comment

ID: 36576016
No, look, I showed you Pair class just as an example - it is analogy to your stuff
0

LVL 47

Expert Comment

ID: 36576032
You don't need andy Pair class

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

0

Author Comment

ID: 36576063
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?
0

Author Comment

ID: 36576074
OK I think I understand now
0

LVL 47

Expert Comment

ID: 36576075
No, No,, No - Pair class is a simple example how it would work - has nothing tom do with your code.
Sorry I posted  it.

0

LVL 47

Expert Comment

ID: 36576081
Post the code for your RevDataset , but mainly decide when two instances of Revdataset are consdiere
equal
0

Author Comment

ID: 36576150
OK I am lost again :)

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
0

Author Comment

ID: 36576164
I guess my other question is if there is a match how does the code skip adding that element to the ArryList
0

LVL 47

Expert Comment

ID: 36576177

No it should be:

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?
0

Author Comment

ID: 36576183
``````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()
``````

``````public class EdiSelection {

static ArrayList<RevDataset> rds;

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

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

//////////////////////////////////////////////////////////////////////////
//                                                                      //
//                        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
``````
0

LVL 47

Expert Comment

ID: 36576187

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
0

Author Comment

ID: 36576199
No I need to check both
0

LVL 47

Expert Comment

ID: 36576205

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

0

LVL 47

Expert Comment

ID: 36576219

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
0

LVL 47

Expert Comment

ID: 36576236
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 ==
0

LVL 47

Expert Comment

ID: 36576290
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;
}
0

Author Comment

ID: 36576301
if(tcRevision.equals(p.getRev())&&(selectedDataset.equals(p.getDataset())

Does that look right to check them both?
0

Author Comment

ID: 36576320
Got that method added to revDataset
0

LVL 47

Expert Comment

ID: 36576326
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
0

Author Comment

ID: 36576359
It Works!!!!!!!!!!!!!

thanks for much for your time
0

LVL 47

Expert Comment

ID: 36576365
You are welcome
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.