Multilevel associative array in java like php

For web service development, I have to make a multi-level associative array like this

Array
(
    [0] => Array
        (
            [id] => 1100
            [content] => This is content
            [creator_info] => Array
                (
                    [id] => 1
                    [fname] => abc
                    [lname] => xyz
                )

            [tag_info] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => my
                        )

                    [1] => Array
                        (
                            [id] => 4
                            [name] => you
                        )

                    [2] => Array
                        (
                            [id] => 5
                            [name] => your
                        )

                )

            [created_date] => 14 JAN 2012
        )

    [1] => Array
        (
            [id] => 1101
            [content] => This is content1
            [creator_info] => Array
                (
                    [id] => 2
                    [fname] => abc1
                    [lname] => xyz1
                )

            [tag_info] => Array
                (
                    [0] => Array
                        (
                            [id] => 35
                            [name] => wer
                        )

                    [1] => Array
                        (
                            [id] => 47
                            [name] => cvb
                        )

                    [2] => Array
                        (
                            [id] => 51
                            [name] => mnv
                        )

                )

            [created_date] => 15 JAN 2012
        )

)

      

referring to this question I am using ArrayList

.

in the following way

There are no associative arrays in Java like PHP.

There are various solutions for what you are doing, but it depends on how you want to search for information. You can easily write a class that stores all your information and stores instances of them in an ArrayList.

public class Foo{
    public String name, fname;

    public Foo(String name,String fname){
        this.name=name;
        this.fname=fname;
    }
}

      

And then...

List<Foo> foos = new ArrayList<Foo>();
foos.add(new Foo("demo","fdemo");
foos.add(new Foo("test","fname");

      

So, you can access them like ...

foos.get(0).name;
=> "demo"

      

It keeps me in awe of one level. If I continue with it, it looks like a complicated procedure.

I need a simple procedure with high speed.

For the last 3 days I have been working on this and I got success for the following

Map<String,Object> myMap1 = new HashMap<String, Object>();
List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();
Map<String,String> user_info = new HashMap<String, String>();
user_info.put("fname", "xyz");
user_info.put("lname", "pqr");
myMap1.put("id", 20);
myMap1.put("content", "This is content");
myMap1.put("creator_info", user_info);
myMap1.put("timestamp", "11 Jan 2012");
myMap.add(0,myMap1);

      

still can't figure out how to do it for tags_info

is there a library for this thing ....?

+3


source to share


2 answers


Java has a kind of associative arrays. They are called maps (see java.util.Map implementations). With a map, you can bind a value to a key, for example.

Map<String, String> mymap = new HashMap<String, String>();
mymap.put("one", "1");
mymap.put("two", "2"); 
// etc.

      



So, to solve your problem, you can create a complex data structure that is a combination of arrays, lists (see java.util.List) and maps.

But your structure looks too complex and won't be easy to manage. So your proposal is good. Create classes like Foo, maybe create another one that contains multiple instances of Foo, etc.

+5


source


This is old I know, but I felt like I just have to answer if anyone else reads this. Java is an object oriented language, so think about your data structure in terms of "objects". Forget about the strict array constructs that PHP forces developers to create.

When switching from a language like PHP to Java, you should leave the "PHP" way to do things and encompass objects.

public class Content {
    String content;
    int id;
    List<Tag> getTags() {...}
    List<Creator> getCreators() {...}

}
public class Tag {
    int id;
    String name;
}
public class Creator {
    String fname;
    String lname;
}

      



When using objects, for example, do the following:

List<Creator> creators = content.getCreators();
List<Tag> tags = content.getTags();

      

+1


source







All Articles