I use HashMaps for storing sets of pairs, but I also have to be sure from the first json serialization (from a Php client) and to the Java HashMap set browsing, the order of keys will be the same
//import com.google.gson.Gson;
//import com.google.common.base.Joiner;
String s = "{\"durant\":\"kevin\", \"james\":\"lebron\",\"harden\":\"james\",\"westbrook\":\"russel\"}";
System.out.println(s);
Gson gson = new Gson();
Type typeOfMap = new TypeToken<Map<String, String>>() {}.getType();
Map<String, String> m = gson.fromJson(s, typeOfMap);
List<String> entries = new ArrayList< String>(m.keySet());
//Collections.sort(entries);
System.out.println(entries);
System.out.println(m.entrySet());
System.out.println( Joiner.on(",").withKeyValueSeparator("").join(m) );
It seems working for small sets of values, without sorting again (Here by keys alphabetically), but is it a reliable way to do? could the order change between the json String and the Map browsing?
Edit: probably LinkedHashMap is more order preserving?