Results 1 to 2 of 2
- 02-11-2012, 05:54 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
Map of words and their occurence [ Reverse Version ]
Hey everyone,
as written in the title im having trouble creating the reverse version of this code :
for each word i associate its number of occurences.
public static Map<String,Integer> orc(ArrayList<String> l)
{
Map<String,Integer> m = new TreeMap<String,Integer>();
for (int j=0; j < l.size() ; j++)
{
int cpt=0;
for (int i=0; i < l.size() ; i++)
{
if ( l.get(i).equals(l.get(j)))
{cpt++;}
}
m.put(l.get(j), cpt);
}
return m;
}
Now i want to create a map<Integer,Set<String>> which contains each number of occurence and the list of words who exists the same number of times:
for example: 2 = { a,b,c} 1 = {d,e}
Thank you :)
- 02-11-2012, 10:51 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Map of words and their occurence [ Reverse Version ]
Go through your first map and add for each string/count pair add it to the new map. Adding to the new map will involve (1) creating a new entry for that count if one does not already exist and (2) adding the string to the count's associated set.
(I had a look at the extensive Guava library, but couldn't find a method that transforms maps like this. There is a MultiMap class, though, which allows duplicate values to be added.)
[Edit] I knew I'd seen it somewhere! Guava does have an inverse() method - but only for ImmutableMultiMap<K,V>.Last edited by pbrockway2; 02-11-2012 at 10:54 PM.
Similar Threads
-
Character Occurence
By DMKanz in forum New To JavaReplies: 11Last Post: 10-21-2011, 02:44 AM -
Reverse words of string.
By Vikash Kumar Singh in forum Advanced JavaReplies: 6Last Post: 08-07-2011, 12:28 PM -
java -version pointing to older version
By deepakts in forum New To JavaReplies: 4Last Post: 05-06-2010, 09:59 AM -
Searching the first occurence
By The Hawk in forum New To JavaReplies: 7Last Post: 11-29-2009, 12:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks