Solved

Duplicates in a ArrayList

Posted on 2011-09-21
27
268 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:jkteater
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 10
27 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36575408

if(!ar.contains(element))ar.add(element);
0
 
LVL 47

Expert Comment

by:for_yan
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

by:for_yan
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 47

Expert Comment

by:for_yan
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

by:
for_yan earned 125 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>();
        all.add(new Pair(2,3));
        all.add(new Pair(5,6));
        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;

    }

}

Open in new window


Output:
contains

Open in new window

0
 

Author Comment

by:jkteater
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

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

Expert Comment

by:for_yan
ID: 36576032
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
 
0
 

Author Comment

by:jkteater
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

by:jkteater
ID: 36576074
OK I think I understand now
0
 
LVL 47

Expert Comment

by:for_yan
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

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

Author Comment

by:jkteater
ID: 36576150
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
0
 

Author Comment

by:jkteater
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

by:for_yan
ID: 36576177

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

Author Comment

by:jkteater
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()

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

0
 
LVL 47

Expert Comment

by:for_yan
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

by:jkteater
ID: 36576199
No I need to check both
0
 
LVL 47

Expert Comment

by:for_yan
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

by:for_yan
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

by:for_yan
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

by:for_yan
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

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

Does that look right to check them both?
0
 

Author Comment

by:jkteater
ID: 36576320
Got that method added to revDataset
0
 
LVL 47

Expert Comment

by:for_yan
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

by:jkteater
ID: 36576359
It Works!!!!!!!!!!!!!

thanks for much for your time
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36576365
You are welcome
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question