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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:40:42+00:00 2026-05-20T18:40:42+00:00

To give a simple example, consider a Place class: public class Place { //fields

  • 0

To give a simple example, consider a Place class:

public class Place {

    //fields
    private String name;

    private String state;

    private int population;

    private int squareMileage;

    private int elevation;

    //constructors
    public Place() {    
    }

    public Place(String name, String state) {
        this.name = name;
        this.state = state;
    }

    public Place(String name, String state, int population, int squareMileage, 
                    int elevation) {
        this.name = name;
        this.state = state;
        this.population = population;
        this.squareMileage = squareMileage;
        this.elevation = elevation;
    }

    //getters and setters
    public String getName() {
        return this.name;
    }

    public String getState() {
        return this.state;
    }
    //... (other getters and setters omitted)

    //do stuff
}

And a Places class (a HashMap of Place objects):

import java.util.*;

public class Places {
    private Map searchablePlaces;

    public Places() {
        searchablePlaces = new HashMap();
    }

    public void add(Place value) {
        Place key = new Place(value.getName(), value.getState());
        searchablePlaces.put(key, value);
    }

    public Place find(Place key) {
        return searchablePlaces.get(key);
    }

    //override hashCode, equals

    //do stuff
}

Essentially, my question is:

  1. Would there be any efficiency gained in searching the HashMap for key, as opposed to searching for value directly in, let’s say, a sorted ArrayList?
  2. What if key was of type String equal to name + state (or of type String[2])?
  • 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-20T18:40:43+00:00Added an answer on May 20, 2026 at 6:40 pm

    It’s confusing, IMHO. I would define a PlaceKey class, holding just a name and a state and defining hashCode and equals. A place would hold a PlaceKey, and other attributes. And I would use a Map<PlaceKey, Place>.

    Note that, in your example, it’s the Place class that needs equals and hashCode, not the Places class.

    Now, to answer your 2 questions :

    1. a hash map is O(1), and a list is O(n). A map will thus usually be faster (except perhaps for very small n values)
    2. concatenating is almost always a bad idea. You could have name1 = aa, state1 = a, name2 = a and state2 = aa, which would result in a collision. An array doesn’t override equals and hashCode in terms of their content, and is not an appropriate map key class.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this simple xml element example: <parent foo=1 bar=2 foobar=3> <child/> </parent> In the
Possible Duplicate: Destructors for C++ Interface-like classes Consider a simple example of a C++
I'll give a very simple example: at the moment I have to write like
Can you please give me a simple example of program that uses threading? I
What is an anonymous function in PHP? Could you give me a simple example,
For instance, like Font. Can anyone give a very simple example? Maybe just a
Can someone give a simple example of updating a textfield every second or so?
can someone give me simple example in c, of using pipe() system call to
Can you give me any simple example? For example I took an error when
Can someone give me a simple example on how to user user-settings instead of

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.