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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:14:34+00:00 2026-06-07T04:14:34+00:00

I am teaching myself Java and I have a simple package with three classes

  • 0

I am teaching myself Java and I have a simple package with three classes – shop, product and shelf. A shop object contains many shelves, and a shelf contains many products, and, in this case, each product is only available on one shelf.

A product looks like this:

public class t_product {
    private t_shelf shelf;
    private String name;
}

And a shelf looks like this:

public class t_shelf {    
    private Set<t_product> products = new HashSet<>();
    private String name;
}

The shop object looks like this:

public class t_shop {    
    private Set<t_shelf> shelves = new HashSet<>();
}

I also have a set of functions which will add or remove a product from the shelf.

myshelf.addProduct(myproduct);

will set myproduct.shelf = myshelf, and add myproduct to myshelf.products. This works fine, and handles the relationship nicely. Similar functions link shop and shelves.

Moving on to the problem

I have a .csv which stores:

Product   |   Shelf
----------------------
Hats      |   Headwear
Helmets   |   Headwear
Socks     |   Footwear
Apples    |   Fruit
Bananas   |   Fruit
Oranges   |   Fruit

When parsing the .csv, I want to search for the shelf by name to see if it has already been created, so, for example, on reading a line “Bananas, Fruit”, it would process:

if (!myshop.getShelfByName("Fruit")){
       myshop.addShelf(new t_shelf("Fruit"));
   }
myshop.getShelfByName("Fruit").addProduct("Bananas"); //Constructors accept the name as a parameter.

My question is:

Is there a neater implementation of getShelfByName(String name) than simply iterating through the HashSet and checking for the name against every single item? (Want to avoid O(N) algorithms).

Thanks!

Any attempt to solve this VERY gratefully received 🙂

  • 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-07T04:14:35+00:00Added an answer on June 7, 2026 at 4:14 am

    If you’re creating classes of objects to be held in a HashSet, you must give these classes decent equals() and hashCode() override methods, ones that make sense and that play well together (that for one use the same invariant fields to determine their results).

    As for your specific question, consider putting things in HashMaps rather than HashSets, as then you can easily find the object by its key.

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

Sidebar

Related Questions

Here is my code, pretty simple really. Its not homework, I'm teaching myself java
I'm extremely new to Java, and have mostly just been teaching myself as I
I am newbie in java but I think I have done well teaching myself
Im teaching myself java and the book i'm looking at just got around to
I have recently started teaching myself C# and Asp.net. I am trying to build
I am teaching myself to use JavaCC in a hobby project, and have a
Im still teaching myself Java so I wanted to try to read a text
i am teaching myself java and i work through the exercises in Thinking in
I am teaching myself to write classes in C++ but can't seem to get
I have started teaching myself about the Android NDK and I have followed this

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.