Java LinkedList clear() Method
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:
- Removes the
LinkedListall elements in the - Resets the list's size to 0
- Does not affect the list's capacity, because
LinkedListthere 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:
- Traverse all nodes in the list
- Set the predecessor and successor references of each node to null
- Set the list's head node (first) and tail node (last) to null
- Set the list's size to 0
This implementation helps the garbage collector reclaim node objects that are no longer in use.
Method Characteristics
- Time Complexity: O(n) - needs to traverse all nodes
- Space complexity: O(1) - no additional storage space is needed
- Thread Safety: Not thread-safe; external synchronization is required in multi-threaded environments
- Return Value: None (void)
Notes
- called
clear()After that, all elements in the list will be removed and cannot be recovered - If the list is shared by multiple references, all references will see the list cleared
- After clearing the list, you can continue to add new elements
- 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:
- Reuses the list object: When you need to reuse the same list object
- Memory management: When you need to release the memory occupied by the list
- 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