Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList clear() Method

Java LinkedList clear() Method

Java LinkedList Java LinkedList


clear() The method is in Java's LinkedList is a commonly used method provided by the class for removing all elements from the list. After calling this method, the list becomes empty.

Method Syntax


public void clear()

Method Functionality

clear() The main functions of the method are:

  1. Removes the LinkedList all elements in the
  2. Resets the list's size to 0
  3. Does not affect the list's capacity, because LinkedList there is no capacity limit

Usage examples

Basic Usage

Example

import

java.util.LinkedList

;

public

class

ClearExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a LinkedList

        LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

       

       

// Add elements

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Cherry"

)

;

       

       

System

.

out

.

println

(

"Linked list before clearing: "

fruits

)

;

       

System

.

out

.

println

(

"Size before clearing: "

fruits.

size

(

)

)

;

       

       

// Use the clear() method to empty the list

        fruits.

clear

(

)

;

       

       

System

.

out

.

println

(

"Linked list after clearing: "

fruits

)

;

       

System

.

out

.

println

(

"Size after clearing: "

fruits.

size

(

)

)

;

   

}

}

Output result:

清空前的链表: [Apple, Banana, Cherry]
清空前的大小: 3
清空后的链表: []
清空后的大小: 0

Implementation principle

In Java's LinkedList implementation,clear() method works through the following steps:

  1. Traverse all nodes in the list
  2. Set the predecessor and successor references of each node to null
  3. Set the list's head node (first) and tail node (last) to null
  4. Set the list's size to 0

This implementation helps the garbage collector reclaim node objects that are no longer in use.


Method Characteristics

  1. Time Complexity: O(n) - needs to traverse all nodes
  2. Space complexity: O(1) - no additional storage space is needed
  3. Thread Safety: Not thread-safe; external synchronization is required in multi-threaded environments
  4. Return Value: None (void)

Notes

  1. called clear() After that, all elements in the list will be removed and cannot be recovered
  2. If the list is shared by multiple references, all references will see the list cleared
  3. After clearing the list, you can continue to add new elements
  4. The difference between new LinkedList<>() Unlike creating a new instance,clear() the method reuses the existing object

Comparison with Other Methods

Method Function Time Complexity Whether the original list is modified
clear() clears all elements O(n) Is
removeAll() Removes all elements of a specified collection O(n*m) Is
new LinkedList<>() Creates a new empty list O(1) No

Practical Application Scenarios

clear() The method is especially useful in the following scenarios:

  1. Reuses the list object: When you need to reuse the same list object
  2. Memory management: When you need to release the memory occupied by the list
  3. Reset state: When you need to reset the list state during algorithm or data processing

Example

// Practical application example: processing batch data

LinkedList

<
Data
dataBuffer
=
new
LinkedList
<>
(
)

;

// Phase 1: Collect data

collectData

(

dataBuffer

)

;

// Process data

processData

(

dataBuffer

)

;

// Clear the buffer to prepare for the next batch of data

dataBuffer.

clear

(

)

;

// Phase 2: Collect new data

collectData

(

dataBuffer

)

;

By using properly clear() method, you can improve the efficiency and readability of your code.

Java LinkedList Java LinkedList