import java.util.HashSet;
import java.util.Set;
public class HashSetToArray {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
String [] arr = new String[hs.size()];
/*
* Returns an array containing all of the
* elements in hs set
*/
hs.toArray(arr);
System.out.println("Array is ");
for (String str : arr) {
System.out.println("Element is = "+str);
}
}
}
Output:
hashset = [javascript, php, html, java, mysql]
Array is
Element is = javascript
Element is = php
Element is = html
Element is = java
Element is = mysql
import java.util.Set;
public class HashSetToArray {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
String [] arr = new String[hs.size()];
/*
* Returns an array containing all of the
* elements in hs set
*/
hs.toArray(arr);
System.out.println("Array is ");
for (String str : arr) {
System.out.println("Element is = "+str);
}
}
}
Output:
hashset = [javascript, php, html, java, mysql]
Array is
Element is = javascript
Element is = php
Element is = html
Element is = java
Element is = mysql
No comments:
Post a Comment