Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Duplicates in a ArrayList

Posted on 2011-09-21
27
Medium Priority
?
272 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 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>();
        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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

721 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