Java TreeSet tutorial and example


The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used


Example

package com.candidjava.core;

import java.util.TreeSet;

public class TreeSetExample {

	public static void main(String[] args) {
		TreeSet<String> ts = new TreeSet<String>();
		ts.add("hai");
		ts.add("123");
		ts.add("mathan");
		ts.add("lts");
		ts.add("mathan");
		ts.add("lts");
		ts.add("ramya");
		ts.add("suji");
		ts.add("ravathi");
		ts.add("sri");

		System.out.println("TreeSet ..  " + ts);
		System.out.println("size ... " + ts.size());
		System.out
				.println("Output will be in ascending order and dupliate will be removed");

	}

}
Output:
TreeSet ..  [123, hai, lts, mathan, ramya, ravathi, sri, suji]
size ... 8
Output will be in ascending order and dupliate will be removed

Click here for more TreeSet Example


Comments


©candidjava.com