Gaurav Sharma
Gaurav Sharma

Reputation: 4052

Multi-dimensional data structure in java

I need to create a data structure that is of the following syntax:

[
 status: "success",
 statusDescription: "statusDescription"
 count: "5",
 states: [
  [stateId: "1", stateDescription: "1 description"],
  [stateId: "2", stateDescription: "2 description"],
  [stateId: "3", stateDescription: "3 description"],
  [stateId: "4", stateDescription: "4 description"],
  [stateId: "5", stateDescription: "5 description"]

 ]
]

I'm not sure how to do that in Java. I'm trying various combinations of ArrayLists and Maps, but can't quite get it.

EDIT: I actually need to return this data type as a response to a JAX-WS @WebMethod call. Creating a separate class, especially one with a List element is causing problems.

Upvotes: 1

Views: 1587

Answers (7)

Alexander Razmakhnin
Alexander Razmakhnin

Reputation: 163

Really bad design, but exactly what you want:

public static void main(String[] args) {
    HashMap<String, Object> hashMap = new HashMap<String, Object>();
    hashMap.put("status", "success");
    hashMap.put("statusDescription", "statusDescription");
    hashMap.put("count", "5");
    List<Object> list = new ArrayList<Object>();
    hashMap.put("states", list);
    for (int i = 1; i < 5; i++) {
        HashMap<String, String> hashMapInternal = new HashMap<String, String>();
        hashMapInternal.put("stateId", String.valueOf(i));
        hashMapInternal.put("stateDescription", i + " description");
        list.add(hashMapInternal);
    }
}

Upvotes: 0

jasiustasiu
jasiustasiu

Reputation: 1668

I would write this:

public class Whatever {
    private Status status;
    private String statusDescription;
    private int count;
    private List<Map<int,String>> states;
}    

or this:

public class Whatever {
    private Status status;
    private String statusDescription;
    private int count;
    private List<State> states;
} 

where State and Status are enums. It's up to you. I suppose there are limited statuses

Upvotes: 0

kanhai shah
kanhai shah

Reputation: 1333

For this You can create one class such as

        class A

     {
       private int statusId;
        private String Description;
        private int count;
      }

now create a data structure as per your requirement and add object of class A to that data structure.

Upvotes: 0

SJuan76
SJuan76

Reputation: 24895

? Do your own class.

status, statusDescription are String properties of your class.

count is an integer property (or String if you want).

states is either an enum, a Map or a two dimensional array. Whatever fits.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198471

Build your own class. Please.

class Foo {
  private final String status;
  private final String statusDescription;
  private final int count;
  private final List<State> states;
}

class State {
  private final int stateId;
  private final String stateDescription;
}

Upvotes: 5

Petar Minchev
Petar Minchev

Reputation: 47403

class Response {
   private boolean success;
   private String statusDescription; 
   private List<State> states;
}

class State {
   private int id;
   private String description;
}

Upvotes: 2

moonwave99
moonwave99

Reputation: 22820

Define a class, with an array [or ArrayList] of states.

Upvotes: 0

Related Questions