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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:34:04+00:00 2026-05-17T18:34:04+00:00

Someone told me hashmaps are rather slow. So I am just wondering whether to

  • 0

Someone told me hashmaps are rather slow. So I am just wondering whether to use hashmap or a switch case logic.

My requirement is this. I have a set of CountryNames and CountryCodes. My ListView displays the names of the countries. When an country name item is clicked, I must Toast the CountryCode.

In such a scenario, should I maintain a HashMap of CountryNames and Codes and access this to get the corresponding Code?:

myMap.put("US", 355);
myMap.put("UK", 459);
//etc

Or is it better to write a switch case like so

switch (vCountryNamePos):
{
case 0:   //US
vCountryCode = 355;
break;
case 1:   //UK
vCountryCode = 459;
break;

//etc
}

Which is faster? If not Hashmaps, then in what practical scenarios would a Map be used?

-Kiki

  • 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-17T18:34:05+00:00Added an answer on May 17, 2026 at 6:34 pm

    For two values, a switch will be faster. A hashmap will always at least check for equality of your key, so it can’t beat one or two .equals() tests.
    For many values, a hash will be faster. A switch has to test every value until it finds the right one.

    For a small number of values (say up to 10 or so), prefer a switch. It’s gonna be lighter and faster.
    For a big number of values (in upwards of 50), prefer a hash. A hash will not have to check all values, so it will be faster than a switch when the number of values increases.
    For 10~50 values, I’d suggest you do what you feel is more readable because the performance is gonna be similar.

    Now if you are looking into extreme performance on static strings known at compile time, you may look into code-generating tools like gnuperf.
    If you don’t know your strings at compile time but you know they are gonna be decently short and decently uniform in length, or with common prefixes, you are probably gonna be fastest with a Trie data structure.
    If you want to keep performance on great number of very heterogeneous strings, or on objects that may not be Strings, then HashMap is the way to go. It’s pretty much unbeatable when the number of objects is very high (in the billions or more).

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

Sidebar

Related Questions

Someone told me to do this in order to keep track of latest people
well I'm new to the facebook api stuff, and just recently someone told me
Someone told me this bit of code prints 29. Why is that? int *a
Someone told me that I need to use a delimiter in my trigger. I'm
I was about to use Firebird for a project, when someone told that DB2
Someone told me that when you use setTimeout you must clear it with clearTimeout
Someone told me that openGL is for graphic only, and that it's very bad
Someone told me once there was a good novel around the story of the
Someone on IRC told me I could create objective c objects directly from plist
Someone told me if I set the ValidateConnection property in Oracle to TRUE, the

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.