双向映射的最佳数据结构 [英] Best data structure for two way mapping

查看:176
本文介绍了双向映射的最佳数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个从键到对象映射的数据结构,反之亦然(与仅在单个方向映射的HashMaps不同)。一个想法可能是将HashMap存储在其内部以进行反向查找,但是它将是



双向映射的最佳实现是什么?

解决方案

最简单的想法:包含2个地图的包装类,其次是交换的键/值。您将保持O(1)的复杂性,并将仅使用稍微更多的内存,因为您可能(可能)保持引用对象。


I want a data structure that maps from key to object and vice-versa(unlike HashMaps that map only in a single direction.) An idea could be to store the HashMap within itself for reverse look-up, but it will be an inefficient approach.

What would be the best implementation for two-way mapping?

解决方案

Simplest idea: wrapper class which contains 2 maps, second with swapped keys/values. You will keep O(1) complexity and will use only slightly more memory since you will (probably) keep there reference to object.

这篇关于双向映射的最佳数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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