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

Java LinkedList size() Method

Java LinkedList size() Method

Java LinkedList Java LinkedList


size() The method is in Java's LinkedList is a commonly used method provided by the class; it belongs to java.util package. The main purpose of this method is to return the number of elements currently in the list.

Method Syntax:

public

int

size

(

)

Return Value:


Method Characteristics

Time Complexity

size() method has a time complexity of O(1), which means that regardless of how many elements are in the list, the time to get the size is constant.

This is because LinkedList class internally maintains a size variable, which is automatically updated when elements are added or removed, and calling size() method simply returns the value of this variable.

Difference from array length

Beginners sometimes confuse size() method and the array's length property through the following steps:

Thread Safety

size() method itself is not thread-safe. If used in a multi-threaded environment, additional synchronization measures are needed.


Usage examples

Basic Usage

Example

import

java.util.LinkedList

;

public

class

LinkedListSizeDemo

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a LinkedList

        LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

       

       

// Add elements

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Orange"

)

;

       

       

// Use the size() method to get the number of elements

       

int

size

=

fruits.

size

(

)

;

       

System

.

out

.

println

(

"Number of elements in LinkedList: "

size

)

;

// Output: 3

   

}

}

Empty list situation

Example

import

java.util.LinkedList

;

public

class

EmptyLinkedListDemo

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
Integer
numbers
=
new
LinkedList
<>
(
)

;

       

System

.

out

.

println

(

"Size of empty list: "

numbers.

size

(

)

)

;

// Output: 0

   

}

}

Dynamic change demonstration

Example

import

java.util.LinkedList

;

public

class

DynamicSizeDemo

{

   

public

static

void

main

(

String

[

]

args

)

{

        LinkedList

<
Character
letters
=
new
LinkedList
<>
(
)

;

       

System

.

out

.

println

(

"Initial size: "

letters.

size

(

)

)

;

// 0

       

        letters.

add

(

'A'

)

;

        letters.

add

(

'B'

)

;

       

System

.

out

.

println

(

"After adding two elements: "

letters.

size

(

)

)

;

// 2

       

        letters.

removeFirst

(

)

;

       

System

.

out

.

println

(

"After removing one element: "

letters.

size

(

)

)

;

// 1

       

        letters.

clear

(

)

;

       

System

.

out

.

println

(

"After clearing: "

letters.

size

(

)

)

;

// 0

   

}

}


Common questions

Does the size() method throw an exception?

size() method usually does not throw an exception. Even if the list is null (note: not an empty list), calling size() method will not throw an exception, but it will throw NullPointerException.

Example

LinkedList

<
String
list
=
null

;

// The following line will throw NullPointerException

System

.

out

.

println

(

list.

size

(

)

)

;

The relationship between size() and isEmpty()

isEmpty() method is based on size() method:

Example

public

boolean

isEmpty

(

)

{

   

return

size

(

)

==

0

;

}

Therefore,list.isEmpty() is equivalent to list.size() == 0, but the former is more readable.


Practical Application Scenarios

Loop traversal

Example

LinkedList

<
String
colors
=
new
LinkedList
<>
(
)

;

colors.

add

(

"Red"

)

;

colors.

add

(

"Green"

)

;

colors.

add

(

"Blue"

)

;

for

(

int

i

=

0

;

i

<

colors.

size

(

)

;

i

++

)

{

   

System

.

out

.

println

(

colors.

get

(

i

)

)

;

}

Capacity check

Example

LinkedList

<
Integer
queue
=
new
LinkedList
<>
(
)

;

// ... Add elements ...

if

(

queue.

size

(

)

10

)

{

   

System

.

out

.

println

(

"Queue is full, cannot add more elements"

)

;

}

Comparison with other collections

Example

LinkedList

<
String
list1
=
new
LinkedList
<>
(
)

;

LinkedList

<
String
list2
=
new
LinkedList
<>
(
)

;

// ... Fill two linked lists ...

if

(

list1.

size

(

)

==

list2.

size

(

)

)

{

   

System

.

out

.

println

(

"The two linked lists have the same size"

)

;

}


Performance considerations

Although size() method itself has O(1) time complexity, but in some cases frequent calls to size() may have performance impact:

  1. Multi-threaded environment: every time you call size() both need to read the size Variables
  2. Complex loop conditions: Such as for (int i = 0; i < list.size(); i++), and it is called on every loop iteration size()

For the second case, it can be optimized as:

Example

int

size

=

list.

size

(

)

;

for

(

int

i

=

0

;

i

<

size

;

i

++

)

{

   

// Loop body

}


Summary

LinkedList 's size() method is a simple but very important method that provides the ability to quickly get the current number of elements in the list. Understanding how this method works and its characteristics helps write more efficient and reliable Java code.

Remember:

Java LinkedList Java LinkedList