An LRU implementation for recording the latest search history

Posted by gazza52 on Fri, 01 May 2020 02:12:24 +0200

For many functions with search requirements, it is generally necessary to display the latest n historical search records, mainly including the following function points:
  • The most recent search items are placed first, and the earliest search records are placed last.
  • Only record the latest n data. If there are more than n search records, delete the record with the longest search time.
  • There is no duplicate search term. If the new search term already exists, the keyword will be brought to the front and the original location keyword will be deleted.
  • It can be easily persistent and recover the original record history by reading the persistent data.

Based on the above conditions, it is not difficult to see that this is an LRU stack without duplicate data. Originally, I thought that the java collection would support the implementation of this requirement. I tried the stack and other collection types and found that they are not easy to handle. Finally, I would like to make a list, which is simple and convenient.

Android:
public class SearchHistoryUtil {
    private LruStackUtil mLruStack = null;

    public SearchHistoryUtil(int maxSize) {
        this.mLruStack = new LruStackUtil(maxSize);
    }

    public void updateSearchHistorys(Context context, String keyWord) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("music_search_history",
                Activity.MODE_PRIVATE);
        String mKeys = sharedPreferences.getString("keys", "");
        mLruStack.reset();
        SharedPreferences.Editor editor = sharedPreferences.edit();

        String[] tmpHistory = mKeys.split(",");
        for (String i : tmpHistory) {
            mLruStack.push(i);
        }
        mLruStack.pushHead(keyWord);
        editor.putString("keys", mLruStack.getAll());
        editor.apply();
    }

    public static String getAllHistorys(Context context) {
        SharedPreferences sharedPreferences= context.getSharedPreferences("music_search_history",
                Activity.MODE_PRIVATE);
        String mKeys = sharedPreferences.getString("keys", "");
        return mKeys;
    }

    public static void clearAll(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("music_search_history",
                Activity.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();
    }
}

public class LruStackUtil {
    ArrayList<String> stack = null;
    private int size = 0;

    public LruStackUtil(int size) {
        this.stack = new ArrayList<String>();
        this.size = size;
    }

    public void pushHead(String keyWord) {
        if (stack.remove(keyWord)) {
            stack.add(0, keyWord);
            return;
        }

        if (stack.size() > this.size - 1) {
            stack.remove(stack.size() - 1);
            stack.add(0, keyWord);
        } else {
            stack.add(0, keyWord);
        }
    }

    public void push(String keyWord) {
        if (stack.contains(keyWord)){
            return;
        }

        if (stack.size() > this.size - 1) {
            return;
        } else {
            stack.add(keyWord);
        }
    }

    public String getAll() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < stack.size(); i++) {
            str.append(stack.get(i) + ",");
        }

        return str.toString();
    }

    public void reset() {
        if (stack != null) {
            stack.clear();
        }
    }
}

In fact, there is no technical difficulty in this implementation, just try to modularize the modified function, interface and make it easy to call.

Topics: Java Android