Sun Oct 21, 2012 1:10 pm
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
String stringsArr[] = {"adam", "tom", "ali", "mina", "saly"};
List arraylist = new ArrayList();
Set hashSet = new HashSet();
for (String str : stringsArr) {
arraylist.add(str);
hashSet.add(str);
}
Iterator listIterator = arraylist.iterator();
Iterator setIterator = hashSet.iterator();
System.out.println("Iterations for array list");
while (listIterator.hasNext()) {
System.out.print(listIterator.next() + "\t");
}
/*
* Notice that the output of set object is not in order.
*
*/
System.out.println("\nIterations for hash set.");
while (setIterator.hasNext()) {
System.out.print(setIterator.next() + "\t");
}
}
}
Iterations for array list.
adam tom ali mina saly
Iterations for hash set.
saly ali adam mina tom
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.