【问题标题】:How to convert a HashMap of Objects into an ArrayList of Strings如何将对象的 HashMap 转换为字符串的 ArrayList
【发布时间】:2015-09-11 03:19:27
【问题描述】:

我有一个像这样的HashMap

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

我想获取这些值并将其放入 ArrayList 中,例如:

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

我想做什么:

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));
}

但我从上面的代码中得到了这个结果:[4, 4;1;2;3, 4;5, 4;5;1;2;3]

【问题讨论】:

  • 在android中编译吗?它不在 Java 8 中。map.put 不会采用 {..} 值。你是说map.put("1",new String[]{"1","2","3"}); 吗?

标签: java android arraylist hashmap


【解决方案1】:

你必须分步进行:

  1. 迭代 Map 键或将 Map 值作为 Collection 并对其进行迭代。
  2. 将每个值的逗号分隔值字符串解析为单独的标记,并将它们添加到所需的值列表或集合中。
  3. 拥有值列表后对其进行排序。

如果您不想重复,请使用 Set。

看起来像 JSON。你在使用 JSONSimple 吗?也许你应该是。

这是一个例子。

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;
    }
}

【讨论】:

  • 我将该地图转换为 json:hashObject = new Gson(); final String json = hashObject.toJson(aMap);
【解决方案2】:

试试这个:

    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);

【讨论】:

    【解决方案3】:

    好的,我知道了,是这样的:

    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);
    

    这给了我我想要的,那就是:[4,5,1,2,3]

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 2018-10-26
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      相关资源
      最近更新 更多