java 将 ArrayList 转换为 HashMap<String, String>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31803200/
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-11-02 19:09:32  来源:igfitidea点击:

Convert ArrayList to HashMap<String, String>

javaandroidarraylist

提问by korunos

I have this ArrayList

我有这个 ArrayList

public ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();

and I want to convert this to:

我想将其转换为:

HashMap<String, String> comparemap2 = new HashMap<>();

What I want is: I want all the Items inside the ArrayList and want to put them into the HashMap My HashMap looks like:

我想要的是:我想要 ArrayList 中的所有 Items 并且想要将它们放入 HashMap 我的 HashMap 看起来像:

KEY VALUE

job_id 032014091029309130921.xml

job_id 201302149014021492929.xml

job_id 203921904901920952099.xml

核心价值

job_id 032014091029309130921.xml

job_id 201302149014021492929.xml

job_id 203921904901920952099.xml

EDIT:Later I want to compare this map with an existing map:

编辑:稍后我想将此地图与现有地图进行比较:

Properties properties = new Properties();
            try {
                properties.load(openFileInput("comparexml.kx_todo"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (String key : properties.stringPropertyNames()) {
                compareMap.put(key, properties.get(key).toString());
            }
HashMap<String, String> oldCompareMap = new HashMap<>();
            for (HashMap key : xmlFileNames) {
                oldCompareMap.putAll(key);
            }
            isEqualMaps(oldCompareMap, compareMap);

I only want to compare, if the filename exists in the compareMap. If not, than add it to the xmlFileNameMap

我只想比较,如果文件名存在于compareMap. 如果没有,则将其添加到xmlFileNameMap

I've looked up in StackOverFlow, how I can convert ArrayList to HashMap. But the other Threads treat data types like Itemor Product.

我在 StackOverFlow 中查找过,如何将 ArrayList 转换为 HashMap。但是其他线程将数据类型视为Itemor Product

I hope you can help me!

我希望你能帮帮我!

Kind Regards

亲切的问候

回答by RNJ

Given...

鉴于...

public ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();

then something like this should do it.

那么这样的事情应该做。

HashMap<String, String> nhm = new HashMap<>();
for (HashMap xmlFileHm : xmlFileNames ) {
  nhm.putAll(xmlFileHm);
}

but be aware if you have duplicate keys in your hashmaps they will get overwritten.

但请注意,如果您的哈希图中有重复的键,它们将被覆盖。

You should also think about coding to interfaces. Take a look at Map and List rather than typing your collections to implementations (ArrayList and HashMap). Take a look at this thread which is quite interesting What does it mean to "program to an interface"?

您还应该考虑对接口进行编码。看看 Map 和 List,而不是将你的集合输入到实现中(ArrayList 和 HashMap)。看看这个线程,它很有趣“编程到接口”什么意思?

Depending on what you are trying to do as well you might consider a MultiMapas this might server your purposes better

根据您正在尝试做的事情,您可能会考虑使用MultiMap,因为这可能会更好地满足您的目的

EditAfter update to the question...

编辑问题更新后...

A multimap would be better here with one key and multiple values. Although arguably if the key never changes then you could just store the values in a list. For multiamps you can use Google's guava library or do one yourself. For example (not checked for compilation errors as Im doing this from my head)

多图在这里使用一个键和多个值会更好。虽然可以说如果键永远不会改变,那么你可以将值存储在列表中。对于多放大器,您可以使用 Google 的番石榴库或自己做一个。例如(没有检查编译错误,因为我是从我的头脑中这样做的)

Map<String, List<String>> m = new HashMap<>();
if (m.containsKey("key")) {
   m.get("key").add("new value");
}
else {
  List<String> l = new ArrayList<>();
  l.add("new value");
  m.put("key", l);
}

回答by Ruchira Gayan Ranaweera

You can try something like this..

你可以试试这样的..

ArrayList<HashMap<String, String>> xmlFileNames = new ArrayList<>();
HashMap<String, String> comparemap2 = new HashMap<>();
for(HashMap<String, String> i:xmlFileNames){
     comparemap2.putAll(i);
}

You may need to consider the case of duplicate keys. else they will get override.

您可能需要考虑重复键的情况。否则他们将被覆盖。

回答by Michal

You can create a new HashMap, then iterate through the list and put all elements from the map from the list to the main map.

您可以创建一个新的 HashMap,然后遍历列表并将地图中的所有元素从列表中放到主地图中。

List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
for (Map<String, String> mapFromList : list) {
    map.putAll(mapFromList);
}

回答by Sandeep Bhardwaj

Create a new map and put All each element of arrayList to the map.

创建一个新地图并将 arrayList 的所有每个元素都放入地图。

But in that case if you have same keys in two element of arrayList (hashmap) then it will override the previous one.

但是在这种情况下,如果您在 arrayList (hashmap) 的两个元素中有相同的键,那么它将覆盖前一个。