aspose file tools*
The moose likes Java in General and the fly likes How SortedMap works Internally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How SortedMap works Internally" Watch "How SortedMap works Internally" New topic
Author

How SortedMap works Internally

Manu Batham
Greenhorn

Joined: Jul 08, 2009
Posts: 14
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Manu Batham
Greenhorn

Joined: Jul 08, 2009
Posts: 14
Thanks Prime for correcting me. It will help me to build up right concepts, just started learning java, it seems me very complex.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

    Joined: Jul 08, 2009
    Posts: 14
    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
    Dipanjan Kailthya
    Greenhorn

    Joined: Jul 04, 2009
    Posts: 20
    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

    Joined: Jul 08, 2009
    Posts: 14
    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

    Joined: Jul 08, 2009
    Posts: 14
    Its running time on average 20 millisecs on my lappy.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How SortedMap works Internally