TreeSet in Java

import java.util.TreeSet;
 
public class myTreeSet {
   public static void main(String[] args) {
      TreeSet<String> animals = new TreeSet<String>();
      animals.add("Dog");
      animals.add("Cat");
      animals.add("Sparrow");
      animals.add("Iguana");
      animals.add("Dragon");
      animals.add("Peacock");
      System.out.println("Number of animals = " + animals.size());
      for (String name : animals) {
         System.out.println(name);
      }
      System.out.println("Animal before Sparrow: " + animals.lower("Sparrow"));
      System.out.println("Animal after Sparrow: " + animals.higher("Sparrow"));
      System.out.println("First element: " + animals.first());
      System.out.println("Last element: " + animals.last());
   }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.