How to Use TreeMap in Java – Selenium and Java Challenge 12

I have added a sample program below that demonstrates the use of TreeMap with Selenium and Java. A TreeMap is a collection that stores key–value pairs and automatically sorts the keys in natural ascending order. Review the program, identify its output, and share your comments or observations below. Click the “Click for solution” button to view the explanation of the output.

package tests;

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

public class SampleProgram12{


@Test
public void testMySkills() {
    System.out.println("Output is"+fetchValue());
}
public int fetchValue() {
        try {
            TreeMap<Integer,Integer> map = new TreeMap<>();
            map.put(3,5);
            map.put(4,5);
            map.put(2,3);

            return map.firstEntry().getValue(); 
        } 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