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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:28:28+00:00 2026-05-17T16:28:28+00:00

I need to implement an n:m relation in Java. The use case is a

  • 0

I need to implement an n:m relation in Java.
The use case is a catalog.

  • a product can be in multiple categories
  • a category can hold multiple products

My current solution is to have a mapping class that has two hashmaps.

  • The key of the first hashmap is the product id and the value is a list of category ids
  • The key to the second hashmap is the category id and the value is a list of product ids

This is totally redundant an I need a setting class that always takes care that the data is stored/deleted in both hashmaps.

But this is the only way I found to make the following performant in O(1):

  • what products holds a category?
  • what categories is a product in?

I want to avoid full array scans or something like that in every way.

But there must be another, more elegant solution where I don’t need to index the data twice.

Please en-light me. I have only plain Java, no database or SQLite or something available. I also don’t really want to implement a btree structure if possible.

  • 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-17T16:28:28+00:00Added an answer on May 17, 2026 at 4:28 pm

    If you associate Categories with Products via a member collection, and vica versa, then you can accomplish the same thing:

    public class Product {
         private Set<Category> categories = new HashSet<Category>();
         //implement hashCode and equals, potentially by id for extra performance
    }
    
    public class Category {
         private Set<Product> contents = new HashSet<Product>();
         //implement hashCode and equals, potentially by id for extra performance
    }
    

    The only difficult part is populating such a structure, where some intermediate maps might be needed.

    But the approach of using auxiliary hashmaps/trees for indexing is not a bad one. After all, most indices placed on databases for example are auxiliary data structures: they coexist with the table of rows; the rows aren’t necessarily organized in the structure of the index itself.

    Using an external structure like this empowers you to keep optimizations and data separate from each other; that’s not a bad thing. Especially if tomorrow you want to add O(1) look-ups for Products given a Vendor, e.g.

    Edit: By the way, it looks like what you want is an implementation of a Multimap optimized to do reverse lookups in O(1) as well. I don’t think Guava has something to do that, but you could implement the Multimap interface so at least you don’t have to deal with maintaining the HashMaps separately. Actually it’s more like a BiMap that is also a Multimap which is contradictory given their definitions. I agree with MStodd that you probably want to roll your own layer of abstraction to encapsulate the two maps.

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

Sidebar

Related Questions

I need to implement following categorization in SQL: group can contain multiple tests or
I need to implement a thread pool in Java (java.util.concurrent) whose number of threads
I need to implement portable code, but I do not know how to deal
I need to implement an efficient excel-like app. I'm looking for a data structure
I need to implement the following: There is a table A which is supposed
i need to implement the email signature with image.As of now we only support
I need to implement AI for game based on fuzzy logic. I need to
I need to implement some new functions on an editor. I picked Emacs -
I need to implement a simple monitoring app in Excel. It is for monitoring
I need to implement a one-to-one videoconferencing solution server-based, runnable by browser, free (or

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.