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 6535511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:22:55+00:00 2026-05-25T10:22:55+00:00

I’m writing a contact book application in Java using the swing and awt libraries.

  • 0

I’m writing a contact book application in Java using the swing and awt libraries. The Application consists of a JList which uses a TreeSet as an abstractListModel.

The TreeSet is for a class called Contact, which has private comparator class that sorts contacts based on their first name. the private boolean equals(Object o) method returns true if the Contact has the same mobileNumber as that of O (after casting, of course).

I want to add a search functionality into this application. I’ve made a search JTextField and added a keyListener and what I want to do is that after each key is pressed, the list displays a narrow down set of results which contains the search terms. Is there a method for this in TreeSet or any other Collection? I want it to be similar to what you have in the Music Application in iPods, where when you type the letter ‘f’, for example, it lists all the songs that contain the letter F but it’s only when you type ‘fifty cent’ that the songs by the singer you want appear.

Thanks for your help.

  • 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-05-25T10:22:56+00:00Added an answer on May 25, 2026 at 10:22 am

    If you want to find all entries that start with the text (e.g. “f”), you can use the subSet(from, to) method, like this:

    SortedSet<String> s = new TreeSet<String>(new Comparator<String>() {
      public int compare( String s1, String s2 ) {
        return s1.compareToIgnoreCase( s2 );
      }
    
    });
    
    
    s.add( "Erich" );
    s.add( "Erica" );
    s.add( "Erin" );
    s.add( "Dave" );
    s.add( "Thomas" );
    
    SortedSet<String> result = s.subSet( "e", "e" + Character.MAX_VALUE ); //"e" represents the user input
    System.out.println(result);//prints [Erica, Erich, Erin]
    
    result = s.subSet( "Eric", "Eric" + Character.MAX_VALUE );
    System.out.println(result); //prints [Erica, Erich]
    
    result = s.subSet( "Erich", "Erich" + Character.MAX_VALUE );
    System.out.println(result); //prints [Erich]
    

    Since the toparameter to subset(from, to) is exclusive, you need something that will clearly be greater. In my example I simply added Character.MAX_VALUE but you might want to get some better upper bound. Note that this depends on your comparator, e.g. how it handles case differences etc.

    If you want to filter using wildcards, like all texts containing the text (e.g. f would translate to *f*), you’d have to iterate over and check all the entries anyways. In that case you don’t get any advantage using a sorted set.

    Edit: updated the example to your data (adding me as well 🙂 ).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.