CodesJava

Easy learning with example program codes

TreeSet in java


TreeSet:

TreeSet extends AbstractSet and implements the NavigableSet interface. It maintains ascending order for its elements i.e. elements will be in sorted form.

 TreeSet example:

TreeSetTest.java

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
 
/**
 * This class is used to show the TreeSet functionality.
 * @author codesjava
 */
public class TreeSetTest {
	public static void main(String args[]){
		//Create TreeSet object.
		Set treeSet = new TreeSet();
 
		//Add objects to the HashSet.
		treeSet.add("Roxy");
		treeSet.add("Sunil");
		treeSet.add("Sandy");
		treeSet.add("Munish");
		treeSet.add("Pardeep");
 
		//Print the TreeSet object.
		System.out.println("TreeSet elements:");
		System.out.println(treeSet);
 
		//Print the TreeSet elements using iterator.
		Iterator iterator=treeSet.iterator();
		System.out.println("TreeSet elements using iterator:");
		while(iterator.hasNext()){  
		   System.out.println(iterator.next());  
		}  
	}
}

Output:

TreeSet elements:
[Munish, Pardeep, Roxy, Sandy, Sunil]
TreeSet elements using iterator:
Munish
Pardeep
Roxy
Sandy
Sunil

Download this example.
 
Next Topic: ArrayList in java with example.
Previous Topic: LinkedHashMap in java with example.



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki