Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:33:57+00:00 2026-06-13T11:33:57+00:00

Hi I am having trouble understanding why the output is ‘ex [6, 7, b,

  • 0

Hi I am having trouble understanding why the output is ‘ex [6, 7, b, d] [6, 7, b]’ for
this piece of code. Please can someone advise how the subset is working with the
numbers and letters? thanks

import java.util.*;
public class Corner {
  public static void main(String[] args) {
    TreeSet<String> t1 = new TreeSet<String>();
    TreeSet<String> t2 = new TreeSet<String>();
    t1.add("b"); t1.add("7");
    t2 = (TreeSet)t1.subSet("5", "c");

     try {
     t1.add("d");
     t2.add("6");
     t2.add("3");
     }
     catch (Exception e) { System.out.print("ex "); }
     System.out.println(t1 + " " + t2);
     } }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:33:58+00:00Added an answer on June 13, 2026 at 11:33 am

    I have made a few changes to clean up your code (usage of raw generic type and unchecked conversion) and to make the log output somewhat more insightful:

    import java.util.SortedSet;
    import java.util.TreeSet;
    
    public class Corner {
        public static void main(String[] args) {
            SortedSet<String> t1 = new TreeSet<String>();
            SortedSet<String> t2 = new TreeSet<String>();
            t1.add("b");
            t1.add("7");
            System.out.println(t1 + " " + t2);
            t2 = t1.subSet("5", "c");
            System.out.println(t1 + " " + t2);
            t1.add("d");
            System.out.println(t1 + " " + t2);
            t2.add("6");
            System.out.println(t1 + " " + t2);
            try {
                t2.add("3");
            } catch (Exception e) {
                e.printStackTrace(System.out);
            }
            System.out.println(t1 + " " + t2);
        }
    }
    

    The output looks like this:

    [7, b] []
    [7, b] [7, b]
    [7, b, d] [7, b]
    [6, 7, b, d] [6, 7, b]
    java.lang.IllegalArgumentException: key out of range
        at java.util.TreeMap$NavigableSubMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at Corner.main(Corner.java:18)
    [6, 7, b, d] [6, 7, b]
    

    Okay, after insertion of “b” and “7”, t1 contains those elements in ASCII (or Unicode) sort order. t2 is empty. No surprise here.

    After the subSet call, both sets have identical content because the given range “5” to “c” spans the whole current range of the original set from “7” to “b”. No surprise either.

    Please note: The subset is backed by the original set as described by the API JavaDoc.

    Furthermore the API description says that the returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range. This will be important later on.

    Okay, next you add element “d” to t1 which is shown in t1, but not in t2 because “d” is outside the range “5” to “c” of t2.

    Now you add element “6” to t2 (which is still backed by t1!). It is in the correct range of t2 and thus successfully added to both logical sets.

    Now you run into trouble because you try to add “3” to t2, which is out of range “5” to “c”, thus causing the IllegalArgumentException which can be seen in the log output. The element is not inserted into t2 (and thus not into t1 either). Consequently it is not shown in the last line of log output.

    Bottom line: Your program behaves just as expected according to the JDK documentation. :-)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble understanding this output for my code: #define MAX 5
Below is come code that I am having trouble understanding. The output is 13
I am beginner in Ruby and having trouble understanding this code require_relative 'custom_page' module
I am having trouble understanding this code: static long long _be_decode_int(const char **data, long
I'm having trouble understanding this code... I was expecting something similar to threading where
I have a 64-tap FIR filter whose output format I am having trouble understanding.
Im having trouble understanding this : why Microsoft put Xattribute under Xobject ? they
I'm having trouble understanding the dynamic keyword (used .net 2 up til recently). Can
Im working on a CS assignment and Im having a little trouble understanding how
I'm having trouble understanding why my code will not work asynchronously. When running asynchronously

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.