Win a copy of Design for the Mind this week in the Design forum!

# Splitting a Map

John Jai
Rancher
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?

Ranch Hand
Posts: 227
• 1
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

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.

Ranch Hand
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
Rancher
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 ----------

Ranch Hand
Posts: 227
Yes. I have corrected the logic. Should be ok now.