import java.util.Set;
public class ArrayToSet {
public static void main(String[] args) {
String[] strArr = { "sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday" };
// To convert an array into a Set first we convert it to a List.
// we create a HashSet and pass the list as the constuctor
List<String> strList = Arrays.asList(strArr);
System.out.println("list objects are:" + strList);
Set<String> strSet = new HashSet<String>(strList);
System.out.println("set objects are:" + strSet);
System.out.println("iterating the set");
Iterator<String> itr = strSet.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
OUTPUT
list objects are:[sunday, monday, tuesday, wednesday, thursday, friday, saturday]
set objects are:[wednesday, thursday, monday, sunday, saturday, friday, tuesday]
iterating the set
wednesday
thursday
monday
sunday
saturday
friday
tuesday
set objects are:[wednesday, thursday, monday, sunday, saturday, friday, tuesday]
iterating the set
wednesday
thursday
monday
sunday
saturday
friday
tuesday
No comments:
Post a Comment
Note: only a member of this blog may post a comment.