1

Sorry for my bad English. I'll try to explain my problem as simple as possible;
I'm making an app-client to one website. No social network, but with blogs.
Now my app downloads 50 posts per request with the following information:

Title, Author, Type of Post, Link

They are stored to ArrayList in such way:

Title1, Author1, Type1, Link1, Title2, Author2, Type2, Link2 ... Title50, Author50, Type50, Link50

So, I have 4 * 50 = 200 elements in arraylist.
(Please, note, that this ArrayList is used while parsing webpage). In future, I want to store some additional information, such as data of post, number of likes, link to avatar and so on. As far as I know, 50 posts will demand ~600-800 elements in ArrayList.
I'm afraid that this will significally slow down performance of my Android app.

So, the question is: How would YOU recommend to store parsed data?
Thanks in advance.

I'll show some code to explain the problem better:
While parsing webpage with Jsoup I do the following thing:

post_data = new ArrayList<String>();
for (Element e : entries) {
post_data.add(e.select("title").text()); // title 
post_data.add(e.select("author name").text()); // author
post_data.add(e.select("category").attr("term").toString()); // type
Elements links_int = e.select("link");
    for (Element g : links_int) {
    if (g.attr("rel").toString().equals("self")) {
     post_data.add(g.attr("href").toString());  // post link (if there's one)
    } else {  continue;
    }
}

Right after page is parsed, I fill my data scructure named "items" (and that data structure is used to form ListView):

for (int i = 0; i < (post_data.size()); i += 4) {
items.add(new WhatsNew_post_Item(
    WhatsNew.this.post_data.get(i),
    WhatsNew.this.post_data.get(i + 1),
    WhatsNew.this.post_data.get(i + 2),
    WhatsNew.this.post_data.get(i + 3)));
}
6
  • Do you want the information to persist permanently or temporarily ? Commented Feb 23, 2014 at 18:27
  • 4
    "I'm afraid that this will significally slow down performance of my Android app." Have you tried it? 800 elements is pretty small for an ArrayList. There's no point in being "afraid" of performance issues without testing it. I'd be far more concerned about your data representation - it sounds like you should have an ArrayList<Post> where each Post contains the title, author, type and link... rather than 4 elements in an ArrayList<String> per post. Commented Feb 23, 2014 at 18:27
  • Moreover in your case if you know that you will be receiving about 50 posts then you can initialize your arraylist to be of size 50 to avoid unneccesary time to expand the arraylist when it become full : List<post> posts = new ArrayList<Post>(50); Commented Feb 23, 2014 at 18:30
  • I've edited question with code, could you please look at it? Commented Feb 23, 2014 at 18:35
  • @JonSkeet I'm adding elements to ArrayList while parsing the webpage. I don't think I can handle creating instances of Post class that time. And after filling ArrayList I create Items classes from ArrayList. Commented Feb 23, 2014 at 20:23

3 Answers 3

3

Create a data structure to hold the information about the post, and store that data structure in your list:

public class PostInfo {
    private String author:
    private String title;
    // ...
}

// ...

public List<PostInfo> postInfos = new ArrayList<>(50);
// retrieve data and add it to the list
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I have this structure, but it's only used to fill ListView in the UI. Ermm.. this ArrayList (explained in question) is used while parsing the webpage.
2

If you know how many items you'll get in your list create the ArrayList with the capacity:

new ArrayList<>(int capacity);

By default, new ArrayList<>() has a capacity of 10 elements. If you reach the capacity, the ArrayList will double the current capacity (10 -> 20 -> 40 -> 80 -> ...) by creating a new array and put old and new objects, the old array will be destroyed after by the garbage collector.

Comments

1

Create a Class having "Title, Author, Type of Post, Link" as instance variables. Read the downloaded posts and create the objects of this class by passing the variables either through constructor or setter methods. Store the objects of this class in your ArrayList. This way you can store 50 objects for 50 posts in an arraylist.

public class BlogPost { private String author: private String title; private String postType; private String link; }

In the Main program where you are reading the downloaded posts, you can say

List<BlogPost> postList = new ArrayList<BlogPost>(50);
BlogPost bPost = new BlogPost(author, title, postType, link);
postList.add(bPost);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.