java循环并向地图添加值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24503743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:38:32  来源:igfitidea点击:

java loop and add values to a map

javalistmaphashmap

提问by user3736748

New to using map and was wondering how to add values and loop thur it to retrieve the values. Below is my code:

刚开始使用 map 并想知道如何添加值并循环遍历它以检索值。下面是我的代码:

Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();

for ( int i = 0, m = 1; i < visualcategory.size(); i = i+2, m = m+2) {
    String categoryName = visualcategory.get(m);
    map.put(categoryName , null);
}

for which i will be having this which is im assuming is correct even with the null

为此我将拥有这个我认为即使为空也是正确的

MAP {5=null, 11=null, 15=null, 24=null, 96=null, 98=null}

Its currently null as the next process (below) will fill up that list as it loops thru a for condition unless it can be done to add the categoryName and the List values at the same time?

它当前为空,因为下一个进程(下面)将在它通过 for 条件循环时填充该列表,除非可以同时添加 categoryName 和 List 值?

Now, i need to loop and add a list from each string/map

现在,我需要循环并从每个字符串/地图中添加一个列表

String value1 = value;
String value2 = value;

for (Entry<String, List<String>> entry : map.entrySet()) {
    map.put(value1, entry.getValue());  --> doesn't work
    map.put(value2, entry.getValue());  --> doesn't work
}

I need something like this

我需要这样的东西

MAP {5=[value1,value2,etc], 11=[value1,value2,etc], .......

Problem is, it doesn't work I can't seem to add inside the List. I need help on how to add values from a map > and loop thru to it?

问题是,它不起作用我似乎无法在列表中添加。我需要关于如何从地图添加值 > 并循环到它的帮助?

采纳答案by Ninad Pingale

Use Iterator insted of EntrySet

使用 EntrySet 的 Iterator 插入

    Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
    Iterator itr=map.keySet().iterator();
    while (itr.hasNext()) {
        String key =  itr.next().toString();
        String value=map.get(key).toString();
        System.out.println(key+"="+value);
    }

回答by shmosel

When you create your map, instead of filling it with nulls, fill it with empty Lists:

创建地图时,不是用nulls 填充它,而是用空Lists填充它:

Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();

for ( int i = 0, m = 1; i < visualcategory.size(); i = i+2, m = m+2) {
    String categoryName = visualcategory.get(m);
    map.put(categoryName , new ArrayList<String>());
}

Now, when you're ready to populate the values, you can do:

现在,当您准备好填充值时,您可以执行以下操作:

String value1 = value;
String value2 = value;

for (List<String> list : map.values()) {
    list.add(value1);
    list.add(value2);
}

If you have the values when you're creating the map, you make it simpler:

如果你在创建地图时有这些值,你可以让它更简单:

String value1 = value;
String value2 = value;

Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();

for ( int i = 0, m = 1; i < visualcategory.size(); i = i+2, m = m+2) {
    String categoryName = visualcategory.get(m);
    List<String> values = new ArrayList<String>();
    values.add(value1);
    values.add(value2);
    map.put(categoryName, values);
}

回答by user5495300

This is what you need:

这是你需要的:

anotherMap.forEach((k, v) -> myMap.put(k.toString(), v.toString()));

Source: Looping Through a Map in Java

来源:在 Java 中循环遍历地图