コレクションを使用して、このタイプのデータ形式を作成したいと思います。
[username] = test
[password] = test
[model] = City
[action] = add
[City] = Array
(
[name] = Added from me
[state_id] = 1243
)
Map
ここにぴったりです
Map<Object, Object> map = new HashMap<Object, Object>();
map.put("username", "test@test.com");
map.put("city", arrayOfCity);
実装を使用している場合は、追加するカスタムタイプHashMap
の適切なバージョンを提供する必要があることhashcode()
を確認してくださいequals()
Map
プロパティのセットが固定されている場合は、クラスを使用して作成する必要があります。そうでない場合は、マップを使用します
オブジェクトを 1 つのオブジェクトにまとめます。
public class Person {
private final String username;
private final String[] cities;
public Person(String username, String[] cities){
this.username = username;
this.cities = cities;
}
public String getUsername(){
return this.username;
}
public String[] getCities(){ // or pass an int and return one city
return this.cities;
}
}
その後、1 つのコレクションに含めることができます。
Person p1 = new Person("test@test.com", new String[]{"Liverpool","London"});
Person p2 = new Person("another@test.com", new String[]{"New York","California"});
List<Person> people = new ArrayList<Person>();
people.add(p1);
people.add(p2);
Java HashMap を使用できます。これにより、まさに必要なものが提供されます。
Map<String, Object> map = new HashMap<String, Object>();
あなたが言及したフィールドへのpojoマッピングを書くことができます。これは、コレクションを使用して個々のエンティティを格納するよりも明確です。