-1

I am having HashMap testmap;

Which is having data with multiple keys this way,

{
    status = [1, 2, 3, 4, 5], data = [
        [1, 2, 3, 4, 5, 6],
        [7, 8 9, 10, 11, 12],
        [13, 14 15, 16, 17, 18],
        [19, 20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29, 30],
    ]
], warning = [
    [1, 2, 3, 4, 5, 6],
    [7, 8 9, 10, 11, 12],
    [13, 14 15, 16, 17, 18],
    [19, 20, 21, 22, 23, 24],
    [25, 26, 27, 28, 29, 30],
], error = [1, 2, 3, 4, 5, 6], [7, 8 9, 10, 11, 12], [13, 14 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30],
}

I am trying to read the values from each key from the above map LIMIT by limit.

I.e, initially start limit =1 and end limit-3, then is should get only 1,2,3 values from each key of above hashmap.

Then if i send start limit =4 and end limit-6, then is hould get only 4,5,6 values form each key of above hashmap.

I tried this way,

Object firstKey = myHashMap.keySet().toArray()[3];
Object valueForFirstKey = myHashMap.get(firstKey);
//iterated the values from so getting 

and got this way, [data = [1,2,3]]

     Instead of this looping , can i get subset of values using both start and end index from hashmap for all the above keys like this?
if start index=1 and end index =3 it should return me output this way,

{
    status = [1, 2, 3], data = [
        [1, 2, 3],
        [7, 8 9],
        [13, 14 15],
        [19, 20, 21],
        [25, 26, 27],
    ]
], warning = [
    [1, 2, 3, [7, 8 9],
        [13, 14 15],
        [19, 20, 21],
        [25, 26, 27],
    ], error = [1, 2, 3],
    [7, 8 9],
    [13, 14 15],
    [19, 20, 21],
    [25, 26, 27],
}

without looping through each key and value as above? What mistake have I made?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Sandy
  • 313
  • 1
  • 8
  • 23
  • 2
    It's unclear to me what the actual keys are. Instead of just showing us JSON, could you show us a [mcve]? – Jon Skeet Mar 18 '16 at 06:44
  • @JonSkeet Actually this map is created from JSOn only.. And I am trying to manipulate it.. – Sandy Mar 18 '16 at 06:46
  • So you can show that (with a smaller data set). Or you could build the same map manually. Either way, you could still reproduce the problem in a way which helps us to see it for ourselves in a complete way. – Jon Skeet Mar 18 '16 at 06:48
  • @Jon Skeet added new help request for this simulating with the pure java map version – Sandy Mar 18 '16 at 08:33
  • No, you haven't added anything - and you haven't clarified the question. *We can't help you if we don't understand the question* - and the simplest way of helping us to understand is by producing a [mcve]. – Jon Skeet Mar 18 '16 at 09:13

1 Answers1

0

Like Jon Skeet noted it's unclear what you are asking.

My best guess what you need is:

item.data
  .SelectMany(o => o)   // linarilize array of arrays
  .Skip(i * 3)          // skip first i * 3 elements 
  .Take(3);             // take first 3 elements from remaining array

This example is in c#, in Java you have more verbose methods. If you happen to use Java 8 then I would imagine you could solve this with:

integerListStream
  .flatMap(Collection::stream)
  .skip(i * 3)
  .limit(3);
Margus
  • 19,694
  • 14
  • 55
  • 103