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 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: "TreeSet" example in JRuby
Post your Comment