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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:43:35+00:00 2026-06-03T01:43:35+00:00

I have to devise a function that will take as input a keyword and

  • 0

I have to devise a function that will take as input a keyword and will output a category id.
Ex:

f('dog') returns _ANIMAL
f('chair') returns _FURNITURE

I already have the mapping and I could just iterate over the tag array each time, but I have a feeling this is not the best solution.

Is there a special data structure (I’m thinking of ternary search trees) in the Java libraries for this specific task? Should I just use HashMap (or maybe Set (since there are few categories))?

P.S. This mapping is fixed, I do not need to add or remove elements from it once it is built.

  • 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-03T01:43:36+00:00Added an answer on June 3, 2026 at 1:43 am

    If I understand you correctly, then HashMap sounds like exactly what you want. You wouldn’t want to iterate through an entire array each time, because with many function calls and/or a large array your program would wind up running slowly. With a HashMap, pulling a value (your category) from a key (your keyword) happens more or less immediately, in constant time.

    You can build the map like this:

    HashMap map = new HashMap();
    map.put("dog", "animal");
    map.put("chair", "furniture");
    map.put("cat", "animal");
    

    And then map.get("dog") returns “animal”, map.get("chair") returns “furniture”.

    As others have indicated, enums would work well (and a tiny bit faster) for this too—with the caveat that they’re fixed at compile time and thus cannot be changed during execution.

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

Sidebar

Related Questions

I have a sample Rails 3.1.1 application that I have set devise up to
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in
Hi I created a function that takes in an accepted sockFD as input and
I have a function that redirects to a countdown while our system is offline,
I have a Rails 3 app that I am trying to implement devise and
I have a Rails 3 app that I am trying to implement devise and
I'm trying to me a function that automatically returns the screen resolution on the
I'm trying to create a User show page (that will function as a profile
I have the following recursive function that is used to search down a hierarchical
I have written a CUDA function that calculates a convex envelop in a set

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.