如何将HashMap放入具有相同键的不同元素中

时间:2017-03-29 22:53:12

标签: java spring list linked-list hashmap

这是我的输出,其中包含三个HashMap。

    var findProductLineByFilename = function(filename, productLines) {
    filename = filename.replace(/( |\-|\.|_)/g, '');
    var i;
    for (i in productLines) {
        productLine = productLines[i];

        // XXX: Hard-code names
        if (productLine.code_name === 'abc1') {
            if (filename.search(/(war1)|(w1)|(abc1)/) >= 0) {
                return productLine;
            }
        } else if (productLine.code_name == 'abc2') {
            if (filename.search(/(w2)|(abc2)/) >= 0) {
                return productLine;
            }
        } else if (productLine.code_name == 'abc3') {
            if (filename.search(/(w3)|(abc3)/) >= 0) {
                return productLine;
            }
        } else if (productLine.code_name == 'abc4') {
            if (filename.search(/(w4)|(abc4)/) >= 0) {
                return productLine;
            }
        } else if (productLine.code_name == 'abc5') {
            if (filename.search(/(w5)|(abc5)/) >= 0) {
                return productLine;
            }
        }
    }
    return null;
};

我希望它们看起来像这样

{2017-03-11=1, 2017-03-05=6}
{2017-03-11=1, 2017-03-05=5}
{2017-03-05=1}

2 个答案:

答案 0 :(得分:1)

您可以为数据结构创建一个包装类,例如

public class DateWrapper{
    private int a;
    private int b;
    private int c;
    private Date date;

    public DateWrapper(int a, int b, int c, Date date){
        this.a = a;
        this.b = b;
        this.a = c;
        this.date = date;
    }

    // getters and setters
}

然后只需制作Map<String, DateWrapper>

这种解决连接的多个事物的数据结构的方式允许管理setter,并且可以轻松地添加更多值

答案 1 :(得分:0)

我相信您可能正在寻找MultiHashMap

中的Apache Commons Collections

以下是文档中的示例:

MultiMap mhm = new MultiHashMap();
mhm.put(key, "A");
mhm.put(key, "B");
mhm.put(key, "C");
List list = (List) mhm.get(key);