jadrijan
jadrijan

Reputation: 1446

Use arrays, lists, or else for thousands of strings?

I have the following method that gets direcory names:

    private List<String> getListOfDirectories(String rootDirectoryPath) {

        List<String> listOfDirectories = new ArrayList<>();

        File directory = new File(rootDirectoryPath);
        File[] listOfFiles = directory.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {

            if (listOfFiles[i].isDirectory()) {
                listOfDirectories.add(listOfFiles[i].getName());
            }
        }

        return listOfDirectories;
    }

I am temporarily storing (not sure if storing is the proper terminology) these directory names in a List. If there are 50000 directory names, is the List the right choice? Is it memory efficient, and can it handle 50000 or more strings?

Edit: I am developing an app that searches local directory for html files and parses those html files.

Upvotes: 2

Views: 1274

Answers (4)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

The list can handle it, it only depends on whether have that much of memory available or not.

If you really know that your going to have 50000 elements then it would be good to declare the list as follows

List<String> list = new ArrayList<String>(50000); //specify the initial capacity

That will eliminate the list-resizing overhead.

Upvotes: 2

rai.skumar
rai.skumar

Reputation: 10667

As you pointed; you are looking for efficient way to search your local directory for html files.

So i think there is no point storing all details in memory in ArrayList as the number of html files will keep on varying. I will suggest you should run a process which searches all those files and you store html file names in a separate physical file ( say a txt file). This way you can keep updating your list of files at regular interval by running the job ( you can have a unix script or can do even through java ).

And when you actually need to perform some operations on those html files; read html file names from your txt file.

After reading from txt file you can use ArrayList; but will not suggest to hold all names in memory.

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359826

List is just an interface, so it really depends on the list implementation. Assuming ArrayList<String>, it will be roughly as memory-efficient as a String[]. The worst case for an array list is that the underlying array is a constant factor (typically 2) larger than the amount of data it's actually storing.

If you need memory efficiency, depending on how you're using the list later, you could use something like a trie or Bloom filter.

Upvotes: 5

Ted Hopp
Ted Hopp

Reputation: 234795

An ArrayList<String> is close to a bare String[] in memory usage. If you know that you are going to store about 50,000 strings, it would help to construct the ArrayList with that initial capacity; it would drastically cut down on reallocations. I definitely would not use a LinkedList, however. That has considerably more overhead.

There will be no trouble storing 50,000 entries provided the strings themselves fit into memory.

Upvotes: 4

Related Questions