2:06 am - Saturday January 21, 2017

Copy all elements from Hashset to an array toArray()

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how to Copy all elements from Hashset to an array  using toArray() method.

Another way copy the elements using array concept.

 Copy the element up to the number of element reach.

Example

import java.util.HashSet;

public class CopyArray

{

    public static void main(String a[])

      {

            HashSet<String> hs = new HashSet<String>();

            hs.add("first");

            hs.add("second");

            hs.add("third");

            System.out.println("HashSet content: ");

            System.out.println(hs);

            String[] strArr = new String[hs.size()];

            hs.toArray(strArr);

            System.out.println("Copied array content:");

            for(String str:strArr){

           System.out.println(str);

        }
       }

Output

       Hash set values: [Learning, Simply, Easy]

       Clone Hash set values: [Learning, Simply, Easy]

BACK

Filed in: Java Examples

Comments are closed.