Java HashSet toArray() Method
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:
Object[] toArray()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
- Returns an array of type Object[]
- Manual type casting is required to use methods of a specific type
- The new array is independent of the original collection; modifying the array does not affect the collection
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
- Parameters
ais an array of type T - If the array
ais large enough to hold all elements in the collection, then this array is used - If it is not large enough, a new array is created
- Usually an array of size 0 is passed (such as
new String[0]), letting the JVM automatically decide the array size
Feature
- Returns an array of the specified type without type casting
- Safer, avoiding ClassCastException
- Can reuse the passed array (if the size is sufficient)
Comparison of the Two Methods
| Feature | Object[] toArray() | |
|---|---|---|
| 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
-
Concurrent Modification: If the collection is modified during the conversion to an array, the behavior of the method is undefined.
-
null Element: HashSet can contain null elements, and the converted array will also contain null.
-
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
(
)
]
)
;
- 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:
- can be used in simple cases
Object[] toArray() - use when type safety or a specific type array is needed
T[] toArray(T[] a) - 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