Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Splitting a Map

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you tell me how to split a TreeMap into smaller ones. Say if the tree map is of size 10 and i specify a split option as 2 then I should be able to get 5 Tree maps. Can you suggest if there is an easier option than below?


 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure about easier... but one more way of doing this would be to replace:

by


EDIT: Resolving a boundary condition (making it even more confusing )
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Aditya,

It is a nice one and worked fine . But it took some time to understand the logic. Thanks but I would rather try to stay bit simpler.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:I would rather try to stay bit simpler.

Always a good idea.

By the way, the logic I implemented can be explained as - Take all keys of the map in a list, and use List.subList() method to split into required sizes. I'm sure you must have figured it out by now.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My bad... when I increased the input size both of the methods did fail. The first one failed when there is an odd no. of inputs (say 11 / 13). I fixed it using a counter.

Input ->


Output ->
Small Map Key value -> Jai --- John
Small Map Key value -> John --- Paul
-------- Processing finished on small map ----------
Small Map Key value -> aaa --- bbb
Small Map Key value -> bleiben --- stay
-------- Processing finished on small map ----------
Small Map Key value -> glauben --- think
Small Map Key value -> hasse --- hate
-------- Processing finished on small map ----------
Small Map Key value -> hellO --- java
Small Map Key value -> java --- ranch
-------- Processing finished on small map ----------
Small Map Key value -> kalt --- cold
Small Map Key value -> laufen --- run
-------- Processing finished on small map ----------
Small Map Key value -> paul --- jai
Small Map Key value -> putzen --- clean
-------- Processing finished on small map ----------
Small Map Key value -> schreiben --- write
Small Map Key value -> wohnen --- live
-------- Processing finished on small map ----------


*********** Calling aditya's method *******
Value of i -> 0
Value of j -> 2
Small Map Key value -> Jai --- John
Small Map Key value -> John --- Paul
-------- Processing finished on small map ----------
Value of i -> 2
Value of j -> 4
Small Map Key value -> aaa --- bbb
Small Map Key value -> bleiben --- stay
-------- Processing finished on small map ----------
Value of i -> 4
Value of j -> 8
Small Map Key value -> glauben --- think
Small Map Key value -> hasse --- hate
Small Map Key value -> hellO --- java
Small Map Key value -> java --- ranch
-------- Processing finished on small map ----------
Value of i -> 8
Value of j -> 15
Small Map Key value -> kalt --- cold
Small Map Key value -> laufen --- run
Small Map Key value -> paul --- jai
Small Map Key value -> putzen --- clean
Small Map Key value -> schreiben --- write
Small Map Key value -> wohnen --- live
Small Map Key value -> zufrieden --- satisfied
-------- Processing finished on small map ----------
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I have corrected the logic. Should be ok now.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:I fixed it using a counter.

I suppose you could fix it by adding this right after your loop:
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay and i corrected mine like below -
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic