3

For development of web services, i have to make multilevel Associative array as follows

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
        )

)

with referencing this question, i am using ArrayList.

as follows

Java doesn't have associative arrays like PHP does.

There are various solutions for what you are doing, but it depends on how you want to look up the information. You can easily write a class that holds all your information and store instances of them into 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"

This keeps me happy for single level. If i keeps continue with it, this looks like as a complicated procedure.

I wants something simple procedure with high speed.

Last 3 days i am working in it and i got the success for 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 not getting that how to do it for tags_info

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

Community
  • 1
  • 1
KuKu
  • 646
  • 2
  • 15
  • 38

2 Answers2

5

Java does have kind of associative arrays. They are called Maps (see implementations of java.util.Map). With map you can associate value to key, e.g.

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 complex data structure that is a combination of arrays, lists (see java.util.List) and maps.

But your structure looks too complicated and will not be easily manageable. So, your suggestion is good. Create classes like Foo, probably create yet another that holds several Foo instances etc.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    is there any library which can help me out..? i am not suppose to be first person facing this problem...! in question i updated my code as well, welcome for any comment.. – KuKu Apr 04 '12 at 18:25
2

This is old I know but, I felt I just have to reply for anyone else reading this. Java is an Object Oriented language so think of your data structure in terms of 'objects'. Forget deep array constructs that PHP leads developers to create.

When moving from a language like PHP to Java you have to leave the 'PHP' way of doing things and embrace 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 the objects for example do the following...

List<Creator> creators = content.getCreators();
List<Tag> tags = content.getTags();
David
  • 5,203
  • 2
  • 16
  • 17