Iterator Java Sort


 

Iterator Java Sort

In this section of tutorial we will learn how to use the static sort() method .We will create an example and sort the ArrayList.Then display the contents of the sorted ArrayList.

In this section of tutorial we will learn how to use the static sort() method .We will create an example and sort the ArrayList.Then display the contents of the sorted ArrayList.

  • Sort is a static method of the Collections class.
  • It sorts the contents of the list Collection.


Example of Java Sort Iterator
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class sort {

	public static void main(String[] args) {
		List list = new ArrayList();
		String country[] = { "India", "Japan", "USA", "UK", "Nepal" };
		for (int i = 0; i < 5; i++) {
			list.add(country[i]);
		}
		Collections.sort(list);
		Iterator i = list.iterator();

		while (i.hasNext()) {
			System.out.print(i.next() + "\t");
		}
	}
}

Output

India Japan Nepal UK USA

Ads