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

Java HashSet toArray() Method

Java HashSet toArray() Method

Java HashSet Java HashSet


toArray() method is a method defined in the Java Collections Framework, used to convert a collection into an array. As an implementation class of the Collection interface, HashSet naturally provides this method.

Method Signature

HashSet provides two toArray() overloaded forms of the method:

  1. Object[] toArray()
  2. T[] toArray(T[] a)

First form: Object[] toArray()

This is the simplest form; it converts all elements in the HashSet into an Object array.

Syntax

Example

Object

[

]

array

=

hashSet.

toArray

(

)

;

Example Code

Example

import

java.util.HashSet

;

public

class

HashSetToArrayExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        HashSet

<

String

fruits

=

new

HashSet

<>

(

)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Orange"

)

;

       

       

// Use the toArray() method to convert to an array

       

Object

[

]

fruitArray

=

fruits.

toArray

(

)

;

       

       

// Iterate over the array

       

for

(

Object

fruit

:

fruitArray

)

{

           

System

.

out

.

println

(

fruit

)

;

       

}

   

}

}

Output result

Orange
Banana
Apple

Note: Since HashSet is unordered, the output order may differ from the insertion order.

Feature


Second form: T[] toArray(T[] a)

This form allows us to specify the type of the returned array, making it more flexible and safer.

Syntax

Example

String

[

]

array

=

hashSet.

toArray

(

new

String

[

0

]

)

;

Example Code

Example

import

java.util.HashSet

;

public

class

HashSetToArrayExample2

{

   

public

static

void

main

(

String

[

]

args

)

{

        HashSet

<

String

fruits

=

new

HashSet

<>

(

)

;

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Orange"

)

;

       

       

// Use the toArray(T[] a) method to convert to an array of a specific type

       

String

[

]

fruitArray

=

fruits.

toArray

(

new

String

[

0

]

)

;

       

       

// Iterate over the array

       

for

(

String

fruit

:

fruitArray

)

{

           

System

.

out

.

println

(

fruit

)

;

       

}

   

}

}

Output result

Orange
Banana
Apple

Parameter Description

Feature


Comparison of the Two Methods

Feature Object[] toArray() T[] toArray(T[] a)
Return Type Object[] Specify type T[]
Type Safety Unsafe Safe
Requires type casting Is No
Array reuse possibility None Yes
Ease of use Simple Slightly complex

Practical Application Scenarios

Scenario 1: Need an array of a specific type

When we need to convert a HashSet into an array of a specific type, we should use the second form:

Example

HashSet

<

Integer

numbers

=

new

HashSet

<>

(

)

;

numbers.

add

(

1

)

;

numbers.

add

(

2

)

;

numbers.

add

(

3

)

;

// Convert to an Integer array

Integer

[

]

numArray

=

numbers.

toArray

(

new

Integer

[

0

]

)

;

Scenario 2: Need to reuse an existing array

If we already have an array and want to reuse it:

Example

String

[

]

existingArray

=

new

String

[

5

]

;

// ... fill part of existingArray ...

HashSet

<

String

set

=

new

HashSet

<>

(

)

;

set.

add

(

"A"

)

;

set.

add

(

"B"

)

;

// Reuse existingArray

String

[

]

result

=

set.

toArray

(

existingArray

)

;

Note: If existingArray is not large enough, a new array is created; if the size is sufficient, existingArray is used.


Notes

  1. Concurrent Modification: If the collection is modified during the conversion to an array, the behavior of the method is undefined.

  2. null Element: HashSet can contain null elements, and the converted array will also contain null.

  3. Performance considerations: For large collections, the second method (specifying the array size) may be more efficient:

Example

// More efficient way - specify the correct size in advance

String

[

]

array

=

set.

toArray

(

new

String

[

set.

size

(

)

]

)

;

  1. Type Safety: When using the first method, after retrieving elements from the Object array, manual type casting is required, which may throw ClassCastException.

Summary

HashSet's toArray() method provides a convenient way to convert a collection into an array:

  1. can be used in simple cases Object[] toArray()
  2. use when type safety or a specific type array is needed T[] toArray(T[] a)
  3. For large collections, specifying the array size in advance can improve performance

Understanding the differences and applicable scenarios of these two methods can help us use HashSet more effectively in actual development.

Java HashSet Java HashSet