I may get a Map
of many many items that needs to be requested to an API.
In order to improve performance, I thought about splitting that list into smaller sublists, and querying the API with this small subset.
For that, I came to this code and was wondering what was your opinion on it.
This is some banal initiator I used to test my code:
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
for (int i = 0; i < 550;i ++) {
map.put(String.valueOf(i), i);
}
Now the real code:
int length = 100;
List<Integer> items = new LinkedList<Integer>(map.values());
int pages = (int) Math.ceil((double) items.size() / length);
for (int i = 0; i < pages; i++) {
List<Integer> sub = items.subList(i * length, ((i+1) * length > items.size() ? items.size() : (i+1) * length));
System.out.println(sub.get(0));
System.out.println(sub.get(sub.size() - 1));
System.out.println("--");
// In my case, I would call the API here, at that place, using the sublist
}
The final output is:
0 99 -- 100 199 -- 200 299 -- 300 399 -- 400 499 -- 500 549 --
This works great. What's your opinion on it?