簡體   English   中英

如何將對象的HashMap轉換為字符串的ArrayList

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

我有一個像這樣的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]

你必須分步完成:

  1. 迭代Map鍵或將Map值作為Collection獲取並迭代。
  2. 將每個值的逗號分隔值String解析為單個標記,並將它們添加到所需的List或Set值中。
  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;
    }
}

好的,我明白了,它是這樣的:

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]

嘗試這個:

    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