Example of show the following
  • HashSet, Set
  • List, List.add
  • ListIterator
  • Conversion of List to Set, with Set set2 = new HashSet(list);
Example code

import java.io.*;
import java.util.List;
import java.util.*;
import java.util.ArrayList;
public class Test_This {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
Process p1 = new Process();
p1.method_1();
}
}
class Process
{
String[] first = {"Java", "C#","XML","JSP"};

public Process()
{
System.out.println("Default) constructor");
}
//Process processor()
public void method_1()
{
//List list = Arrays.asList(first);
List list = new ArrayList();
Set set2 = new HashSet(list);

System.out.println("for loop to add values to List");
for(int i = 0; i < first.length; i++)
{
list.add(first[i]);
System.out.println(list + "\t");
Set set = new HashSet(list);
//System.out.println(set);
set2 = set;
}

System.out.println("--Print out with set");
System.out.println(set2);

System.out.println("--Using ListIterator and While loop");
ListIterator iter = list.listIterator();
while(iter.hasNext())
{
System.out.print("\t" + iter.next());
}

}
}
 

Since, System.out.println(list + "\t"); is within the for loop, the loop prints out the array list member in sequence 1,; 1,2; 1,2,3; 1,2,3,4. If you put this line out side of the for loop the out put will be in linear like this " [Java, C#, XML, JSP]". Link