How to increment tailSet integers in Treeset<Atomicinteger> without addAll()?

Asked

Viewed 22 times

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.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.