3

I have a HashMap like so:

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:

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]

1
  • Does that compile in android? It doesn't in Java 8. map.put won't take the {..} values. Do you mean map.put("1",new String[]{"1","2","3"}); ? Commented Jun 24, 2015 at 19:16

3 Answers 3

1

You have to do it in steps:

  1. Iterate over the Map keys or get the Map values as Collection and iterate over that.
  2. Parse each value's comma separated value String into individual tokens and add them to the List or Set of values you want.
  3. Sort the List of values once you have it.

Use a Set if you'd rather not have duplicates.

Looks like 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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I transform that map into a json : hashObject = new Gson(); final String json = hashObject.toJson(aMap);
1

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]

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.