I have in code
List<HashSet<Integer>> list = new ArrayList<HashSet<Integer>>(50);
pos = 17; // just some index less than 50
list.add(pos, new HashSet<Integer>());
list.get(17).add(99);
list.get(17).add(88);
After a while I want to remove the HashSet with {99, 88} inside and create a new one, like this:
// pos is still here
list.add(pos, new HashSet<Integer>());
Is it allowed? Do you know any faster solution? Thanks.
list.add(pos, new HashSet<Integer>());is going to add a new set at that position and shift the existing one topos+1. That does not seem to be what you want.If you want to replace the set by a new one, use
set:In terms of performance,
ArrayList#setruns in constant time so it is an efficient way of replacing the existing set in the list.