Java Best Practices for Clearing a Linked List

I am encoding a linked list data structure in java (I don’t use any standard java libraries for my training) and I want to clear the data structure via null from the links. Please suggest me which approach is better

1) Just enter the link to the top of the list and that will be enough.

2) In addition to nullifying the beginning, I de-link all the following pointers of all internal nodes to null. Does this help the garbage collector in any way?

The JDK for implementing LinkedList is confused in I, see approach 2. But I don't see the same for TreeMap

I am using JDK 8

+4
source share
1

, .

: . , . ( ) , , , .

( , , LinkedList ArrayList ArrayDeque. , . , LinkedList , , .)

, , clear LinkedList . JDK 8. 2003 , JDK 5. JDK-4863813. ( ) , . , .

, LinkedList, , , . , JVM . , , , , , , . ( , , . , parallelism .) LinkedList , , -, .

2009 , LinkedList " ". JDK-6897553 . " " clear() O (n) O (1), . (, , !) , , .

,

, Iterator

, :

// fields in some class
List<Obj> list = createAndPopulateALinkedList();
Iterator<Object> iterator;

void someMethod() {
    iterator = list.iterator();
    // ...
    list.clear();
}

. , , node , clear() . , , , , . , , for.

, TreeMap. , , LinkedList , TreeMap - . , JDK , LinkedList , TreeMap. , . , , LinkedList, , TreeMap. , TreeMap.

+2

Source: https://habr.com/ru/post/1617590/


All Articles