Re: [List handling][Resolved]number of occurences
Daniel Moyne wrote:
I have this type of list :
private static ArrayList<String> firstJurisdictionInPlace=new
ArrayList<String>();
private static Collator myCollator;
firstJurisdictionInPlace.removeAll(firstJurisdictionInPlace);
if (_place.equals(my_PLAC)) {
if (!firstJurisdictionInPlace.contains(firstJurisdiction)) {
firstJurisdictionInPlace.add(firstJurisdiction);
firstJurisdictionCount+=1;
}
}
}
/* we sort alphabetically all lieux-dits */
Collections.sort(firstJurisdictionInPlace,myCollator);
So basically I update my list with a new "firstJurisdiction" each time
the "if condition" is true ; besides I count the number of entries in my
list ; it works fine.
When I meet a "firstJurisdiction" already in the list I do nothing but now
I want to count the number of occurences of each entry of my list by doing
something like this :
if (!firstJurisdictionInPlace.contains(firstJurisdiction)) {
firstJurisdictionInPlace.add(firstJurisdiction);
firstJurisdictionCount+=1;
}
}
else {
_count_for_this_jurisdictio+=1;
......
}
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.
Thanks.
One of the possible solution was to use no more lists but a hashMap instance
where I could store my string as a key and the asssociated number of
occurences as a Value ; here is what I did :
private static Map<String,Integer>my_firstJurisdiction= new HashMap<String,
Integer>();
............
/* we start with empty map */
my_firstJurisdiction.clear();
............
/* we feed the map structure */
/* we do not care for any order during this procedure */
if (!my_firstJurisdiction.containsKey(firstJurisdiction)) {
my_firstJurisdiction.put(firstJurisdiction,1);
}
else {
my_firstJurisdiction.put(firstJurisdiction,my_firstJurisdiction.get(firstJurisdiction)+1);
}
/* we sort the map with the alphabetical order of the key */
my_firstJurisdiction=new TreeMap<String, Integer>(my_firstJurisdiction);
/* we list result of search */
/* we cannot iterate directly a TreeMap :we go with a Set */
for (Iterator it=my_firstJurisdiction.entrySet().iterator(); it.hasNext(); )
{
Map.Entry entry = (Map.Entry)it.next();
println("S= "+entry.getKey().toString()+"
V= "+entry.getValue().toString()}));
}
This last section is a little strange as you can only extract Object from
the set such as :
Object key=entry.getKey();
Object value=entry.getValue();
So though initially key was typed as a String and value as an integer.
QUESTION : Should value had initially been a list for example in such case
how to get from the :
Object value=entry.getKey();
the elements of the List ?
Thanks for help.
Daniel