Skip to content

Instantly share code, notes, and snippets.

@casidiablo
Created April 17, 2012 18:10
Show Gist options
  • Save casidiablo/2407905 to your computer and use it in GitHub Desktop.
Save casidiablo/2407905 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Dummy {
private static final Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
static {
map.put(120, Arrays.asList(1));
map.put(121, Arrays.asList(2, 3));
map.put(122, Arrays.asList(4, 5, 6));
// 0 - 0,0 | 1 - 1,0 | 2 - 1,1,
// etc.
}
public static int getChildId(int index) {
int sum = 0;
List<Integer> keys = new ArrayList<Integer>(map.keySet());
Collections.sort(keys);
for (Integer key : keys) {
int innerIndex = index - sum;
List<Integer> innerList = map.get(key);
int size = innerList.size();
if (innerIndex < size) {
return innerList.get(innerIndex);
}
sum += size;
}
return -1;// unknown or invalid index
}
// use this if you want to know how many items there are
public static int getCount() {
int sum = 0;
for (Integer key : map.keySet()) {
sum += map.get(key).size();
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment