You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Mike Zatko <mz...@boscovs.com> on 2005/03/14 21:04:34 UTC

[Collections] What collection fits my problem?

I need a sorted collection that also allows deep copied duplicate 
objects (exact same object, different memory addresses). Is there a 
collection that fits that requirement? I've been having difficulty 
finding one myself. Thanks for any help.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Collections] What collection fits my problem?

Posted by Mike Zatko <mz...@boscovs.com>.
Using your idea, I added an == comparison inside the compareTo() method. 
(I didn't have to change the equals() method)

if(this==otherObject)
    return 0;
else
    return -1;

I beleive this does break the compareTo contract to some extent, but it 
does work.



Stephen Colebourne wrote:

> You describe the correct behaviour of SortedBag, and collections in 
> general. Most collections use .equals() comparisons.
>
> You have two choices:
> a) Write a sorted collection that uses == for comparison instead of 
> .equals
>
> b) Use a TreeSet, but wrap each of your objects in an inner class 
> style wrapper, eg:
> public class Wrapper {
>  public Object realData;
> }
>
> Stephen
>
> ----- Original Message ----- From: "Mike Zatko" <mz...@boscovs.com>
>
>> FYI, I did try a sorted bag which appeared to be what I was looking 
>> for. But, for some reason if you add 2 identical objects (deep 
>> copied) and add them to the bag, it will merely overwrites the first 
>> objects reference with the second objects reference. To me, this 
>> seems like it would be a bug, but I could be wrong. Any thoughts?
>>
>>
>> Mike Zatko wrote:
>>
>>> I need a sorted collection that also allows deep copied duplicate 
>>> objects (exact same object, different memory addresses). Is there a 
>>> collection that fits that requirement? I've been having difficulty 
>>> finding one myself. Thanks for any help.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Collections] What collection fits my problem?

Posted by Simon Kitching <sk...@apache.org>.
Hmm..my understanding of "Bag" would have matched Mike Zatko's, ie it is
a collection where multiple instances of identical objects can be added,
and those *instances* are added to the collection.

I have therefore attached a patch that updates the javadoc for class
o.a.c.c.Bag to make this clear. I hope these changes are acceptable.

Note that the javadoc changes I have made indicate that when duplicate
objects are added, a reference to the *first* added object is retained.
If this is not the case, then this would need to be updated.

Thanks,

Simon

On Tue, 2005-03-15 at 22:51 +0000, Stephen Colebourne wrote:
> You describe the correct behaviour of SortedBag, and collections in general. 
> Most collections use .equals() comparisons.
> 
> You have two choices:
> a) Write a sorted collection that uses == for comparison instead of .equals
> 
> b) Use a TreeSet, but wrap each of your objects in an inner class style 
> wrapper, eg:
> public class Wrapper {
>   public Object realData;
> }
> 
> Stephen
> 
> ----- Original Message ----- 
> From: "Mike Zatko" <mz...@boscovs.com>
> > FYI, I did try a sorted bag which appeared to be what I was looking for. 
> > But, for some reason if you add 2 identical objects (deep copied) and add 
> > them to the bag, it will merely overwrites the first objects reference 
> > with the second objects reference. To me, this seems like it would be a 
> > bug, but I could be wrong. Any thoughts?
> >
> >
> > Mike Zatko wrote:
> >
> >> I need a sorted collection that also allows deep copied duplicate objects 
> >> (exact same object, different memory addresses). Is there a collection 
> >> that fits that requirement? I've been having difficulty finding one 
> >> myself. Thanks for any help.
> >>

Re: [Collections] What collection fits my problem?

Posted by Stephen Colebourne <sc...@btopenworld.com>.
You describe the correct behaviour of SortedBag, and collections in general. 
Most collections use .equals() comparisons.

You have two choices:
a) Write a sorted collection that uses == for comparison instead of .equals

b) Use a TreeSet, but wrap each of your objects in an inner class style 
wrapper, eg:
public class Wrapper {
  public Object realData;
}

Stephen

----- Original Message ----- 
From: "Mike Zatko" <mz...@boscovs.com>
> FYI, I did try a sorted bag which appeared to be what I was looking for. 
> But, for some reason if you add 2 identical objects (deep copied) and add 
> them to the bag, it will merely overwrites the first objects reference 
> with the second objects reference. To me, this seems like it would be a 
> bug, but I could be wrong. Any thoughts?
>
>
> Mike Zatko wrote:
>
>> I need a sorted collection that also allows deep copied duplicate objects 
>> (exact same object, different memory addresses). Is there a collection 
>> that fits that requirement? I've been having difficulty finding one 
>> myself. Thanks for any help.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Collections] What collection fits my problem?

Posted by Mike Zatko <mz...@boscovs.com>.
FYI, I did try a sorted bag which appeared to be what I was looking for. 
But, for some reason if you add 2 identical objects (deep copied) and 
add them to the bag, it will merely overwrites the first objects 
reference with the second objects reference. To me, this seems like it 
would be a bug, but I could be wrong. Any thoughts?


Mike Zatko wrote:

> I need a sorted collection that also allows deep copied duplicate 
> objects (exact same object, different memory addresses). Is there a 
> collection that fits that requirement? I've been having difficulty 
> finding one myself. Thanks for any help.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org