Solved

A unique collection

Posted on 2006-11-26
8
709 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

24 Experts available now in Live!

Get 1:1 Help Now