1+ /*********************************************************************************
2+ * (setToList) Write the following method that returns an ArrayList from a set. *
3+ * *
4+ * public static <E> ArrayList<E> setToList(Set<E> s) *
5+ *********************************************************************************/
6+ import java .util .ArrayList ;
7+ import java .util .Set ;
8+
9+ public class Exercise_27_11 {
10+ public static void main (String []args ) {
11+ // Create a set
12+ Set <String >set =new java .util .HashSet <>();
13+ set .add ("Mark" );
14+ set .add ("Smith" );
15+ set .add ("Anderson" );
16+ set .add ("Sally" );
17+ System .out .println ("Elements in the set: " +set );
18+
19+ // Convert the set to an ArrayList
20+ ArrayList <String >list =setToList (set );
21+ System .out .println ("Elements in the ArrayList: " +list );
22+ System .out .println ("Elements at ArrayList index 2: " +list .get (2 ));
23+
24+ }
25+
26+ /** Return an ArrayList from a set */
27+ public static <E >ArrayList <E >setToList (Set <E >s ) {
28+ // Create an ArrayList
29+ ArrayList <E >list =new ArrayList <>();
30+
31+ // Add elements from set
32+ for (E e :s ) {
33+ list .add (e );
34+ }
35+
36+ return list ;
37+ }
38+ }