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

x
?
Solved

A unique collection

Posted on 2006-11-26
8
Medium Priority
?
715 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Technology Partners: 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!

Question has a verified solution.

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

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…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
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