Home Tutorial Java Core Hashset Example of toArray() method of hashset in java.

 
 

Example of toArray() method of hashset in java.
Posted on: April 4, 2011 at 12:00 AM
Advertisement
Example of toArray() method of hashset in java.

Example of toArray() method of hashset in java.

The toArray() is a method of HashSet in java. It is used to create array of object. It is also used to copy all element of hashset into array of object.

Code

HashSetToArray .java

package net.roseindia;

import java.util.HashSet;

public class HashSetToArray {

public static void main(String[] arg) {

HashSet obHashSet = new HashSet();

obHashSet.add(new Integer(1));

obHashSet.add(new Integer(2));

obHashSet.add(new Integer(3));

obHashSet.add(new Integer(4));

System.out.println("All Elements in HashSet : " + obHashSet);

/* Copy to array of object... */

Object[] num = obHashSet.toArray();

/* All elements of array... */

for (int i = 0; i < num.length; i++) {

System.out.println("Array element : " + num[i]); } }

}

Output :

All Elements in HashSet : [1, 2, 3, 4]

Array element : 1

Array element : 2

Array element : 3

Array element : 4

Advertisement

Related Tags for Example of toArray() method of hashset in java.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 4, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com