Fastest way to store char [] [] in general settings

In my application, the main dataset is a two-dimensional char ( char[][]

) array , in which some of the values ​​may be non-printable characters and even characters \0

. What would be the fastest way to store this array in shared privileges and retrieve it later? Retrieval speed is much more important than storage speed. The arrays are not particularly large, probably no more than 100x100.

I am currently converting it to a string by simply concatenating all the characters, column by column, and storing the string along with the dimensions (as int).

I've also considered just serializing an array ( writeObject

in ByteArrayOutputStreram

and then using the stream method toString

), but haven't tried it yet.

Any other suggestions? Again, the fastest possible retrieval (and rest as a char [] [] array) is my main problem.

+3


source to share


2 answers


Since the methods StringSet

(put and get) are only available on Android 3.0, and also because I found preferences to be less reliable when storing long strings, especially those containing 0 characters, I use a different way of storing data in the application.

I use the internal files ( fileGetInput

and fileGetOutput

) then creates HashMap<Integer, char[][]>

and writes it to a file using writeObject

. Since I have several of these char arrays identified by an integer id, this way I store them all in one go.



I understand that I might lose something in terms of performance, but in this case, reliability comes first.

0


source


I have sent you a method that uses a lot of native functions, and so is most likely fast. Keep in mind that this is untested and should only be used as inspirational.



public void save(char[][] chars) {
    Set<String> strings = new LinkedHashSet<String>(chars.length);

    for(int i = 0, len = chars.length; i < len; i++) {
        strings[i] = new String(chars[i]);
    }

    getSharedPreferences().edit().putStringSet("data", strings).commit();
}

public char[][] read() {
    Set<String> strings = getSharedPreferences().getStringSet("data", new LinkedHashSet<String>());

    char[][] chars = new char[strings.size][];
    int i = 0;

    for(String line : strings) {
        chars[i++] = line.toCharArray();
    }

    return chars;
}

      

0


source







All Articles