- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy all elements in Java TreeSet to an Object Array
Create a TreeSet and add elements −
TreeSet<String> tSet = new TreeSet<String>(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet");
Now, let us copy it to object array −
Object[] arr = tSet.toArray(); for (Object ob: arr) { System.out.println(ob); }
The following is an example to copy all elements in TreeSet to an Object Array −
Example
import java.util.*; public class Demo { public static void main(String args[]){ TreeSet<String> tSet = new TreeSet<String>(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet"); System.out.println("TreeSet elements = "+tSet); Object[] arr = tSet.toArray(); for (Object ob: arr) { System.out.println(ob); } } }
Output
TreeSet elements = [Internet, Radio, TV] Internet Radio TV
Advertisements