开发者

Database vs List

开发者 https://www.devze.com 2023-04-01 03:17 出处:网络
I am developing an android app.. 开发者_运维技巧I want to have a storage of about 1000 string.. I started using mysql but found it difficult so I am using Lists now..Will that be too much of a differe

I am developing an android app.. 开发者_运维技巧I want to have a storage of about 1000 string.. I started using mysql but found it difficult so I am using Lists now..Will that be too much of a difference or is it okay?


SQLLite is an ok solution, but it will produce a larger data footprint (your app will take up more space). The problem you will find with SQLLite is that you will need to do one of the following:

1.) On startup, manually read in your list of values to your SQLlite data base on the first run to set it up and get it running for subsequent use (slow) just so you can have the data set up and available

2.) Include a copy of your SQLlite database as a file in the application install that is already pre-populated with the values (a bit annoying to figure out how to do this in Android). There are several tutorial online about how to do this.

If all you are doing is using a list of 1,000 strings and looking up a string, you might want to use a .CSV and read it into a HashMap. A HashMap is much faster and easier to use than SQLLite in Android (and smaller). You can also serialize and deserialize the hashmap to and from disk and include it as a pre-serialized object in the Android assets folder to speed things along. 1,000 strings is not a lot of data to search if that is all you are doing.

*EXTRA CREDIT*

Here is a lovely class that does all the serialization and deserialization to and from disk for you:

package com.example;

import android.app.Activity;
 android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.TextView;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class MyActivity extends Activity
{
public  final String DICTIONARY_FILE_NAME = "dictionarys.ser";
private TextView myLabel;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    writeToFile();
    //writeToSDCard();

   /*  */

     readFile();
}

private void readFile()
{
    try
    {
        Map data = null; //object to be deserialized
        InputStream is = null;
        ObjectInputStream ois=null;
        AssetManager assets = getAssets();
        is = assets.open(DICTIONARY_FILE_NAME);
        ois = new ObjectInputStream(is);
        data = (Map) ois.readObject();
        ois.close();
    }
    catch (Exception e){
        Log.v(e.getMessage(), "message");
    }

}

private void writeToFile()
{
         //read the csv into the new database
     //this requires there to be a dictionary.csv file in the raw directory
    InputStream inputStream = getResources().openRawResource(R.raw.dictionary);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    try
    {
       String word;//word
       int primaryKey = 0;//primary key
       Map dictionaryHash = new HashMap();

       while ((word = reader.readLine()) != null)
       {
           dictionaryHash.put(primaryKey,word );
           primaryKey++;

           if(primaryKey % 1000 == 0)
           {
               Log.v("Percent load completed ", " " + primaryKey);
               myLabel.setText("Percent load completed " + primaryKey);
           }
       }

        //write the dictionary to a file
        File file = new File(DICTIONARY_FILE_NAME);
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(getFilesDir() +"/"+ DICTIONARY_FILE_NAME));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dictionaryHash);
        oos.flush();
        oos.close();


        //FileInputStream fis = new FileInputStream(getFilesDir() +"/"+DICTIONARY_FILE_NAME);
        //ObjectInputStream ois = new ObjectInputStream(fis);
        //Map dictionaryMap = (Map) ois.readObject();

        //ois.close();

   }
   catch (Exception ex) {
       // handle exception
       Log.v(ex.getMessage(), "message");
   }
   finally
    {
       try
       {
           inputStream.close();

       }
       catch (IOException e) {
           // handle exception
          Log.v(e.getMessage(), "message");
       }
   }

}

private void writeToSDCard()
{
    //this requires there to be a dictionary.csv file in the raw directory
    InputStream inputStream = getResources().openRawResource(R.raw.dictionary);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    try
    {
       String word;//word
       int primaryKey = 0;//primary key
       Map dictionaryHash = new HashMap();

       while ((word = reader.readLine()) != null)
       {
           if(word.length() < 7)
           {
               dictionaryHash.put(primaryKey,word );
               primaryKey++;



               if(primaryKey % 1000 == 0)
                   Log.v("Percent load completed ", " " + primaryKey);
           }
       }

        //write the dictionary to a file
        File file = new File(DICTIONARY_FILE_NAME);
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory() +"/"+ DICTIONARY_FILE_NAME));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dictionaryHash);
        oos.flush();
        oos.close();
                 Log.v("alldone","done");
          /*
        FileInputStream fis = new FileInputStream(getFilesDir() +"/"+DICTIONARY_FILE_NAME);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Map dictionaryMap = (Map) ois.readObject();
        ois.close();  */

   }
   catch (Exception ex) {
       // handle exception
       Log.v(ex.getMessage(), "message");
   }
   finally
    {
       try
       {
           inputStream.close();

       }
       catch (IOException e) {
           // handle exception
          Log.v(e.getMessage(), "message");
       }
   }
}

}


Don't pre-optimize. (see also Premature Optimization)

Until you know, or can reasonably estimate, that a method of storage is going to be "too slow", don't worry about it. Get the application working then see if the area you were concerned about is really a bottleneck.

1000 Strings really isn't very much (unless each string is the length of a Dostoyevsky book).

If you already know the strings at compile time (sounds like it), you could load them into an arrays.xml file which gets included in your application at build time.


That entirely depends on how you intend on using them, and what type of list you're using. If these are things which need to be in memory constantly, for example, then it makes more sense to have them as a list. If these are things which you need to actually search through, then you probably want a database.

As an aside, SQLLite is faster, but SQL being SQL, it has a lot of the same syntax as MySQL.

As another aside: be careful which list you are using. LinkedList takes a long time to look up entities at the end of the List, while ArrayList does occasionally have extra overhead in insertion. Of course, if you're able to store everything to be referenced by constants, then you might want to consider a HashMap or HashTable -- a HashMap is pretty easy to understand.


You could even use a string array, and store the data in a resource.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号