How to store the created ArrayList in memory so it doesn't get created every time app starts?

user13143281 :

I have a song lyrics app with a list of song names and numbers that opens lyrics of selected song. It's list of more than 1000 songs names with numbers that are read by a BufferedReader from a file and placed to ArrayList that is used to make a RecyclerView width CardWidgets. Is there a way to store the genereted ArrayList in internal memory of the phone so the app wouldn't repeat the same process on the start? (Without using a database) Here is some sample code:

public void setRecyclerView(final ArrayList<ExampleSong> exampleList){
    mRecycleView = findViewById(R.id.recyclerView);
    mRecycleView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mAdapter = new ExampleAdapter(exampleList);

    mRecycleView.setLayoutManager(mLayoutManager);
    mRecycleView.setAdapter(mAdapter);

    mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
        @Override
        public void OnItemClick(int position) {
            // open new intent
            Intent intent = new Intent(MainActivity.this, SongOpened.class);
            intent.putExtra("Example item", exampleList.get(position));
            startActivity(intent);
        }
    });
}

// Read songs from file and return in array list
public ArrayList<ExampleSong> createList(String fileName) {
    BufferedReader reader = null;
    try {
        ArrayList<ExampleSong> exampleList = new ArrayList<>();
        reader = new BufferedReader(
                new InputStreamReader(getAssets().open(fileName)));

        // do reading, usually loop until end of file reading
        String mLine;
        while ((mLine = reader.readLine()) != null) {
            int begin = mLine.indexOf(" ");
            int end = mLine.length();

            String num = mLine.substring(0,begin);
            String title = mLine.substring(begin+1, end);

            exampleList.add(new ExampleSong(title,num));
        }
        reader.close();
        return exampleList;
    } catch (IOException e) {
        //log the exception
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                //log the exception
            }
        }
    }

}
Basil Bourque :

No, you cannot continue to use memory after your Java app terminates. All the memory allocated to your app is reclaimed. And if your JVM is terminating as well, its memory is reclaimed.

Even if you were able to maintain data in memory, RAM is completely cleared when the computer or device is shutdown.

Writing data as a file to use later when your app runs again is exactly the purpose of persisting to storage.

In an ideal world, RAM and storage would be one and the same. But we do not live in that world. In our world, RAM is very fast but volatile and expensive while non-volatile storage is cheap but slow.

Reading a thousand short rows from local storage is very fast, imperceptible to the user. That is not a performance problem to be worried about. Beware of premature optimization.

By the way, a library such as Apache Commons CSV can help with the chore of reading/writing tab-delimited or comma-separated values text files.

If you are frequently rewriting the data, then you may want to consider using a database engine such as H2. If you are modifying the data while accessing across threads, then you should definitely be using a database engine such as H2.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400379&siteId=1