Last Updated : 24 Dec, 2018
public Object[] toArray()Parameters:It does not take in any parameter. Return Value:It returns an array containing all the elements in the set. Below examples illustrates the CopyOnWriteArraySet.toArray() method: Example 1: Java
// Java Program Demonstrate toArray()
// method of CopyOnWriteArraySet
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of CopyOnWriteArraySet
CopyOnWriteArraySet<Integer> set
= new CopyOnWriteArraySet<Integer>();
// Add numbers to end of CopyOnWriteArraySet
set.add(7855642);
set.add(35658786);
set.add(5278367);
set.add(74381793);
System.out.println("CopyOnWriteArraySet: "
+ set);
Object[] a = set.toArray();
System.out.println("Returned Array: "
+ Arrays.toString(a));
}
}
Output:
CopyOnWriteArraySet: [7855642, 35658786, 5278367, 74381793] Returned Array: [7855642, 35658786, 5278367, 74381793]
public <T> T[] toArray(T[] a)Parameters: The method accepts one parameter arr[] which is the array into which the elements of the CopyOnWriteArraySet are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. Return Value: The method returns an array containing the elements similar to the CopyOnWriteArraySet. Exception: The method might throw two types of exception:
// Java code to illustrate toArray(arr[])
import java.util.concurrent.*;
import java.util.*;
public class CopyOnWriteArraySetDemo {
public static void main(String args[])
{
// Creating an empty CopyOnWriteArraySet
CopyOnWriteArraySet<String> set
= new CopyOnWriteArraySet<String>();
// Use add() method to add
// elements into the CopyOnWriteArraySet
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("For");
set.add("Geeks");
// Displaying the CopyOnWriteArraySet
System.out.println("The CopyOnWriteArraySet: "
+ set);
// Creating the array and using toArray()
String[] arr = new String[5];
arr = set.toArray(arr);
// Displaying arr
System.out.println("Returned Array: "
+ Arrays.toString(arr));
}
}
Output:
The CopyOnWriteArraySet: [Welcome, To, Geeks, For] Returned Array: [Welcome, To, Geeks, For, null]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4