Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
CopyOnWriteArrayList retainAll() method in Java with Examples
Next article icon

CopyOnWriteArraySet toArray() method in Java with Example

Last Updated : 24 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
  • 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:




    // 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]  
  • 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




    // 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]  


Next Article
CopyOnWriteArrayList retainAll() method in Java with Examples

S

SandySharma
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2018
Practice Tags :
  • Java

Similar Reads

  • CopyOnWriteArraySet retainAll() method in Java with Example
    The retainAll() method of java.util.concurrent.CopyOnWriteArraySet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to
    3 min read
  • CopyOnWriteArrayList set() method in Java with Examples
    The set(E e) method in the class CopyOnWriteArrayList class replaces the element at the specified index with the element provided as a parameter to the method. The method returns the element that has been replaced by the new element.Syntax: public E set(int index, E element) Parameters: The method t
    2 min read
  • CopyOnWriteArraySet removeAll() method in Java with Examples
    The removeAll() method of CopyonWriteArraySet method removes all the elements of this CopyOnWriteArraySet that are present in the specified collection. That means elements which are common in both the collections are removed from this CopyOnWriteArraySet. Syntax: public boolean removeAll(Collection
    2 min read
  • CopyOnWriteArraySet removeIf() method in Java with Examples
    The removeIf() method of CopyonWriteArraySet method removes the element from this CopyOnWriteArraySet that satisfies the specified condition. Syntax: public boolean removeIf (Predicate<E> filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on
    2 min read
  • CopyOnWriteArrayList retainAll() method in Java with Examples
    The Java.util.concurrent.CopyOnArrayList.retainAll() method in Java is used to retain only the elements in the list that are contained in specific collection. Syntax: public boolean retainAll(Collection col) Parameters: This method accepts a mandatory parameter col which is of the type of collection
    2 min read
  • CopyOnWriteArrayList remove() method in Java with Examples
    The remove()method of CopyOnArrayList in Javais used to remove the element in the list. Syntax: 1. public E remove(int index) 2. public boolean remove(Object o) 1. remove(int index) The remove(int index) method of CopyOnArrayList in Java is used to remove the element at the specified position in the
    3 min read
  • CopyOnWriteArrayList subList() method in Java with Examples
    The subList() method of java.util.CopyOnWriteArrayList class is used to return a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-str
    2 min read
  • CopyOnWriteArrayList removeAll() method in Java with Examples
    The removeAll() method in CopyOnWriteArrayList class that removes all the elements that are contained in the specified collection from the CopyOnArrayList object you call on. Syntax: public boolean removeAll(Collection collection) Parameter: The method accepts only a single parameter collection whic
    2 min read
  • CopyOnWriteArraySet forEach() method in Java with Examples
    The forEach() method of CopyOnWriteArraySet is an in-built function in Java which is used to traverse each element in this Set. Syntax: public void forEach (Consumer<E> action) Parameters: This method takes a parameter action which represents the action to be performed for each element. Return
    2 min read
  • CopyOnWriteArraySet addAll() method in Java with Examples
    The addAll() method of CopyonWriteArraySet method adds all the element of the specified collection to this CopyOnWriteArraySet which are not present in it. This methods results the union of the two collections. Syntax: public boolean addAll(Collection<E> c) Parameters: This method accepts a pa
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences