Master java skills

TreeSet in Java

TreeSet class maintains natural sorting order of elements. Meaning if TreeSet contains integer values, then least value will be the first element in the TreeSet, and the highest value will become the last.

TreeSet example

package com.javatrainingschool;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetExample {
	
	public static void main(String[] args) {
		
		Set<Integer> set = new TreeSet<Integer>();
		
		set.add(500);
		set.add(100);
		set.add(700);
		set.add(400);
		set.add(900);
		set.add(900);
		
		for(int value : set) {
			System.out.println("Value : " + value);
		}
	}
}
Output :
Value : 100
Value : 400
Value : 500
Value : 700
Value : 900