aspose file tools*
The moose likes Beginning Java and the fly likes Splitting a Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Splitting a Map" Watch "Splitting a Map" New topic
Author

Splitting a Map

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 25, 2003
Posts: 227

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 25, 2003
Posts: 227

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 25, 2003
Posts: 227

Yes. I have corrected the logic. Should be ok now.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

John Jai wrote:I fixed it using a counter.

I suppose you could fix it by adding this right after your loop:
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Okay and i corrected mine like below -
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Splitting a Map