wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TreeMap help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeMap help" Watch "TreeMap help" New topic
Author

TreeMap help

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

I am not getting, why headMap() functionality is totally different than headSet().I thought, it should print all elements from k1 to k2 but it is printing only last element of k1 and k2 ? what is this ?


F:\OCJP\Chapter7\TreeMap>java NaviBack
a
f

Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:I am not getting, why headMap() functionality is totally different than headSet().I thought, it should print all elements from k1 to k2 but it is printing only last element of k1 and k2 ? what is this ?


F:\OCJP\Chapter7\TreeMap>java NaviBack
a
f


why you think it should print all elements of k1, k2. let me give you a hint. it is a treemap. which means keys should be unique, which means not all values corresponding to all k1's won't be added to the map. if you check the api for put method it says that put method will replace the previous mapping if it is already there. so the latest value will be added.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
saloni jhanwar wrote:I am not getting, why headMap() functionality is totally different than headSet().I thought, it should print all elements from k1 to k2 but it is printing only last element of k1 and k2 ? what is this ?


F:\OCJP\Chapter7\TreeMap>java NaviBack
a
f


why you think it should print all elements of k1, k2. let me give you a hint. it is a treemap. which means keys should be unique, which means not all values corresponding to all k1's won't be added to the map. if you check the api for put method it says that put method will replace the previous mapping if it is already there. so the latest value will be added.


I don't understand your replacement logic.If k1 is a bucket then i can put many different objects in it ,i m not putting same object so is there any meaning of replacement logic in this context , and it is also not necessary that key should be unique.
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

TreeMap is a Map implementation. A Map takes key values pairs where the value is associated to a key.When you put a key which is already present in the map, the value of it would be replaced.
This is as per the Java design.

Check this


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

saloni jhanwar wrote:why headMap() functionality is totally different than headSet().

because headMap is a Map and headSet is a Set. and both doest not support duplicates(in case of Map it is Key), hence it has recently inserted element.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Ok nice thanks sumit.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

the question is why map replaces existing value? because it is designed in that way
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

For more details check this and this
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Seetharaman Venkatasamy wrote:the question is why map replaces existing value? because it is designed in that way


Yes,i read in book something like this that a hashcode/key is like bucket which can have many objects associated with it.it is possible that two or many different objects have same hashcode so they both can't reside in ? becauseof bucket concept of book i got confused.we call a bucket because it can hold many things but if one huge bucket holds only one thing then there is any logic to use bucket word here huh..
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Saloni
That is a different concept:
1. Hashcode is an index of a table , which holds the elements in TreeMap.
2. A key of an element is a unique ID of the element.

I believe there is a hashcode algorithm that is used to generate the hashcode of the element with a specific key.
Just for example, if the key is "dog", the hashcode is generated as 1. The element with "dog" as its key, will be put into index 1 of a table in TreeMap.
If the key is "cat", the hashcode is generated as 2. The element will put put in index 2 of the table and etc.

So, when you search for "dog", the hashcode is calculated and the result is 1. So, your searching algorithm will go to index 1 to retrieve "dog"'s element.

You can design your own hashcode algorithm to generate the index for each key.
There are different kinds of hashcode algorithms to research on.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

saloni jhanwar wrote:it is possible that two or many different objects have same hashcode so they both can't reside in ?

if two different objects has same hash code, then yes both will sit in the specific index/bucket in a singly linked list fashion. this situation is known as collision. a good hashing algorithm should give different hash code for different objects. collision create a linked list, hence searching an element time increase to directly proportional to an elements are in the list.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: TreeMap help
 
Similar Threads
I don't know what's wrong with this chess game
Resort a treeMap
comparator
TreeMap
HashSet