How to convert HashMap of objects to ArrayList of strings

I have it HashMap

like this:

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 them in ArrayList

like:

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 am getting this result from the code above: [4, 4;1;2;3, 4;5, 4;5;1;2;3]

+3


source to share


3 answers


You need to do it step by step:

  • Iterate over the keys of the Map, or get the values โ€‹โ€‹of the Map as a Collection and repeat that.
  • Separate each comma separated String value into separate tokens and add them to the list or set of values โ€‹โ€‹you want.
  • Sorting the list of values โ€‹โ€‹after it appears.

Use Set if you don't have duplicates.



Similar to JSON. Are you using 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;
    }
}

      

+1


source


Ok I figured it out 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 this gives me what I want, namely: [4,5,1,2,3]

+1


source


Try the following:

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

      

0


source







All Articles