简体   繁体   English

如何将对象的HashMap转换为字符串的ArrayList

[英]How to convert a HashMap of Objects into an ArrayList of Strings

I have a HashMap like so: 我有一个像这样的HashMap

Hashmap<String, Object> map = new Hashmap<String, Object>();
map.put(1, {id_student:"1;2;3"});
map.put(2, {id_student:"4;5"});

I want to get the values and put it in an ArrayList like: 我想获取值并将其放在ArrayList如:

array = [0] - 1
        [1] - 2
        [2] - 3
        [3] - 4
        [4] - 5

what I tried to do: 我试图做的:

private Set<String> checked = new TreeSet<String>();

String idsAlunos = "";
for (Iterator<Map.Entry<String, GPSEscolas>> it = aMap.entrySet().iterator(); it.hasNext(); ) {
    Map.Entry<String, GPSEscolas> entry = it.next();

    String id_escola = entry.getKey();
    String ids_aluno = entry.getValue().getAlunos();

    idsAlunos += ";" + ids_aluno;

    checked.add(idsAlunos.substring(1));
}

but I'm getting this result from above code: [4, 4;1;2;3, 4;5, 4;5;1;2;3] 但是我从上面的代码中得到了这个结果: [4, 4;1;2;3, 4;5, 4;5;1;2;3]

You have to do it in steps: 你必须分步完成:

  1. Iterate over the Map keys or get the Map values as Collection and iterate over that. 迭代Map键或将Map值作为Collection获取并迭代。
  2. Parse each value's comma separated value String into individual tokens and add them to the List or Set of values you want. 将每个值的逗号分隔值String解析为单个标记,并将它们添加到所需的List或Set值中。
  3. Sort the List of values once you have it. 拥有它后,对值列表进行排序。

Use a Set if you'd rather not have duplicates. 如果您不想重复,请使用Set。

Looks like JSON. 看起来像JSON。 Are you using JSONSimple? 你在使用JSONSimple吗? Perhaps you should be. 也许你应该这样。

Here's an example. 这是一个例子。

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MapToArrayExample
 * User: mduffy
 * Date: 6/24/2015
 * Time: 3:17 PM
 * @link http://stackoverflow.com/questions/31034737/how-to-convert-a-hashmap-of-objects-into-an-arraylist-of-strings/31034794?noredirect=1#comment50094533_31034794
 */
public class MapToArrayExample {

    public static void main(String[] args) {
        Map<Integer, String> data = new HashMap<Integer, String>() {{
            put(1, "3, 2, 3, 3, 1");
            put(2, "4, 5, 5, 6, 7");
        }};
        List<String> result = parseMapOfJson(data);
        System.out.println(result);
    }

    public static List<String> parseMapOfJson(Map<Integer, String> map) {
        List<String> values = new ArrayList<String>();
        for (Integer key : map.keySet()) {
            String csv = map.get(key);
            String [] tokens = csv.split(",");
            for (String token : tokens) {
                values.add(token.trim());
            }
        }
        Collections.sort(values);
        return values;
    }
}

Ok, I got it, it is like this: 好的,我明白了,它是这样的:

idsAlunos = "";

for (Iterator<Map.Entry<String, GPSEscolas>> it = aMap.entrySet().iterator(); it.hasNext(); ) {
   Map.Entry<String, GPSEscolas> entry = it.next();

   String id_escola = entry.getKey();
   String ids_aluno = entry.getValue().getAlunos();

   idsAlunos += ";" + ids_aluno;

   String[] array = idsAlunos.substring(1).split(";");

   list = new ArrayList<String>(Arrays.asList(array));

}

System.out.println(list);

and that gives me what I want, which is: [4,5,1,2,3] 这给了我想要的东西,这是: [4,5,1,2,3]

Try this: 尝试这个:

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("1", "1;2;3");
    map.put("2", "4;5");

    Set<String> checked = new TreeSet<String>();

    for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
        Map.Entry<String, String> e = (Map.Entry<String, String>) i.next();
        checked.addAll(Arrays.asList(e.getValue().split("\\s*;\\s*")));
    }
    System.out.println(checked);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM