Home Tutorial Java Iterator Iterator Java Sort

 
 

Share on Google+Share on Google+
Iterator Java Sort
Posted on: November 4, 2009 at 12:00 AM
Advertisement
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
Advertisement

Related Tags for Iterator Java Sort:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 4, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com