Was ist los mit Sammlungen in Java und warum wird Guava nicht helfen

Einführung


Der Titel des Beitrags ist wirklich "heilig", aber meiner Meinung nach haben sowohl Java als auch die beliebte Guava- Bibliothek eine Reihe von Architekturproblemen, die auf lange Sicht zu Unklarheiten und Meinungsverschiedenheiten im Team führen.
Bild


Java Collections Framework und Unveränderlichkeit auf Vertrauen


Collections Framework , . java.util.Collection add, clear, remove ... : Collections.emptyList(), Collections.singletonList<T>(T t), Arrays.asList<T>(T.. values) . . :


List<Integer> list = Arrays.asList(21, 22, 23);
list.add(24)        //  ! UnsupportedOperationException

? java.util.List java.util.Collection, add. , Arrays.asList<T>(T... t) , add remove . : , . , - .


List<Integer> list = Arrays.asList(21, 22, 23);
list.set(0, 121);
System.out.println(list);        // [121, 22, 23]

. . , . , , , . .


List<Integer> filtered = filter(List<Integer> list);

filter List<Integer> . ? , ? try {} catch(Exception e) {} add remove — . ? ? list, filtered? . , . , — . , , list.clear(), , . SOLID, L — , , . , add , .


- , Stream API. , Java , , , . , , .


, java.util.Collection, java.util.Iterable, Collection. Iterable , Iterator, for-each . .


public interface Iterator<E> {
    boolean hasNext();

    E next();

    default void remove() {
        throw new UnsupportedOperationException("remove");
    }

    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

remove. , for-each, , , API , , . - , , , , Java , .


Guava


Guava? — ImmutableList.


ImmutableList<String> people = ImmutableList.of("Michael", "Simon", "John")
...

? «Immutable». , ImmutableList java.util.List, add, clear ... , . , final. : , List Collection. , ImmutableList, , , « ». Guava .


Kotlin


? , JetBrains Kotlin. , , , . :


Bild


, , , .


val list: List<Int> = listOf(1, 2, 3)
val mutableList: MutableList<Int> = list as MutableList<Int>
mutableList.clear()      //  !  UnsupportedOperationException

- , MutableList List MutableList, , . , Java Collections Framework, Guava: , . , , . , Java, - , , , .



? ? , : , , , . . Java Collections Framework Immutable. :


Bild


, , «Immutable». , . , ImmutableQueue, , . . LinkedList , , ( , ). ImmutableLinkedList .


- , , . List ImmutableList. , toMutableList toImmutableList ImmutableList List . , , . , ArrayList ImmutableArrayList . . ArrayList , ImmutableArrayList, ImmutableArrayList ArrayList. API , .


, — java.util.Iterable. , for-each , . java.util.Iterator, , remove, . , .


  1. ImmutableIterable, ImmutableIterator. , , Java for-each Iterable, ImmutableIterable.
  2. ImmutableIterator, java.util.Iterator remove, final, . , .
  3. remove Iterator, , Java, , .


, Java JP . , — , Java-. , , . !


P.S.


Ich arbeite derzeit an einer Open-Source-Bibliothek, die vollständig unveränderliche Java-Sammlungen bereitstellt. Interessenten finden den Link auf Github .


All Articles