TreeSet and HashSet

Hello

There is a very useful functionality, that I think should be implemented in

TreeSet nad HashSet

that is the method: Object get(Object o).

The method should return the same object from colletion as the parameter object.

In TreeSet complexity would be log(n), in HashSet would be constant.

With lack of this functionality one must implement collections on maps, so the unnecessary and more complex type will be used.

What do you think about this?

Regards

Marcin

[526 byte] By [MartinAa] at [2007-11-26 17:08:51]
# 1

> that is the method: Object get(Object o).

> The method should return the same object from

> colletion as the parameter object.

Maybe I don't understand what you are saying.

Wouldn't the method look like:

public Object get(Object o){

return o;

}

If you have the object you don't need to get it from the set.

zadoka at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 2

Not exactly.

In the collection we have objects of class1 that have many fields.

for example field1, field2, field3 etc.

The method equals for these objects looks like following:

public boolean equals(Object o)

{

if (field1.equals((class1)o.field1))

{

return true

}else

{

return false

}

}

And we have an object o1 of class1 that is not in the collection and it has only one field set (field1), and we want to find the same object in the collection (equals), in order to check the other fields (field2, etc).

Marcin

MartinAa at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 3

This is a strange question :)

I assume, the OP does not want the object to be returned, but the one from the Set, which is equals to the given object, or null. This might be useful, if operating in a template scenario like JINI, where the given object defines a template (or pattern) that suffices the equals method but does not have further data contained by the object in the Set (which actually should be solved by using a Map). Or if you want to match and return the equal object to have the exact instance. E.g. for a simple Set:public Object get(Object o) {

for (Object object : objects) {

if (object.equals(o)) {

return object;

}

}

}

which might be optimized for HashSet (quicker due to buckets) and TreeSet (quicker due to tree structure).

€dit: Ok, my first scenario was the one. I'd suggest using a Map instead, which is much cleaner wrt. equals/hashcode implementation and contract.

Message was edited by: stefan.schulz

stefan.schulza at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 4

When you have large sets, and you want to use maps instead, you have to create many key objects, so the memory usage will be much higher. When the keys will be space consuming, it really matters.

The simplicity of the code is better when you use Sets in my opinion, because sets concept is simpler than mapping concept. The code when using sets looks simpler. I think also it is not a good idea to create another class only for having key objects.

Marcin

MartinAa at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 5
Make up your mind. If you want to be able to lookup a Set via an object that isn't in the Set but has the same lookup properties, it must be a key object!
ejpa at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 6

Not only ejp is right, but also the argument of "consuming more space" is rubbish. Either the keys or the mapped objects have to contain the lookup properties. The overhead for storing two objects splitting both information or one object containing all the information is barely an issue.

Further, HashSet as well as TreeSet are internally backed by a map (mostly HashMap and TreeMap), hence, using a Set instead a Map does not buy anything but cause bad design.

stefan.schulza at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 7
Yes about more space is my mistake. For futher discussion please look at: http://groups.google.pl/group/comp.lang.java.programmer/browse_thread/thread/bf4b66581335218f/4b57ad430482c34b?hl=pl#4b57ad430482c34bMarcin
MartinAa at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 8

Suppose i have a Map :-

HashMap InfoMap = new HashMap();

infoMap.put("name","nitin");

infoMap.put("city","delhi");

infoMap.put("country","India");

then I can get the value of the name by infoMap.get("name");

Is this same thing can be possible with Set or any other 'Collection' ?

If I use Set collection then I will be having this information as

"nitin","delhi","India", How do i know that at the third place i have kept country ?

So how can we talk about replacing Map with Set or any other collection ?

Kindly correct me if I am wrong.

nitin.softplusa at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...
# 9
> So how can we talk about replacing Map with Set or> any other collection ?We can't. Did anybody do that?
ejpa at 2007-7-8 23:36:43 > top of Java-index,Core,Core APIs...