Usage of tree set in Java – Selenium and Java Challenge 11

I have given program below with the usage of TreeSet using Selenium and Java. We can use TreeSet to sort the values in sequential order. You can review the execution output generated by the Selenium script and share your comments. Click the “Click for solution” button to view the explanation of the output.

package tests;

import java.util.List;

import java.util.TreeSet;

import java.util.ArrayList;
import org.testng.annotations.Test

public class SampleProgram11{


@Test
public void testMySkills() {
    System.out.println("Output is"+fetchValue());
}
public int fetchValue() {
        try {
            List<Integer> list = new ArrayList<>();
            list.add(4);
            list.add(3);
            list.add(7);

            TreeSet<Integer> treeSet = new TreeSet<>();

            for (int val : list) {
                treeSet.add(val);
            }

            return treeSet.first(); // smallest element
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return -1; // default/fallback value
        }
    }

}

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , Smart Fitness Guide , CodesToolbox , Testing Forum
Scroll to Top
0
Would love your thoughts, please comment.x
()
x