Мутация списка и сборка мусораJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Мутация списка и сборка мусора

Сообщение Anonymous »

У меня есть следующий класс Java.

Код: Выделить всё


import java.util.List;
import java.util.Objects;

public final class CircularNavigatorString {

private final List items;
private int index = 0;

public CircularNavigatorString(List items) {
Objects.requireNonNull(items, "items cannot be null");
if (items.isEmpty() || items.size() < 2) {
throw new IllegalArgumentException("items list cannot be empty or contain < 2 items");
} else {
System.out.println("List is valid with items#: " + items.size());
}
this.items = List.copyOf(items); // immutable
}

// ---------- ACCESSORS ----------

public List items() {
return items;
}

public int currentIndex() {
return index;
}

public String current() {
return items.get(index);
}

// ---------- MUTATORS ----------

public void setCurrentIndex(int newIndex) {
if (newIndex < 0 || newIndex >= items.size()) {
throw new IndexOutOfBoundsException("Invalid index: " + newIndex);
}
this.index = newIndex;
}

public void setCurrentItem(String item) {
int pos = items.indexOf(item);
if (pos < 0) {
throw new IllegalArgumentException("Item not found: " + item);
}
this.index = pos;
}

// ---------- NAVIGATION ----------

public String first() {
index = 0;
return current();
}

public String last() {
index = items.size() - 1;
return current();
}

public String next() {
index = (index + 1) % items.size();
return current();
}

public String previous() {
index = (index - 1 + items.size()) % items.size();
return current();
}

public static void main(String[] args) {

var nav = new CircularNavigatorString(List.of("Alpha", "Bravo", "Charlie", "Delta"));

System.out.println("Current: " + nav.current()); // Alpha
System.out.println("Next: " + nav.next()); // Bravo
System.out.println("Next: " + nav.next()); // Charlie
System.out.println("Next: " + nav.next()); // Delta
System.out.println("Next: " + nav.next()); // Alpha (wraps)

nav.setCurrentItem("Charlie");
System.out.println("Current: " + nav.current()); // Charlie
System.out.println("Previous: " + nav.previous()); // Bravo
System.out.println("Previous: " + nav.previous()); // Alpha
System.out.println("Previous: " + nav.previous()); // Delta (wraps)

nav.setCurrentIndex(3);
System.out.println("Current: " + nav.current()); // Delta
System.out.println("Next: " + nav.next()); // Alpha (wraps)
System.out.println("Previous: " + nav.previous()); //Delta
}

}
Как мы все знаем, здесь List.copyOf(items) дает неизменяемый список.
Вопросы:
  • Для МУТАТОРОВ, где происходит мутация?
  • В памяти? Если да, стоит ли мне беспокоиться о GC (сборке мусора)? Как этого добиться?
  • Или это происходит ТОЛЬКО в «едином представлении» списка и сбор мусора не требуется?


Подробнее здесь: https://stackoverflow.com/questions/798 ... collection
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»