toArray(K[]): This method is available in java.util.HashSet class of Java.
Syntax:
<K> K[] java.util.HashSet.toArray(K[] a)
This method takes one argument. This method returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.
Parameters: One parameter is required for this method.
a: the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns: an array containing all the elements in this set.
Exceptions: NA
Approach
Java
import java.util.Arrays;import java.util.HashSet;public class HashSettoArray3 {public static void main(String[] args) {HashSet<String> hashSet = new HashSet<String>();hashSet.add("Hello");hashSet.add("Java");hashSet.add("C++");hashSet.add("Hello");String arr[] = { "Hello", "Java" };System.out.println(Arrays.toString(hashSet.toArray(arr)));}}
Output:
[Java, C++, Hello]
No comments:
Post a Comment