Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeMap help

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok nice thanks sumit.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the question is why map replaces existing value? because it is designed in that way
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For more details check this and this
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic