Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A unique collection

Posted on 2006-11-26
8
Medium Priority
?
716 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:twibblejaway
ID: 18014852
yeah, what is the syntax for extending something?  
0
 
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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 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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

971 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