1
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 exchange 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, so Treeset would still be right. But that changes the elements outside of Treeset, so I’m in doubt.