Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional array in Java like PHP

I want to create an multidimensional array in Java, like the PHP one shown below. I want to create an exact same array. Is it possible?

Array
(
    [0] => Array
        (
            [name] => sagar
            [company] => Visa
        )

    [1] => Array
        (
            [name] => shloka
            [company] => Doctor
        )

    [2] => Array
        (
            [name] => sanket
            [company] => zon
        )

    [3] => Array
        (
            [name] => kamlesh
            [company] => nsc
        )

    [4] => Array
        (
            [name] => siddhi
            [company] => KES
        )

    [5] => Array
        (
            [name] => sanket
            [company] => zon
        )

    [6] => Array
        (
            [name] => bunty
            [company] => India Bull
        )

    [7] => Array
        (
            [name] => siddhi
            [company] => KES
        )

)
like image 711
Sanket Utekar Avatar asked Mar 09 '26 18:03

Sanket Utekar


1 Answers

The "Way of the Java" would be to make a POJO class to store that info, like this:

class UserInfo {
    private String name;
    private String company;

    public UserInfo(String name, String company) {
        this.name = name;
        this.company = company;
    }

    public String getName() {
        return name;
    }

    public String getCompany() {
        return company;
    }
}

UserInfo[] users = {
    new UserInfo("sagar", "visa"),
    new UserInfo("shloka", "Doctor"),
    ....
};

However, if you want it exactly the same way you did in PHP, there you go:

public static Map<String,String> createUser(String name, String company) {
    final Map<String,String> result = new HashMap<String,String>();
    result.put("name",name);
    result.put("company",company);
    return result;
}

List<Map<String,String>> users = new ArrayList<Map<String,String>>();
users.add(createUser("sagar","visa"));
users.add(createUser("shloka","Doctor"));
....

Added:

For the sake of completeness of the answer, here's another way using instance initializers. It may look prettier that the other solutions in some cases, but basically it has one flaw: it uses unrequired permgen space. Not much of it, but it is still unrequired:

List<Map<String,String>> result = new ArrayList<Map<String,String>>() {{ 
    add(new HashMap<String,String>() {{
        put("name", "someName");
        put("company", "someCompany");
    }}); 
    add(new HashMap<String,String>() {{
        put("name", "someName1");
        put("company", "someCompany1");
    }}); 
}};

Basically this solution is overriding an ArrayList with a new class (that's why a permgen is used) which has an instance initializer with addition commands. Each addition also uses an overriden HashMap class with an instance initializer. This may be considered a "synthetic sugar" for Java.

like image 185
bezmax Avatar answered Mar 11 '26 09:03

bezmax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!