合并两个 Hash Map Android

发布于 2024-09-26 11:13:35 字数 1158 浏览 4 评论 0原文

我想合并两个 HashMap。

我可以使用map1.putAll(map2);但我不想覆盖密钥,因为它们会有冲突的密钥。

因此,每个映射中的键将像这样

word1     word1
word2     word2
word3     word3

,当我合并它们时,我想:

word1
word2
word3
word4
word5
word6

它可以覆盖键,只要键是增量的并使用第一个键文本,即读取其中一个键并提取“单词”,这样每个都是word1 word2。

但另一个需要注意的是,我正在考虑移动环境,以及我可以做什么,而无需设置加载屏幕,甚至不需要设置加载屏幕。

所以作为一个初学者,我想:

    HashMap<String, Object> hm1 = new HashMap<String, Object>();
    hm1.put("key1", "a");
    hm1.put("key2", "a");
    hm1.put("key3", "a");
    HashMap<String, Object> hm2 = new HashMap<String, Object>();
    hm2.put("key1", "1");
    hm2.put("key2", "2");
    hm2.put("key3", "3");

    HashMap<String, Object> newHM = new HashMap<String, Object>();      
    String keyWord = "";
    for (String  s: hm1.keySet()) {
        keyWord = s;
        break;
    }
    int count = 0;
    for (Object o : hm1.values()) {
        newHM.put(keyWord+count, o);
    }
    for (Object o : hm2.values()) {
        newHM.put(keyWord+count, o);
    }

但我想知道,这有多有效?看起来是正确的,有更好的方法吗?我不想不必要地使用额外的对象

I want to merge Two HashMaps.

I could use map1.putAll(map2); but I don't want to overwrite the key's as yes they will have conflicting keys.

So the keys in each map will be like this

word1     word1
word2     word2
word3     word3

and when I merge them I would like:

word1
word2
word3
word4
word5
word6

It can just overwrite the keys, aslong as the keys are incremental and use the first key text i.e. reads one of the pairs and extracts 'word' so each would be word1 word2.

But the other caveat I was thinking of the mobile environment and what I can do without having to put up a loading screen or even capable of.

So as a starter I suppose:

    HashMap<String, Object> hm1 = new HashMap<String, Object>();
    hm1.put("key1", "a");
    hm1.put("key2", "a");
    hm1.put("key3", "a");
    HashMap<String, Object> hm2 = new HashMap<String, Object>();
    hm2.put("key1", "1");
    hm2.put("key2", "2");
    hm2.put("key3", "3");

    HashMap<String, Object> newHM = new HashMap<String, Object>();      
    String keyWord = "";
    for (String  s: hm1.keySet()) {
        keyWord = s;
        break;
    }
    int count = 0;
    for (Object o : hm1.values()) {
        newHM.put(keyWord+count, o);
    }
    for (Object o : hm2.values()) {
        newHM.put(keyWord+count, o);
    }

But I'm wondering, how efficient is this? It looks correct, And is there a better way to do it? I don't want to use extra object's unnecessarily

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我早已燃尽 2024-10-03 11:13:35

如果您的键是增量的并且基本上代表一个简单的索引,那么您应该使用List

您可以尝试自己的 List 实现,它也将存储关键字。

class KeyWordedArrayList<T> extends ArrayList<T>{
    private final String keyword;

    public KeyWordedArrayList(String keyword){
        this.keyword = keyword;
    }

    public String getKeyword(){
        return keyword;
    }
}

您还可以执行 Map 的实现:

class KeyWordedMap<T> extends HashMap<Integer, T> {
    private final String keyword;

    public KeyWordedMap(String keyword) {
        this.keyword = keyword;
    }

    public String getKeyword() {
        return keyword;
    }

    @Override
    public void putAll(Map<? extends Integer, ? extends T> m) {
        for (Map.Entry<? extends Integer, ? extends T> entry : m.entrySet()) {
            int i = entry.getKey();
            while (this.containsKey(i)) {
                i++;
            }
            this.put(i, entry.getValue());
        }
    }
}

If your keys are incremental an basically represent a simple index, you should use a List.

You could try your own implementation of List which will also store a keyword.

class KeyWordedArrayList<T> extends ArrayList<T>{
    private final String keyword;

    public KeyWordedArrayList(String keyword){
        this.keyword = keyword;
    }

    public String getKeyword(){
        return keyword;
    }
}

You can also do an implementation of Map :

class KeyWordedMap<T> extends HashMap<Integer, T> {
    private final String keyword;

    public KeyWordedMap(String keyword) {
        this.keyword = keyword;
    }

    public String getKeyword() {
        return keyword;
    }

    @Override
    public void putAll(Map<? extends Integer, ? extends T> m) {
        for (Map.Entry<? extends Integer, ? extends T> entry : m.entrySet()) {
            int i = entry.getKey();
            while (this.containsKey(i)) {
                i++;
            }
            this.put(i, entry.getValue());
        }
    }
}
一花一树开 2024-10-03 11:13:35

为了匹配我的例子,它是:

@Override
public void putAll(Map<? extends String, ? extends Object> m) {
    for (Map.Entry<? extends String, ? extends Object> entry : m.entrySet()) {
        String keyWord = "";
        for (String  s: this.keySet()) {
          keyWord = s.substring(0, s.length()-1);
          break;
        }
        int i = 0;
        while (this.containsKey(i)) {
            i++;
        }
        this.put(keyWord +i, entry.getValue());
    }
}

To match my example it would be:

@Override
public void putAll(Map<? extends String, ? extends Object> m) {
    for (Map.Entry<? extends String, ? extends Object> entry : m.entrySet()) {
        String keyWord = "";
        for (String  s: this.keySet()) {
          keyWord = s.substring(0, s.length()-1);
          break;
        }
        int i = 0;
        while (this.containsKey(i)) {
            i++;
        }
        this.put(keyWord +i, entry.getValue());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文