• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How SortedMap works Internally

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frnds, I want to know how SortedMap works internally in comparing two key values while inserting elements in it. Because i want to override the compare method that it uses in comparing objects according, so can anyone explain it. Is it uses the compare method of Object class???

My problem is that i have some words with there freqency, now i want to sort the words according to their frequency, now the idea i have in my mind is that i just want insert the frequency of words as keys and words as values in SortedMap, but there are more than one 'same frequency words', so my key is not unique so it is not valid, so i make an object that have class count {String word, int times} i.e. which word appear how many times. Now i want to insert in sortedmap as sortedmap.put(key objectOfCount, value objectOfCount). Now i want to know that how sortedmap works internally while putting the keys, i.e. is it compares one key with another using the compare method of Object class, or something else, so that i override that method and get my result.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manu Batham wrote:Frnds, I want to know how SortedMap works internally in comparing two key values while inserting elements in it. Because i want to override the compare method that it uses in comparing objects according, so can anyone explain it. Is it uses the compare method of Object class???


SortedMap does not work at all - it's just an interface.
As for how TreeMap (the most often used implementation) works, read Campbell's first reply in this thread. Also, Object does not have a compare method. That's located either in java.lang.Comparable (compareTo), or in java.util.Comparator. And yes, TreeMap does use one of those; the latter if a Comparator was provided for the constructor, otherwise the former.
 
Manu Batham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prime for correcting me. It will help me to build up right concepts, just started learning java, it seems me very complex.
 
Marshal
Posts: 80982
530
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you are confusing three things to learn.
  • Java syntax. How do you call methods, run loops, select choices, etc?
  • Object-oriented programming. How many classes do you need? Which class has which fields or methods, etc?
  • The structure of a set, a map, a tree, etc.
  • The last is often taught as "algorithms and data structures," in a module separate from programming.

    All three are valuable bits of knowledge. But the idea of object-oriented programming is that you are presented with a class, maybe for a set, and told what it does, not how it does it. A bit like learning to drive. What happens when you push the right pedal? Does it differ whether you are using a diesel engine, a carburettor for petrol/gas, injection for petrol/gas, an LPG (methane) engine, or a Prius with an electric engine? Can you drive the car without knowing what sort of engine it uses?
     
    Manu Batham
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks dude, i know rightly about the other things, but the thing in which i confused every time is that

    Which class has which fields or methods, etc?



    And i think i can learn them only through practice, can you give me a list of good program or a list of assignment, so that i can work i a right direction to learn java, I also see other replies of you to different posts, i also extract the src folder and try to understand SortedMap interface and treeMap class, but only very little bit i gain by seeing them, so if you have some sort of list of programs (that should be really nice and time consuming for a noob), please send me there problem definition. Thanks a lot
     
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Which class has which fields or methods, etc?



    That's what the API docs are for.

    there are more than one 'same frequency words', so my key is not unique so it is not valid



    What happens when you use the word as the key and the frequency of it as the value?
     
    Manu Batham
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok Deepanjan, it is done. The problem was there was that i have to sort the words according to their frequency, and if the two words have the same frequency then i have to sort them according to alphabetical order, the problem before me was that i can't sort them according frequency because sortedmap doesn't allow duplicate elements, so i did the following --





    After that i was able to sort them, as i want, but i don't hav any file for testing, you guys please test it and send me reply, and correct it if have a new idea for more optimized code.
     
    Manu Batham
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Its running time on average 20 millisecs on my lappy.
     
    I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
    Clean our rivers and oceans from home
    https://www.kickstarter.com/projects/paulwheaton/willow-feeders
    reply
      Bookmark Topic Watch Topic
    • New Topic