Re: [List handling]number of occurences



On Fri, 27 Jul 2007 14:09:39 +0200, Daniel Moyne <dmoyne@xxxxxxxxxx>
wrote, quoted or indirectly quoted someone who said :

How to manage this for me to be able to retrieve the number of occurences of
each item of my list ; I would like to avoid using typical array as here I
do not care about dimension of my list.

What do you want to pop out the end?
I would think it would be an ArrayList of Occurrence Objects where an
Occurrence has String name and an int count.

You use the usual prev technique to compare this with prev. If they
are the same you create an Occurrence Object with count 1 and tack it
on the name of the array list. However there is a matching Occurrence
object already there, you add 1 to the counter.

If you want to see some prev-dup detecting logic see
http://mindprod.com/products.html#SORTED

the alternative, conceptually simpler is to use a HashMap, and look up
by string. If you find it, add 1. If you don't create an Occurrence
and add one. You don't have to sort. This would likely be slower.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
.