如何在Jva中实现n:m关系? [英] How to implement n:m relation in Jva?

查看:131
本文介绍了如何在Jva中实现n:m关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java中实现一个n:m关系。
用例是一个目录。




  • 一个产品可以分为多个类别

  • 一个类别可以容纳多个产品



经典...



我目前的解决方案是具有两个hashmaps的映射类。




  • 第一个hashmap的关键是产品ID,值为类别ID列表

  • 第二个hashmap的关键是类别ID,值是产品ID列表



这是完全冗余的,我需要一个设置类,始终关心数据在两个hashmaps中的存储/删除。



但是是我在 O(1)中找到的唯一方法:




  • 什么产品持有类别?

  • 什么类别的产品?



我想避免全阵列扫描或者是这样的东西!



不是另一个更优雅的解决方案,我不需要两次索引数据!



请点燃我...我只有纯Java,没有数据库或SQLite或可用的东西...如果可能,我也不希望实现一个btree结构。

解决方案

如果你通过成员集合将产品分类与产品相关联,反之亦然,您可以完成相同的操作:

  public class Product {
私人设置< Category> categories = new HashSet< Category>();
//实现hashCode和equals,可能通过id来获得额外的性能
}

public class Category {
private Set< Product> contents = new HashSet< Product>();
//实现hashCode和equals,可能通过id获得额外的性能
}

唯一困难的部分是填充这样的结构,可能需要一些中间地图。



但是使用辅助的hashmaps / tree进行索引的方法不是一个坏的。毕竟,放在数据库上的大多数索引例如是辅助数据结构:它们与行表共存;行不一定组织在索引本身的结构中。



使用这样的外部结构可以让您保持彼此分离的优化和数据;这不是坏事。特别是如果明天你想添加O(1)查找产品给供应商,例如



编辑:由方式,它看起来像你想要的是 Multimap 进行优化,以便在O(1)中进行反向查找。我不认为Guava有这样的做法,但是您可以实现Multimap界面,所以至少你不必单独处理HashMaps。其实它更像是一个BiMap,也是一个Multimap鉴于他们的定义,这是矛盾的。我同意MStodd,你可能想要滚动自己的抽象层来封装这两个地图。


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

The classic...

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.

解决方案

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.

这篇关于如何在Jva中实现n:m关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆