-1

I am a beginner in java programming and majorly a javascript developer. I have a js array like,

[
  {
    "key1": 0,
    "key2": 1,
    "key3": "string"
  },
{
    "key1": 1,
    "key2": 2,
    "key3": "string2"
  }
]

How do I construct the same structure in Java?

sSD
  • 282
  • 2
  • 17

5 Answers5

2

Here as per requirement you need to follow below steps:

1) Create a Class with members (key1, key2, key3).

2) Create object of that class and add that to List. (as you mentioned in comments that you have to iterate it, it's better to use ArrayList.)

Step 1 :

public class Data {

  private int key1;

  private int key2;

  private String key3;

  public Data(final int key1, final int key2, final String key3) {
    super();
    this.key1 = key1;
    this.key2 = key2;
    this.key3 = key3;
  }

  /**
   * @return the key1
   */
  public int getKey1() {
    return key1;
  }

  /**
   * @return the key2
   */
  public int getKey2() {
    return key2;
  }

  /**
   * @return the key3
   */
  public String getKey3() {
    return key3;
  }

  /**
   * @param key1
   *          the key1 to set
   */
  public void setKey1(final int key1) {
    this.key1 = key1;
  }

  /**
   * @param key2
   *          the key2 to set
   */
  public void setKey2(final int key2) {
    this.key2 = key2;
  }

  /**
   * @param key3
   *          the key3 to set
   */
  public void setKey3(final String key3) {
    this.key3 = key3;
  }

}

Step 2:

public class Test {

  public static void main(final String[] args) {

    //Using List
    final List<Data> myDataList = new ArrayList<Data>();
    myDataList.add(new Data(0, 1, "string"));
    myDataList.add(new Data(1, 2, "string2"));

    // Or

    //Using Array
    final Data[] myData = {new Data(0, 1, "string"), new Data(1, 2, "string2")};


  }

}
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • "Using Array" is wrong, you assign both to same index. Would be simpler, and closer to JS version, if you used array initializer, which would automatically fix the error too. – Andreas Nov 05 '19 at 09:25
  • @Andreas : Sorry for typo of array index. Yes make sense to use array initializer. Thanks for the inputs. :) – Gaurav Jeswani Nov 05 '19 at 09:28
0

from what I can see what you are trying to achieve can be simply created by using a list of Map if you do not care about types (not recommended).

List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();

But if you are serious about using Java you will need to learn about Object Oriented Programming. You will need to create a class with strongly typed attributes. After creating a class, you can create an array and fill it with instances of the said class (Object).

Gauthier
  • 3
  • 3
0

Option 1: List of maps. Not type-safe, since we don't declare exact value types.

List<Map<String, ?>> list = List.of(
    Map.of(
        "key1", 0,
        "key2", 1,
        "key3", "string"
    ),
    Map.of(
        "key1", 1,
        "key2", 2,
        "key3", "string2"
    )
);

Option 2: Array of POJOs. Type-safe, since all values are explicitly typed.

MyObj[] array = { new MyObj(0, 1, "string"),
                  new MyObj(1, 2, "string2") };
public class MyObj {
    private int key1;
    private int key2;
    private String key3;
    public MyObj() {
    }
    public MyObj(int key1, int key2, String key3) {
        this.key1 = key1;
        this.key2 = key2;
        this.key3 = key3;
    }
    public int getKey1() {
        return this.key1;
    }
    public void setKey1(int key1) {
        this.key1 = key1;
    }
    public int getKey2() {
        return this.key2;
    }
    public void setKey2(int key2) {
        this.key2 = key2;
    }
    public String getKey3() {
        return this.key3;
    }
    public void setKey3(String key3) {
        this.key3 = key3;
    }
}

List vs array can of course go either way, I just wanted to show both options there too.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

You have to create a Java class with key1, key2, and key3 and also setter/getter. After that, you can use google gson gson artifact from maven(if you using maven project) or Json for java (jsonartifact).

MANITORATION
  • 557
  • 2
  • 5
  • 19
-1

First you have to create a Java class having variables key1, key2 and key3. Then use the Jackson library to parse Json to Java object.

wr93_
  • 130
  • 1
  • 1
  • 13
  • OP is not asking to parse JSON. OP is showing JavaScript code, and is asking what the equivalent Java code would be. – Andreas Nov 05 '19 at 09:24