Tuesday 2 June 2015

How to convert array to LinkedHashSet

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

public class ArrayToLinkedHashSet {
   
    public static void main(String[] args) {
       
        String [] arr ={"one","two","third","four","five"};

        // make linkedhashset by arr (array of string)
       
        Set<String> lhs = new LinkedHashSet<String>(Arrays.asList(arr));
       
        // print linkedhashset
       
        for (String str : lhs) {
            System.out.println(" Element of lhs is="+str);
        }
       
    }

}

Output:

 Element of lhs is=one

 Element of lhs is=two

 Element of lhs is=third

 Element of lhs is=four

 Element of lhs is=five

No comments:

Post a Comment