servlet多重映射_Java:是否有一个数据结构类似多重映射,但接受重复的键?

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

// 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);

price = (String) map.get(price);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值