1. Converting an array to List
2. Converting the resulting List to a Set
Following program constructs an array with strings, String array is converted into a List using the asList method available in Arrays. Then the resulting list is converted into a Set.
---
package com.blog.javaexposure.util;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Array2Set {
public static void main(String rags[]){
// Creating a string array
String[] str = {"One","Two","Three","Four","
// Convert array to List
List
System.out.println("Elements in the list are:");
//Checking the elements in the list
for(String s : list){
System.out.println(s);
}
// Convert list to set
Set
System.out.println("Elements in the set are:");
//Checking the elements in the set
for(String s : set){
System.out.println(s);
}
}
}
No comments:
Post a Comment