Java:是否有一个数据结构类似多重映射,但接受重复的键? [英] Java: Is there a data structure that works like a multimap but accepts duplicate keys?

查看:580
本文介绍了Java:是否有一个数据结构类似多重映射,但接受重复的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困难。实质上,我需要一个数据结构,有点像数据库。我需要能够为每个密钥拥有多个数据位,并且有可能拥有相同名称的多个密钥。然后,我需要能够搜索该数据结构并提取正确的关键字并检查其他可能的数据。这是一个例子:

 键|名称|价格

飞机|波音| 10000
飞机| LearJet | 4000
飞机|空客| 20000
车|本田| 500
车|福特| 450
车|雪佛兰600

所以我需要能够专门搜索波音公司的飞机和飞机。由于很多原因,我无法使用实际的数据库,因为我没有很多记录,所以它是完全的,因为我需要能够提取这些值。我在Java中发现了一个multimap实现,但是它不允许我插入多个具有相同名称的键,也不允许我不仅搜索键,而且还可以搜索具有相应值的键。



任何人都知道如何实现这一点?
谢谢



编辑:好的,我看了所有的答案。由于某种原因,我无法理解任何需要密钥的情况,但您正在使用地图。有很多答案,这个解决方案的特点,我把所有的错误,因为无法理解他们。
最终我需要有多个独特的密钥,我知道这些解决方案是解决方法,但我根本无法理解。



虽然很远从一个完美的解决方案,我使用Apache的MultiKey类,因为我只需要拉一个可能的值它可以正常工作。我感谢大家的努力,不幸的是我不能得到它的工作否则。对不起

解决方案

为什么不从Apache Commons系列中使用MultiKey



a href =http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/keyvalue/MultiKey.html =nofollow noreferrer> http://commons.apache.org /collections/api-3.1/org/apache/commons/collections/keyvalue/MultiKey.html

  //使用数据映射键+名称填充地图
Map map = new HashMap();
MultiKey multiKey = new MultiKey(key,name);
map.put(multiKey,price);

//稍后退休
MultiKey multiKey = new MultiKey(key,name);
< Number> price =(String)map.get(price);


I'm having a bit of a hard time. Essentially I need a data structure that would work a bit like a database. I need to be able to have multiple bits of data for each key and have the possibility of having multiple keys with the same name. Then I need to be able to search that data structure and pull the correct keyword and check it against other possible data. Here is an example:

Keys   |   Name   |  Price

Airplane | Boeing | 10000
Airplane | LearJet | 4000
Airplane | Airbus | 20000
Car | Honda | 500
Car | Ford | 450
Car | Chevy | 600

So I need to be able to specifically be able to search for Airplanes and Airplanes that are Boeing's. For a lot of reasons I cannot use an actual database, it'd be complete overkill, since I don't have many records, but I do need to be able to pull these values. I found a multimap implementation in Java but it didn't allow me to insert multiple keys with the same name nor did it allow me to do search not only keys but keys with a corresponding value.

Anyone have an idea of how this might be implemented? Thanks

Edit: Okay I looked over all the answers. I for some reason just cannot understand any situation where you need keys to be unique but you are using maps. There are many answers that feature this solution and I take all fault for not being able to understand them. Ultimately I needed to have multiple unique keys and I know these solutions were workaround for that but I just couldn't make sense of it.

While it's far from being a perfect solution, I used Apache's MultiKey Class and since I only need to pull one possible value it works okay. I appreciate everyone's effort unfortunately I just couldn't get it work otherwise. Sorry

解决方案

Why not use MultiKey from Apache Commons collections

http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/keyvalue/MultiKey.html

// populate map with data mapping key+name to price
Map map = new HashMap();
MultiKey multiKey = new MultiKey(key, name);
map.put(multiKey, price);

// later retireve the price
MultiKey multiKey = new MultiKey(key, name);
<Number> price = (String) map.get(price);

这篇关于Java:是否有一个数据结构类似多重映射,但接受重复的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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