4

Needs:

  • Storing objects of a class which overrides equals and hash code
  • Will be looping and shoving objects into the datastructure
  • Need to be able to call contains to check whether a certain object is stored in the structure
  • If contains returns true then fetch that specific object from the structure and call a certain getter on that object

Options I've considered:

  • Map - this works for all the needs but I don't really have a map (key and a value). all I have is bunch of objects. Would it be a good practice to forcefully use a map by storing objects as key and integer or something in the value?

  • Set would work, however, it doesn't have a fetch method like get.

  • List would also work, but it doesn't have a method to fetch that is non index based. Meaning, once contains returns true I'll have to loop through the list to find the index of my particular object and then fetch it.

I'm open to using different libraries like apache commons or guava for example.

1
  • 1
    Funny, you don't want a map, but you need a fetch method :-) And what do you want to fetch? The objects I suppose. And based on what criteria? And no, it is not such a good idea to store objects as key (set?) and integer as a value. That's wasting memory and introducing another vector of debugging. Oct 12, 2012 at 11:12

3 Answers 3

2

List would also work, but it doesn't have a method to fetch that is non index based.

List has an indexOf(Object) method which will do exactly what you want.

9
  • He doesn't need the indexOf method, because his objects are not to be retrieved by an index, which is a specialization of a key. This is also mentioned somewhat by the last paragraph in OPs question. Oct 12, 2012 at 11:21
  • Did you read the last point of the OP? He says that a List is not appropriate, since he cannot get the index of the object he uses for contains(). This is where indexOf() helps.
    – Baz
    Oct 12, 2012 at 11:22
  • No, what he said was "List would also work, but it doesn't have a method to fetch that is non index based". He wants a method that is not index based. That's because he doesn't know indexes. What he will do is create an object, and since the class implements equals, he will simply call contains. Of course, it all depends on the equals implementation. Oct 12, 2012 at 11:24
  • 1
    @amn.. He didn't knew that he has a method called :- indexOf that can fetch him an index. He don't want to Loop, which he will have to do in case of a Set, which you have suggested.
    – Rohit Jain
    Oct 12, 2012 at 11:27
  • 1
    @amn. Common man. Can't you re-read the OP rather than arguing?? Why do you think he doesn't need to iterate?? How would you fetch data from a Set without looping??
    – Rohit Jain
    Oct 12, 2012 at 11:30
2

Although the best thing to use in this scenario would be a Map, because it offers fast retrieval based on Key-Value pair.

But List also allows to fetch data based on index.

So, you can use either a List or a Map. But to make your task easier, I would prefer a Map. Because i case of Map you won't have to search for an index of an Object, then get the Object at that index. Fetching is just a one-line operation.

// When using a List.
List<String> myList = new ArrayList<String>();
if (myList.contains("rohit")) {
    myList.get(myList.indexOf("rohit"));
}

// When using Map.
Map<String, String> myMap = new HashMap<String, String>();
// You can directly fetch your object, based on some Key if you have one..
myMap.get("key"); 
7
  • Why a downvote? It would be more productive to leave a comment after you downvote. So that the post can be improvised.
    – Rohit Jain
    Oct 12, 2012 at 11:18
  • Certainly @Rohit. Based on OPs criteria, your answer is simply not a good one. I am sorry to come across like this. Please check his explicit requirements - what he needs is a set. You simply emulate a set with your list, why? A set is made to provide a contains method, to make use of equals and hashCode, for "looping and shoving" objects into it, and if you want to check if it contains an object then you (again) call the contains(object). Oct 12, 2012 at 11:20
  • @amn.. And you must also notice that he don't want to iterate to fetch the data. Set does not have a method to fetch on index. So, even if he knows that Set contains the object, he would have to iterate.. Thats why I posted two options.
    – Rohit Jain
    Oct 12, 2012 at 11:22
  • Really? And how will he know what object to fetch from your list? Oct 12, 2012 at 11:23
  • 1
    @amn.. You didn't see my example?? using indexOf() method. I have posted the code
    – Rohit Jain
    Oct 12, 2012 at 11:24
-2

You need a set. You don't need a fetch method (you think you do), because like you said you only have a bunch of objects. And since these use equals and hashCode, a set is exactly what you need.

Of course a map could do as well, because its keys is a set as well, but in the end you need to better specify your requirements, as it appears you are a bit confused as to the purpose of your data structure. From what I understand, you do not need a map indeed.

A hash set implementation will do. Here is what you can do with it all:

class Foo
{
    final String name;

    Foo(String name)
    {
        this.name = name;
    }

    boolean equals(Object obj)
    {
        return (obj instanceof Foo) && ((Foo)obj).name.equals(name);
    }
}

Set<Foo> fooSet = new HashSet<Foo>();

fooSet.add(new Foo("someFoo"));

assert fooSet.contains(new Foo("someFoo"));
14
  • 1
    @amn.. Ok, now you know that your Set contains your object.. Now how would you fetch it? Using loop ofcourse. That is what OP don't want. Please re-read the OP
    – Rohit Jain
    Oct 12, 2012 at 11:28
  • No, you do not understand well what equals does - if objects are equal, you do not need the "original" object. The encapsulation of data within the object makes sure that you can use whatever new object you have created that equals the object in the set. Unless* of course, he **does need an index or a key, which makes my answer invalid. But that depends on the nature of his objects. I am merely replying to his requirements. Oct 12, 2012 at 11:31
  • @amn contains() will only tell him that it is contained, but there is no other way than looping to get it. A list also has contains() but in addition there is indexOf(). If you argue that he does not have to get the object back from the Collection then it does not make any difference if you use List or Set. There is no reason to downvote if I suggest a List.
    – Baz
    Oct 12, 2012 at 11:31
  • @amn.. Ok, here I'll try to clarify. Suppost OP wants to check whether the List contains an Object or not. Now, you're right that he can check it with contains method and use the object which he is comparing with. But what if he wants to modify the Object inside the Set?? how you approach this case? huh?
    – Rohit Jain
    Oct 12, 2012 at 11:33
  • @Baz, you do not need to get anything - the equals means objects are equal, and either one will do. Two string objects are considered equal if both have say "apple" as value, even if they are actually two distinct objects occupying different locations in memory. Your lists and/or maps retrieve the original object for him, which defeats the point of equals and hashCode. Oct 12, 2012 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.