Results 1 to 2 of 2
  1. #1
    Goldstein is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Exclamation 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 :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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 11:54 PM.

Similar Threads

  1. Character Occurence
    By DMKanz in forum New To Java
    Replies: 11
    Last Post: 10-21-2011, 03:44 AM
  2. Reverse words of string.
    By Vikash Kumar Singh in forum Advanced Java
    Replies: 6
    Last Post: 08-07-2011, 01:28 PM
  3. java -version pointing to older version
    By deepakts in forum New To Java
    Replies: 4
    Last Post: 05-06-2010, 10:59 AM
  4. Searching the first occurence
    By The Hawk in forum New To Java
    Replies: 7
    Last Post: 11-29-2009, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •