[collections] reference counting

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[collections] reference counting

Torsten Curdt
In the back of my head I thought this would
be possible already ...but right now  I just
cannot find how :-/

I would like to have a Set that does
reference counting. Example:

 set.add("a") // 1x a
 set.add("a") // 2x a
 set.remove("a") // 1x a
 set.remove("a") // empty

A useful addition or just me not
seeing the obvious?

cheers
--
Torsten

signature.asc (193 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: [collections] reference counting

Mike Pettypiece

Are you thinking of a Bag?

http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/or
g/apache/commons/collections/Bag.html


-----Original Message-----
From: Torsten Curdt [mailto:[hidden email]]
Sent: July 7, 2005 9:34 AM
To: Jakarta Commons Developers List
Subject: [collections] reference counting

In the back of my head I thought this would
be possible already ...but right now  I just
cannot find how :-/

I would like to have a Set that does
reference counting. Example:

 set.add("a") // 1x a
 set.add("a") // 2x a
 set.remove("a") // 1x a
 set.remove("a") // empty

A useful addition or just me not
seeing the obvious?

cheers
--
Torsten

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [collections] reference counting

Torsten Curdt
> Are you thinking of a Bag?

Doh! That's it ...thanks :)

cheers
--
Torsten

signature.asc (193 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [collections] reference counting

Torsten Curdt
Torsten Curdt wrote:
>>Are you thinking of a Bag?
>
>
> Doh! That's it ...thanks :)

Hm...

    org.apache.commons.collections.bag.HashBag bag
     = new org.apache.commons.collections.bag.HashBag();
    bag.add("a");
    System.out.println(bag.getCount("a"));
    bag.add("a");
    System.out.println(bag.getCount("a"));
    bag.remove("a");
    System.out.println(bag.getCount("a"));
    bag.remove("a");
    System.out.println(bag.getCount("a"));

gives

1
2
0
0

but I'd expect

1
2
1
0

this is because the remove(obj) is not
delegating to a remove(obj,1) but is
a full remove. ...so I have to extend
that class.

IMO having

 remove(obj) -> remove(obj,1)
 removeAll(obj)
 removeAll()

would make more sense.

My 2 cents

cheers
--
Torsten

signature.asc (193 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: [collections] reference counting

James Carman
Nope.  The remove(Object o) method removes ALL occurrences of the object
from the Bag.  There is a remove(Object o, int nCopies) method available
that you might want to try (using a value of 1 of course).  Hope that helps.

-----Original Message-----
From: Torsten Curdt [mailto:[hidden email]]
Sent: Thursday, July 07, 2005 2:57 PM
To: Jakarta Commons Developers List
Subject: Re: [collections] reference counting

Torsten Curdt wrote:
>>Are you thinking of a Bag?
>
>
> Doh! That's it ...thanks :)

Hm...

    org.apache.commons.collections.bag.HashBag bag
     = new org.apache.commons.collections.bag.HashBag();
    bag.add("a");
    System.out.println(bag.getCount("a"));
    bag.add("a");
    System.out.println(bag.getCount("a"));
    bag.remove("a");
    System.out.println(bag.getCount("a"));
    bag.remove("a");
    System.out.println(bag.getCount("a"));

gives

1
2
0
0

but I'd expect

1
2
1
0

this is because the remove(obj) is not
delegating to a remove(obj,1) but is
a full remove. ...so I have to extend
that class.

IMO having

 remove(obj) -> remove(obj,1)
 removeAll(obj)
 removeAll()

would make more sense.

My 2 cents

cheers
--
Torsten


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [collections] reference counting

James Carman
Oops.  Sorry!  I didn't see the rest of your message.  Apparently, you
already found the aforementioned method.  My mistake!

-----Original Message-----
From: James Carman [mailto:[hidden email]]
Sent: Thursday, July 07, 2005 2:59 PM
To: 'Jakarta Commons Developers List'
Subject: RE: [collections] reference counting

Nope.  The remove(Object o) method removes ALL occurrences of the object
from the Bag.  There is a remove(Object o, int nCopies) method available
that you might want to try (using a value of 1 of course).  Hope that helps.

-----Original Message-----
From: Torsten Curdt [mailto:[hidden email]]
Sent: Thursday, July 07, 2005 2:57 PM
To: Jakarta Commons Developers List
Subject: Re: [collections] reference counting

Torsten Curdt wrote:
>>Are you thinking of a Bag?
>
>
> Doh! That's it ...thanks :)

Hm...

    org.apache.commons.collections.bag.HashBag bag
     = new org.apache.commons.collections.bag.HashBag();
    bag.add("a");
    System.out.println(bag.getCount("a"));
    bag.add("a");
    System.out.println(bag.getCount("a"));
    bag.remove("a");
    System.out.println(bag.getCount("a"));
    bag.remove("a");
    System.out.println(bag.getCount("a"));

gives

1
2
0
0

but I'd expect

1
2
1
0

this is because the remove(obj) is not
delegating to a remove(obj,1) but is
a full remove. ...so I have to extend
that class.

IMO having

 remove(obj) -> remove(obj,1)
 removeAll(obj)
 removeAll()

would make more sense.

My 2 cents

cheers
--
Torsten


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [collections] reference counting

Torsten Curdt
> Oops.  Sorry!  I didn't see the rest of your message.  Apparently, you
> already found the aforementioned method.  My mistake!

;) ...no worries

cheers
--
Torsten

signature.asc (193 bytes) Download Attachment