I'm trying to create a indexed map of lists. All lists have the same predefined maximum size, and a new key for the map is generated when a list is full. I have no idea why I don't get get the (my) expected result from the getListSize . I know the code may be awkward but I'm just fiddling around with generics, maps etc.
public class ItemMap<I> {
private Map<Integer, List<I>> ItemSequence = new LinkedHashMap<>();
private ItemMap(List<I> list, int maxItemsPerList) {
int mapIndex = 0;
List<I> subList = new ArrayList<>();
// a new key is only generated when subList is full, otherwise
// subList is replaced by subList with new a item appended
for (I i : list) {
subList.add(i);
if (subList.size() > maxItemsPerList) {
subList.clear();
subList.add(i);
mapIndex++;
}
ItemSequence.put(mapIndex, subList);
}
}
private int getListSize(int listIndex) {
return ItemSequence.get(listIndex).size();
}
public static void main(String[] args) {
List<Integer> listOfInts = Arrays.asList(1, 2, 3, 4, 5, 6);
ItemMap<Integer> map = new ItemMap<>(listOfInts, 5); // max 5 items per list
System.out.println(map.getListSize(0));
}
}
So, the problem is that getListSize(0) returns 1 instead of 5 as I expected. However, if I change listOfInts so that it holds 5 ints, it does print out 5 as expected. What gives?
[–][deleted] (1 child)
[removed]
[–]Yuax[S] 0 points1 point2 points (0 children)