Hesam
Hesam

Reputation: 53610

Java, Which of these ways better for achieving faster response and memory management? 100 object of a class or 1 object with arrayList

In my project i need to load aml data from internet and after that parse it. I have no problem in parsing xml. Each xml has 20 elements. let me explain my question with a sample.

this is the format of xml:

<XML>
    <Item1> 
        <element1>a</element1> 
        <element2>b</element2> 
        ... 
        <element20>f</element20> 
    </Item1>
    <Item2> 
        <element1>d</element1> 
        <element2>s</element2> 
        ... 
        <element20>l</element20> 
    </Item2>
...
</XML>

My question is about the class that i want to create. One way is creating class and put each element as its properties such as:

class myItems {
    private String element1;
    private String element2;
    ...
    private String element20;
    //and also adding all setters and getters
}

Therefore, each item of XML will have one object of this class. So, if i have 100 items in xml file, i will have 100 objects of this class.

second way is using ArrayList in the class. something like:

Class myItems {
    ArrayList<String> element1 = new ArrayList<String>();
    ArrayList<String> element2 = new ArrayList<String>();
    ...
    ArrayList<String> element20 = new ArrayList<String>();

    //and for setters and getters
    public ArrayList<String> getElement1() {
        return element1;
    }
    public void setElement1(String str) {
        this.element1.add(str);
    }
    ...
}

Therefore, I have one object that it has all of my variables.

My question is, which of these ways is better in terms of speed, memory consumption and anything else?

Upvotes: 1

Views: 101

Answers (4)

Navneeth G
Navneeth G

Reputation: 7305

How would you use this in your application?

If they are accessed directly like getItem1(), getItem2() etc... then go forward with your first approach, note that the fields should be meaningful if you want this approach to be a "good one".

It they are like ingredients of a recipe, and you dont matter the order in which they appear, then go for the ArrayList.

If they are meaningful headers and are subject to change, have a HashMap that stores the values. HashMap is the most flexible solution, but beware, performance!

Upvotes: 0

Alex Ciminian
Alex Ciminian

Reputation: 11498

The best way to do this in my opinion is have a clear model of the object you're trying to represent, as a class:

class Item {
    private member; // etc.
    ...
}

You will want to abstract meaningless stuff from the XML structure into your class. For example, you could combine two or three elements from the XML into one member if it makes sense. You can do that when creating the object. You will end up with a smaller footprint (fewer public methods) and a clearer implementation. Then, you can represent the list of items in your program as a List:

List<Item> allItems = new ArrayList<Item>()

Personally, I believe that if you're not facing serious performance constraints from the beginning (which I don't think you are), clarity and clean implementation trumps performance. You should be more careful about design as opposed to performance right now. Premature optimization is generally viewed as a bad thing.

Unrelated, here is an interesting presentation on Java memory consumption :).

Upvotes: 1

feldoh
feldoh

Reputation: 658

You will get better performance and memory usage from the second as arraylists obtain too much memory at a time... a regular array would be more efficient. The first method is more readable and to be honest more standard. You could also use a hash map instead of an arraylist to make the code more readable, as you can name the fields.

Upvotes: 0

John3136
John3136

Reputation: 29266

Answer is "it depends" ;-) You want your data structures to map to how the data "is".

i.e. if this is really a 20 column wide grid, then why not store the data that way so you can look it up by row/col. If each item is an individual, then the Item should be a class. In terms of memory there probably isn't a lot of difference.

For what I've seen above, I think I'd do

class Item
{
    private ArrayList<String> items = new ArrayList<String>(20); 
}

And provide appropriate getters and setters.

Upvotes: 1

Related Questions