How to create TreeSet In Java

Explain the constructors of TreeSet class and write java program to create TreeSet collection In post. We will write java program to create treeset collection using different constructors of TreeSet. If you are new with TreeSet i will recommended to you first read about TreeSet Constructors here TreeSet In Java . Let’s See How to create TreeSet In Java

package com.javapro.treeset;

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

public class CreateTreeSet {

public static void main(String[] args) {

Set<String> ts = new TreeSet<String>();
ts.add("mumbai");
ts.add("delhi");
ts.add("kolkata");
ts.add("chandigarh");
ts.add("dehradun");

// print treeset
System.out.println(" ts ="+ts);

}

}

Output:

ts =[chandigarh, dehradun, delhi, kolkata, mumbai]

Leave a Reply

Your email address will not be published. Required fields are marked *

− 4 = 1