CopyOnWriteArraySet toArray() method in Java with Example

  • toArray()

    The Java.util.concurrent.CopyOnWriteArraySet.toArray() method returns an array containing all the elements in the set in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based and collection-based APIs.

    Syntax

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    Output:

    CopyOnWriteArraySet: [7855642, 35658786, 5278367, 74381793]
    Returned Array: [7855642, 35658786, 5278367, 74381793]
    
  • toArray(T[])

    The toArray(arr[]) method method of CopyOnWriteArraySet class in Java is used to form an array of the same elements as that of the CopyOnWriteArraySet. It returns an array containing all of the elements in this CopyOnWriteArraySet in the correct order; the run-time type of the returned array is that of the specified array. If the CopyOnWriteArraySet fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the run time type of the specified array and the size of this CopyOnWriteArraySet.
    If the CopyOnWriteArraySet fits in the specified array with room to spare (i.e., the array has more elements than the CopyOnWriteArraySet), the element in the array immediately following the end of the CopyOnWriteArraySet is set to null. (This is useful in determining the length of the CopyOnWriteArraySet only if the caller knows that the CopyOnWriteArraySet does not contain any null elements.)

    Syntax:

    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:

    • ArrayStoreException: When the mentioned array is of the different type and is not able to compare with the elements mentioned in the CopyOnWriteArraySet.
    • NullPointerException: If the array is Null, then this exception is thrown.

    Below program illustrates the working of the CopyOnWriteArraySet.toArray(arr[]) method.

    Program 1: When array is of the size of CopyOnWriteArraySet

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    Output:

    The CopyOnWriteArraySet: [Welcome, To, Geeks, For]
    Returned Array: [Welcome, To, Geeks, For, null]
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.