Sorrting Elements In Map Collection Based On Value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KWSW
    New Member
    • May 2007
    • 72

    #1

    Sorrting Elements In Map Collection Based On Value

    I need to sort out based on frequency certain events that happen.

    I use a map with the event name as the key(String) and the frequency(Integ er) as the value and every time an event happens.

    I have no problems with the part of adding and updating but am abit stuck at the sorting part as I need to display the events sorted from higest frequency to lowest.

    At first I was thinking of putting them into another hashmap with the frequency as the key as it auto sorts based on key but if I have 2 or more events with the same frequency, I might overwrite them.

    TIA :)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by KWSW
    I need to sort out based on frequency certain events that happen.

    I use a map with the event name as the key(String) and the frequency(Integ er) as the value and every time an event happens.

    I have no problems with the part of adding and updating but am abit stuck at the sorting part as I need to display the events sorted from higest frequency to lowest.

    At first I was thinking of putting them into another hashmap with the frequency as the key as it auto sorts based on key but if I have 2 or more events with the same frequency, I might overwrite them.

    TIA :)
    So now you have a Map<String, Integer> where the keys are Strings and the
    frequencies are Integers.

    Build another map like this: Map<Integer, List<String>> and add your value, key
    pairs (frequencies, keys) like this:

    [code=java]

    // the 'inverted' map
    private Map<Integer>, List<String>>= new TreeMap<Integer , List<String>>() ;
    ...
    public void add(Integer freq, String key) {
    List<String> list= map.get(freq);
    if (list == null) map.put(freq, list= new ArrayList<Strin g>());
    list.add(key);
    }
    [/code]

    That's all there is to it ;-)

    kind regards,

    Jos

    Comment

    • KWSW
      New Member
      • May 2007
      • 72

      #3
      Originally posted by JosAH
      So now you have a Map<String, Integer> where the keys are Strings and the
      frequencies are Integers.

      Build another map like this: Map<Integer, List<String>> and add your value, key
      pairs (frequencies, keys) like this:

      [code=java]

      // the 'inverted' map
      private Map<Integer>, List<String>>= new TreeMap<Integer , List<String>>() ;
      ...
      public void add(Integer freq, String key) {
      List<String> list= map.get(freq);
      if (list == null) map.put(freq, list= new ArrayList<Strin g>());
      list.add(key);
      }
      [/code]

      That's all there is to it ;-)

      kind regards,

      Jos
      ok will try it out...

      btw it will be from the lowest to highest right? Is there anyway to get from the back or sort it the other way around since I need the highest frequency?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by KWSW
        ok will try it out...

        btw it will be from the lowest to highest right? Is there anyway to get from the back or sort it the other way around since I need the highest frequency?
        Use -frequency for the key values? This is just a quick hack.

        kind regards,

        Jos

        Comment

        • KWSW
          New Member
          • May 2007
          • 72

          #5
          Originally posted by JosAH
          Use -frequency for the key values? This is just a quick hack.

          kind regards,

          Jos

          thanks again... it works... one last qn(hopefully).. .

          I know i can use

          for(Int i : map.keyset())

          to go through the whole map, but if I want to just take out the first ten, is there another type of for loop to do it?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by KWSW
            thanks again... it works... one last qn(hopefully).. .

            I know i can use

            for(Int i : map.keyset())

            to go through the whole map, but if I want to just take out the first ten, is there another type of for loop to do it?
            Sure, use a counter and when it reaches the value 10 break out of the loop
            (there are more ways to skin this cat though).

            kind regards,

            Jos

            Comment

            Working...