12:06 am - Saturday June 24, 2017

Basic Treeset Operations Methods

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how  to do basic Treeset operations.

basic operations on TreeSet like creating object, adding elements to it,

verifies elements existence, deleting all elements at once,

size of the set and deleting a specific element.

Example

import java.util.TreeSet;

public class MyBasicTreeset

{

   public static void main(String[] args)throws Exception

   {

       TreeSet<String> ts = new TreeSet<String>();

         ts.add("one");

         ts.add("two");

         ts.add("three");

         System.out.println("Elements: "+ts);           

         System.out.println("Is set empty: "+ts.isEmpty());            

         ts.clear();

         System.out.println("Is set empty: "+ts.isEmpty());

         ts.add("one");

         ts.add("two");

         ts.add("three");

         System.out.println("Size of the set: "+ts.size());            

         ts.remove("two");

         System.out.println("Elements: "+ts);

    }

}

Output

            Elements: [one, three, two]

           Is set empty: false

           Is set empty: true

           Size of the set: 3

           Elements: [one, three]

BACK

Filed in: Java Examples

Comments are closed.