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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:33:33+00:00 2026-06-14T03:33:33+00:00

I have a hashmap, I wish to get an array of the keys in

  • 0

I have a hashmap, I wish to get an array of the keys in this hashmap, but I wish for the array to be sorted.

For instance if the map looks like this:

<"2",obj1>
<"4",obj2>
<"6",obj3>
<"10",obj4>
<"5",obj5>
<"1",obj6>
<"15",obj7>
<"3",obj8>

I want the array to be: [“1”, “2”, “3”, “4”, “5”, “6”, “10”, “15”]

The faster the better. Is there a built in way to do this?

Update 1

When i am using TreeMap, keys are sorted in following order:

“1”, “10”, “2” and so on.

but i want them to be like: “1”,”2″,”3″.. “10”,”15″.

  • 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-14T03:33:35+00:00Added an answer on June 14, 2026 at 3:33 am

    I have some keys like “10A”, “2AB”

    In that case you need a custom comparator like

    public static void main(String... args) {
        NavigableMap<String, Object> map = new TreeMap<String, Object>(new Comparator<String>() {
            final Pattern pattern = Pattern.compile("(\\d+)(.*)");
    
            @Override
            public int compare(String o1, String o2) {
                Matcher matcher1 = pattern.matcher(o1);
                Matcher matcher2 = pattern.matcher(o2);
                matcher1.find();
                matcher2.find();
                int cmp = Long.compare(Long.parseLong(matcher1.group(1)), Long.parseLong(matcher2.group(1)));
                if (cmp != 0)
                    return cmp;
                return matcher1.group(2).compareTo(matcher2.group(2));
            }
        });
    
        map.put("1", "one");
        map.put("1A", "oneA");
        map.put("10", "ten");
        map.put("10AB", "tenAB");
        map.put("15", "fifteen");
        map.put("2", "two");
        map.put("2AB", "twoAB");
        map.put("2", "three");
    
        System.out.println(map.keySet());
    }
    

    prints

    [1, 1A, 2, 2AB, 10, 10AB, 15]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a HashMap that looks like this: HashMap<String, MyObject> where the String
I have a HashMap which looks like : ( student1 => Map( name =>
I have a HashMap of JButtons which represents pictures. this.nahledy.put(s.getName(), new JButton()); this.nahledy.get(s.getName()).addActionListener(); but
I have a HashMap<String, Object> that looks like this when I call .toString() on
I have a hashmap, I wish to get an array of the values in
I have hashmap that has keys like this:'2+4+5' , '653+65+1324+75'.(integer values delimited by a
I have a hashmap that is 101 keys in size, but I know for
I have hashmap and its keys are like folder/1.txt,folder/2.txt,folder/3.txt and value has these text
I have a HashMap with various keys and values, how can I get one
I have a HashMap<String,String> and there is a static method which returns this map

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.