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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:19:54+00:00 2026-06-10T16:19:54+00:00

I have a HashMap to which I am adding integers from an array. The

  • 0

I have a HashMap to which I am adding integers from an array. The key is the integer and the value is the number of duplicates of the integer. When I iterate through the HashMap to get the keys in the HashMap they seem to be in random order. Is there any particular order in which these keys are added ? This is how I am iterating through.

       Set<String> a = names.keySet();
        Iterator i = a.iterator();

        while(i.hasNext())
            String t = (String) i.next();
  • 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-10T16:19:56+00:00Added an answer on June 10, 2026 at 4:19 pm

    Indeed, HashMap returns entries in a nondeterministic (unpredictable/unreliable) order. If you want entries to appear in order by key (so, 2 before 3), use a TreeMap (javadoc). If you want entries to appear in order by initial insertion, use a LinkedHashMap (javadoc).


    Edited to add: You say that “the key is the integer”, but then you use String everywhere. Note that the string "123" sorts before the string "23", just as "abc" would sort before "bc". If you want your keys treated as numbers, e.g. by being sorted in numeric order, then you should be using a numeric type, such as Integer, rather than String. (It’s technically possible to give your TreeMap a Comparator<String> that compares string-valued keys by examining their contents and doing an integer comparison, but I don’t think you want to do that.)

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

Sidebar

Related Questions

I have a List of HashMap 's which has key of type Integer and
I have a strange problem when adding a value to a String array which
If I have the value foo , and a HashMap<String> ftw for which ftw.containsValue(foo)
I have a HashMap where the key is a word and the value is
I have a hashmap with some information(key and value) in a perl file. I
I have a HashMap which I want to convert into a array. Which the
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 which has X number of elements I need to move
I have an application which has to process XML files by adding metadata from
i want to clone a HashMap which have a Object as value. How does

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.