Solved

A unique collection

Posted on 2006-11-26
8
710 Views
Last Modified: 2010-07-27
Hello,

I want to have a collection of objects(kind of).  The problem I have is that things like the java Collection and Vector class allow me check to see if an object already exists in a given collection.  What I actually want to check is to see if there is an object which has been initialized to the same values already in teh collection.

What would be the easiest way of doing this?

Thanks,
0
Comment
Question by:twibblejaway
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18014762
Use java.util.Set/java.util.HashSet
0
 

Author Comment

by:twibblejaway
ID: 18014810
That has the same problem as the other 2 methods.  It tests to see if the objects are the same, not if what they are initialized is equal.  I think I need to write my own Contains function for one of these classes.  I'm just  not sure on the exact syntax of how to do that in java?

Can somebody show me an example.

Thanks,
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18014849
when collection framework checks if object already exists, it calls equals() method to figure out that.
You can extend equals() method of the java.lang.Object class to do that.
0
 

Author Comment

by:twibblejaway
ID: 18014852
yeah, what is the syntax for extending something?  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:hoomanv
ID: 18014853
Objects that are being added to a Set must implement hashCode() and equals() methods as well
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22032063.html
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18014878
public class MyClass {
    public boolean equals(Object o) {
        // Write your logic here.
    }
}
0
 
LVL 12

Accepted Solution

by:
keyurkarnik earned 500 total points
ID: 18083693
Make sure you do the same for hashcode too.
Also, do NOT extend the equals() method, override it!!
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 18156897
twibblejaway
In case of duplicate or similar comments, you should always select the first comment posted
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now