I have the following part of a class:
private TreeSet<AtomicInteger> offsets;
// ...
public void addIndex(int index) {
SortedSet<AtomicInteger> set = offsets.tailSet(new AtomicInteger(index), false);
AtomicInteger[] backup = set.toArray(new AtomicInteger[0]);
set.clear();
for (AtomicInteger i : backup) {
i.incrementAndGet();
}
offsets.addAll(Arrays.asList(backup));
}
addIndex () basically increments all indexes after index
of the set. My question is: can I change the method for this?
public void addIndex(int index) {
SortedSet<AtomicInteger> set = offsets.tailSet(new AtomicInteger(index));
for (AtomicInteger i : set) {
i.incrementAndGet();
}
}
Because the integers are still in order, then TreeSet would still be right. But this changes the elements outside of the TreeSet, so I'm in doubt.