• Post Reply Bookmark Topic Watch Topic
  • New Topic

splitting the map on a certain condition  RSS feed

 
tuntun punpun
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I have a map as shown below:





As you can see, there will be final constant named split whose value is 40



I have to implement the logic such as if the value of the map reaches 40,
then the first key of the map from where the calculation started and the key where exactly it reaches to 40 will be chosen as min and max also, as explained above.

Besides this, care needs to be taken if sum reaches more than 40. If so, we have to ignore it and take the previous value itself as min and max in the case where min and max would be equal.

Please suggest me how can I achieve this with Java and `Map`.


 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tuntun punpun wrote:Hi Folks,

I have a map as shown below:





As you can see, there will be final constant named split whose value is 40



I don't understand the requirement. In the first case you are adding two consecutive values. In the next case also you are adding the next set of two consecutive values. Why in the third case you are adding three consecutive values?

since 42 >40 so here min and max will be same that is 43

Why 43? Why not 45? What if the sum of the values was 40 again? What if it was less than 40?

 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what are you going to do with these min and max values? Where will they go? I mean are you going to display them to the user?
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heena Agarwal wrote:I don't understand the requirement. In the first case you are adding two consecutive values. In the next case also you are adding the next set of two consecutive values. Why in the third case you are adding three consecutive values?


And why those two or three values? Why not some other values? (Heena, you used the word "consecutive" which suggests you are assuming that there is some ordering to this Map, whereas a Map generally doesn't have an ordering.) And what are the keys for?
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Heena Agarwal wrote:I don't understand the requirement. In the first case you are adding two consecutive values. In the next case also you are adding the next set of two consecutive values. Why in the third case you are adding three consecutive values?


And why those two or three values? Why not some other values? (Heena, you used the word "consecutive" which suggests you are assuming that there is some ordering to this Map, whereas a Map generally doesn't have an ordering.) And what are the keys for?


Do you mean the values are not ordered? Or are you saying that Maps are unordered? And TreeMap and LinkedHashMap are SortedMap and hence we shouldn't associate order with a Map unless someone specifically mentions they are talking about a SortedMap?

 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heena Agarwal wrote:Or are you saying that Maps are unordered? And TreeMap and LinkedHashMap are SortedMap and hence we shouldn't associate order with a Map unless someone specifically mentions they are talking about a SortedMap?


Well, basically yes, but the original post doesn't have anything like that sort of detail.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Heena Agarwal wrote:Or are you saying that Maps are unordered? And TreeMap and LinkedHashMap are SortedMap and hence we shouldn't associate order with a Map unless someone specifically mentions they are talking about a SortedMap?


Well, basically yes, but the original post doesn't have anything like that sort of detail.


Yeah. The original post doesn't say that the entries are ordered.

Edit : Paul, I was just thinking about what you've suggested. I just realized that it is important to not think of specializations while we are referring to a more general API. Like for instance the Arrays' asList method just returns a List and that is all we must consider it returns. Thanks for the correction.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!