如何实现多对一的数据结构?

use*_*667 1 java map many-to-one data-structures

我希望反向绘制地图,这意味着我有很多键,只有一个值.我需要这个结构,所以当我搜索其中一个键时,我得到了值.

在此输入图像描述

我可以使用简单的哈希映射,但由于多次存储值,它会浪费空间.我正在寻找java中的优化和高效实现.我很感激你的建议.

Ade*_*dem 7

应该使用HashMap.当你将"布"作为值放入HashMap时,它不会在内存中重复.只是引用写入HashMap.

String hat = "hat";
String dress = "dress";
String paths = "paths";
String scarf = "scarf";
String cloth = "cloth";
HashMap h = new HashMap();
h.put(hat,cloth);
h.put(paths,cloth);
h.put(dress,cloth);
h.put(scarf,cloth);
Run Code Online (Sandbox Code Playgroud)

对于此示例,内存仅保留布料对象一次.