Shows how to search for historical LRU implementations and how to search for lru

Source: Internet
Author: User

Shows how to search for historical LRU implementations and how to search for lru
For many functions that require search, you need to display the last n historical search records. The main features are as follows, the earliest search records are placed at the end. Only the last n data records are recorded. If more than n search records exist, the records with the longest search time are deleted. There are no repeated search items. If the new search keyword already exists, the keyword is mentioned at the beginning and the original location keyword is deleted. It is convenient for persistence, and can restore the original record history by reading persistent data.

Based on the above conditions, it is not difficult to see that this is an LRU stack with no duplicate data. I thought the java Collection would support the implementation of this requirement. I tried Collection types such as stack and found it was not very good, finally, let's use list to make it 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
 
   stack = null;    private int size = 0;    public LruStackUtil(int size) {        this.stack = new ArrayList
  
   ();        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, this implementation has no technical difficulty at all. It just tries its best to modularize and interface the functions to facilitate calls.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.