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

  • Home
  • SEARCH
  • 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 958735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:54:50+00:00 2026-05-16T00:54:50+00:00

I need an example on how to use a comparable class on a HashSet

  • 0

I need an example on how to use a comparable class on a HashSet to get an ascending order. Let’s say I have a HashSet like this one:

HashSet<String> hs = new HashSet<String>();

How can I get hs to be in ascending order?

  • 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-16T00:54:51+00:00Added an answer on May 16, 2026 at 12:54 am

    Use a TreeSet instead. It has a constructor taking a Comparator. It will automatically sort the Set.

    If you want to convert a HashSet to a TreeSet, then do so:

    Set<YourObject> hashSet = getItSomehow();
    Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator());
    treeSet.addAll(hashSet);
    // Now it's sorted based on the logic as implemented in YourComparator.
    

    If the items you have itself already implements Comparable and its default ordering order is already what you want, then you basically don’t need to supply a Comparator. You could then construct the TreeSet directly based on the HashSet. E.g.

    Set<String> hashSet = getItSomehow();
    Set<String> treeSet = new TreeSet<String>(hashSet);
    // Now it's sorted based on the logic as implemented in String#compareTo().
    

    See also:

    • Object ordering tutorial
    • Collections tutorial – Set Implementations
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know how to use a Dialog class in J2me. Any example.
How to use linsched (Linux scheduler simulator)? In need of an example on how
I need to explain myself why I do not use static methods/propertis. For example,
why i need to use the const function in the less traits? for example,
i need an example of the shortest path of directed graph cycle bye one
If I have a class Person that implements Comparable (compares personA.height to personB.height ,
I want to define one protocol with few properties and need to use those
My application makes multiple web calls in order to get authentication. I need to
I have been struggling to get a simple DynamicObject example working in .NET 3.5.
I need a basic example of how to use the IComparable interface so that

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.