020-002-013

Sorted Set with TreeSet

Medium

Problem Description

Sorted Set with TreeSet

In this problem: You will create a program using TreeSet from the collections framework to automatically sort elements and use navigation methods like first, last, and subsets.

Learning Objective: Understand TreeSet's automatic sorting behavior and navigation methods such as first/last/subSet

Overview

TreeSet is a Set implementation that automatically sorts elements in natural order (ascending for numbers, alphabetical for strings). In addition to duplicate elimination, it provides navigation methods like first(), last(), and headSet().

Specifications

  • Create a TreeSet and add integers
  • Print all elements in sorted order
  • Print minimum using first() and maximum using last()
  • Print elements less than 50 using headSet(50)

Output Format

Sorted: [10, 25, 30, 50, 75, 90]
First: 10
Last: 90
Less than 50: [10, 25, 30]

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up