The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java program to Copy all elements of Java HashSet to an Object Array

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java program to Copy all elements of Java HashSet to an Object Array Posted: Mar 14, 2016 8:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java program to Copy all elements of Java HashSet to an Object Array
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.Basic java example program to Copy all elements of Java HashSet to an Object Array
  • Object[] toArray()   This method is used To copy all elements of java HashSet object into array use

  1. package com.copyelementhashset;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5.  
  6. public class HashsetExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10. //create object of HashSet
  11.  HashSet<Integer> hashSet = new HashSet();
  12.        
  13.  //add elements to HashSet object
  14.  hashSet.add(1);
  15.  hashSet.add(2);
  16.  hashSet.add(3);
  17.  hashSet.add(4);
  18.  hashSet.add(5);
  19.      
  20. Object[] objArray = hashSet.toArray();
  21.  
  22. //display contents of Object array
  23. System.out.println("HashSet elements are copied into an Array. Now Array Contains..");
  24.  
  25.  for(int index=0; index < objArray.length ; index++)
  26.   System.out.println(objArray[index]);

  27.  
  28. System.out.println("Hashset contains");
  29.  
  30. Iterator it=hashSet.iterator();
  31.              
  32. while(it.hasNext()){
  33. System.out.println(it.next());
  34.                      
  35. }   
  36.  
  37. }
  38.  
  39. }
     



Output:

  1. HashSet elements are copied into an Array. Now Array Contains..
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. Hashset contains
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5

Read: Java program to Copy all elements of Java HashSet to an Object Array

Topic: DevOps Interview Questions Previous Topic   Next Topic Topic: Simple Way to Import all Missing Packages at Once : Eclipse shortcut

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use