"TreeSet" example in JRuby

In the previous sections of JRuby tutorials we have learned about how to use Java classes in JRuby program now here is one more example that uses Java class TreeSet.

"TreeSet" example in JRuby

"TreeSet" example in JRuby

     

In the previous sections of JRuby tutorials we have learned about how to use Java classes in JRuby program now here is one more example that uses Java class TreeSet. 

In this example we have first included java.util.TreeSet class and then we have created a new instance of TreeSet class. To add elements in this TreeSet object we have used add method.

Here is the example code TreeSetExample.rb as follows:

TreeSetExample.rb

require "java"
include_class "java.util.TreeSet"
   set = TreeSet.new
   set.add "Amit"
   set.add "Kumar"
   set.add "Vineet"
   set.each do |v|
   puts "values #{v}"
   end

Output:

On executing JRuby program following output will be generated:

C:\JRuby>jruby TreeSetExample.rb
values Amit
values Kumar
values Vineet

Download Source Code