1. 程式人生 > >SearchView+RecyclerView+GreenDao的搜尋功能實現(2)

SearchView+RecyclerView+GreenDao的搜尋功能實現(2)

簡單的介面效果如下

①開啟搜尋框顯示的是歷史和熱詞,歷史可刪除 ②點選某一項,顯示搜尋的單詞和結果

③單詞聯想

佈局如下,通過一個RecyclerView控制顯示三種情況的顯示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="@color/material_primary" android:theme="@style/ToolBarStyle" app:popupTheme="@style/Theme.AppCompat.Light"> </android.support.v7.widget.Toolbar> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_below="@+id/toolbar" android:background="@color/material_icons" android:scrollbars="none"/> <TextView android:id="@+id/tv_tip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:textColor="@color/material_red_a700" android:text="Open The SearchView" android:layout_centerInParent="true"/> </RelativeLayout>

①介面初始化,簡單設定下Toolbar,RecyclerView ,
mHistoryOrAllSearchList用於存放歷史或聯想詞;mHotSearchList用於存放熱詞
SharedPreUtil.readString(Constant.OPENTIME).isEmpty()用來模擬版本變化更新資料庫
更新資料庫會將上版本的熱詞和聯想詞刪除,涉及到大資料量,所以開執行緒操作mKeywordsTableDao.getSession().runInTx

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar == null) {
            return;
        }
        mToolbar.setTitle(R.string.app_name);
        setSupportActionBar(mToolbar);

        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        StaggeredGridLayoutManager mManager = new StaggeredGridLayoutManager(2, 
                                                StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(5));

        mAdapter = new SearchAdapter(this);
        mAdapter.setItemClickLitener(this);

        mRecyclerView.setAdapter(mAdapter);

        mHistoryOrAssociativeSearchList = new ArrayList<>();
        mHotSearchList = new ArrayList<>();

        mKeywordsTableDao = getSearchKeywordsTableDao(mKeywordsTableDao);

        if (SharedPreUtil.readString(Constant.OPENTIME).isEmpty()) {
            // 詞庫版本有更新才去更新資料庫的熱詞和練習詞
            final List<SearchKeywords> keywordses = new ArrayList<>();

            // 熱詞
            keywordses.add(new SearchKeywords("women", 12, -1, Constant.SEARCH_KEYWORDS_HOT));
            keywordses.add(new SearchKeywords("men", 10, -1, Constant.SEARCH_KEYWORDS_HOT));
            keywordses.add(new SearchKeywords("beauty", 16, -1, Constant.SEARCH_KEYWORDS_HOT));
            keywordses.add(new SearchKeywords("makeup", 2, -1, Constant.SEARCH_KEYWORDS_HOT));
            keywordses.add(new SearchKeywords("clothes", 6, -1, Constant.SEARCH_KEYWORDS_HOT));

            // 聯想詞
            keywordses.add(new SearchKeywords("women", 12, -1, 
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));
            keywordses.add(new SearchKeywords("men", 10, -1, 
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));
            keywordses.add(new SearchKeywords("beauty", 16, -1, 
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));
            keywordses.add(new SearchKeywords("makeup", 2, -1,   
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));
            keywordses.add(new SearchKeywords("clothes", 6, -1, 
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));
            keywordses.add(new SearchKeywords("cute", 26, -1, 
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));
            keywordses.add(new SearchKeywords("watch", 32, -1, 
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));
            keywordses.add(new SearchKeywords("sexy", 16, -1, 
                            Constant.SEARCH_KEYWORDS_ASSOCIATIVE));

            mKeywordsTableDao.getSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    // 避免資料太多阻塞主執行緒
                    // 清除舊版本的熱詞和聯想詞
                    clearTopAndAssociativeSearchKeyWords();
                    insertSearchKeywordsToDb(keywordses);
                }
            });

            SharedPreUtil.writeString(Constant.OPENTIME, System.currentTimeMillis() + "");
        }

    }

②RecyclerView介面卡的設計

會有三種檢視模式,暴露方法獲取當前模式和設定當前模式

    // 歷史和熱詞模式
    public static final int MODE_KEYWORDS_REVIEW = 0;
    // 搜尋結果模式
    public static final int MODE_RESULT = 1;
    // 聯想匹配模式
    public static final int MODE_KEYWORDS_MATCH = 2;

    public int getCurrentMode() {
        return mCurrentMode;
    }

    public void setCurrentMode(int currentMode) {
        this.mCurrentMode = currentMode;
    }

    private int mCurrentMode;

填充資料有兩個成員變數

    /**
     * 搜尋熱詞
     */
    private List<String> mHotSearchList;

    public List<String> getHotSearchList() {
        return mHotSearchList;
    }

    public void setHotSearchList(List<String> allSearchList) {
        this.mHotSearchList = allSearchList;
    }

    /**
     * 搜尋歷史,或者是匹配的單詞
     */
    private List<String> mHistoryOrAssociativeSearchList;

    public List<String> getHistoryOrAssociativeSearchList() {
        return mHistoryOrAssociativeSearchList;
    }

    public void setHistoryOrAssociativeSearchList(List<String> historyOrAssociativeSearchList) {
        this.mHistoryOrAssociativeSearchList = historyOrAssociativeSearchList;
    }

有四種View型別:
①VIEW_TYPE_TIP ,標題,在顯示歷史和熱搜的時候為標識型別;顯示搜尋結果的時候標識搜尋結果的搜尋單詞
②VIEW_TYPE_KEYWORD ,搜尋單詞
③VIEW_TYPE_RESULT ,搜尋結果
④VIEW_TYPE_DELETE_HISTORY,一鍵清空的佈局

    private final int VIEW_TYPE_TIP = 0;
    private final int VIEW_TYPE_KEYWORD = 1;
    private final int VIEW_TYPE_RESULT = 2;
    private final int VIEW_TYPE_DELETE_HISTORY = 3;

    @Override
    public SearchAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerViewHolder viewHolder;
        switch (viewType) {
            case VIEW_TYPE_TIP:
                viewHolder = new RecyclerViewHolder
                (LayoutInflater.from(mContext).inflate(R.layout.item_search_head, parent, false));
                return viewHolder;
            case VIEW_TYPE_KEYWORD:
                View view LayoutInflater.from(mContext).inflate(R.layout.item_search, 
                parent, false);
                viewHolder = new RecyclerViewHolder(view);
                view.findViewById(R.id.iv_delete).setOnClickListener(this);
                view.findViewById(R.id.ll_search).setOnClickListener(this);
                return viewHolder;
            case VIEW_TYPE_RESULT:
                viewHolder = new RecyclerViewHolder
               (LayoutInflater.from(mContext).inflate(R.layout.item_search_result, parent, false));
                return viewHolder;
            case VIEW_TYPE_DELETE_HISTORY:
                View view1 = 
                LayoutInflater.from(mContext).inflate(R.layout.item_search_delete_history, parent, 
                false);
                viewHolder = new RecyclerViewHolder(view1);
                view1.findViewById(R.id.tv_clear_history).setOnClickListener(this);
                return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(SearchAdapter.RecyclerViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_TIP:
                StaggeredGridLayoutManager.LayoutParams lp = 
                (StaggeredGridLayoutManager.LayoutParams) holder.mHeadLayout.getLayoutParams();
                lp.setFullSpan(true);
                if (mCurrentMode != MODE_RESULT) {
                // 歷史和熱詞模式,位置不為0,說明同時存在歷史和熱搜,標配自然是“Hot”,如果等於0,
                    holder.mTitleText.setText(position != 0 ? "Hot" : mHistoryExist ? 
                    "History" : "Hot");
                } else {
                // 結果模式,搜尋單詞
                    holder.mTitleText.setText("word: " + mSearchWord);
                }
                break;

            case VIEW_TYPE_DELETE_HISTORY:
                // 不用做任何事,只是一個點選事件清空歷史
                StaggeredGridLayoutManager.LayoutParams lp3 = 
                (StaggeredGridLayoutManager.LayoutParams) ((ViewGroup) 
                holder.mClearHistoryText.getParent()).getLayoutParams();
                lp3.setFullSpan(true);
                break;

            case VIEW_TYPE_KEYWORD:

                StaggeredGridLayoutManager.LayoutParams lp1 = 
                (StaggeredGridLayoutManager.LayoutParams) ((ViewGroup) 
                holder.mSearchKeyWordText.getParent()).getLayoutParams();
                lp1.setFullSpan(true);

                if (mCurrentMode == MODE_KEYWORDS_MATCH) {
                // 聯想匹配模式
                    if (mHistoryExist) {
                        holder.mDeleteImg.setVisibility(View.GONE);
        holder.mSearchKeyWordText.setText(mHistoryOrAssociativeSearchList.get(position));
                        holder.mSearchLayout.setTag(position);
                    }
                } else if (mCurrentMode == MODE_KEYWORDS_REVIEW) {
                // 歷史和熱搜模式
                    if (mHistoryExist) {
                    // 存在歷史資料
                        if (position < mHistoryOrAssociativeSearchList.size() + 1) { 
                        // 刪除按鈕設定為可見,歷史可以刪除                    
                        holder.mSearchKeyWordText
                        .setText(mHistoryOrAssociativeSearchList.get(position - 1));
                            holder.mDeleteImg.setVisibility(View.VISIBLE);
                            holder.mDeleteImg.setTag(position - 1);
                            holder.mSearchLayout.setTag(position - 1);
                        } else {
                        // 設定熱搜資料
//                            DebugLog.e("position=" + position);
                            holder.mDeleteImg.setVisibility(View.GONE);
                            holder.mSearchKeyWordText.setText(mHotSearchList.get(position - 3 - 
                            mHistoryOrAssociativeSearchList.size()));
                            holder.mSearchLayout.setTag(position - 3);
                        }
                    } else if (mHotExist) {
                    // 只存在熱搜資料
                        holder.mDeleteImg.setVisibility(View.GONE);
                        holder.mSearchKeyWordText.setText(mHotSearchList.get(position - 1));
                        holder.mSearchLayout.setTag(position - 1);
                    }
                }
                break;
            case VIEW_TYPE_RESULT:
            // 搜尋結果
                StaggeredGridLayoutManager.LayoutParams lp2 = 
                (StaggeredGridLayoutManager.LayoutParams) ((ViewGroup) 
                holder.mResultText.getParent()).getLayoutParams();
                lp2.setFullSpan(true);
                holder.mResultText.setText("The search word is " + mSearchWord);
                break;

        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mCurrentMode == MODE_KEYWORDS_REVIEW) {
            int hotTitlePosition = 0;
            if (mHistoryExist && mHotExist) {
                // 同時存在的時候才有第二個標題
                hotTitlePosition = mHistoryOrAssociativeSearchList.size() + 1 + 1;
            }
            if (mHistoryExist && position == mHistoryOrAssociativeSearchList.size() + 1) {
                // 有歷史記錄存在,歷史紀錄後面是一個一鍵情空歷史按鈕
                return VIEW_TYPE_DELETE_HISTORY;
            }
            return position == 0 || position == hotTitlePosition ? VIEW_TYPE_TIP : 
            VIEW_TYPE_KEYWORD;
        } else if (mCurrentMode == MODE_KEYWORDS_MATCH) {
            return VIEW_TYPE_KEYWORD;
        } else if (mCurrentMode == MODE_RESULT) {
            return position == 0 ? VIEW_TYPE_TIP : VIEW_TYPE_RESULT;
        }
        return -1;
    }

    boolean mHistoryExist;
    boolean mHotExist;

    @Override
    public int getItemCount() {
        int itemCount = 0;
        if (mCurrentMode == MODE_KEYWORDS_REVIEW) {
            mHistoryExist = mHistoryOrAssociativeSearchList != null && 
            mHistoryOrAssociativeSearchList.size() > 0;
            mHotExist = mHotSearchList != null && mHotSearchList.size() > 0;
            if (mHistoryExist) {
                // 存在歷史,加個歷史的標題
                itemCount += mHistoryOrAssociativeSearchList.size() + 1;
                // 加個一鍵清空歷史的按鈕
                itemCount += 1;
                if (mHotExist) {
                    // 存在熱搜,加個熱搜的標題
                    itemCount += mHotSearchList.size() + 1;
                }
            } else {
                // 不存在歷史
                if (mHotExist) {
                    // 存在熱搜,加個熱搜的標題
                    itemCount += mHotSearchList.size() + 1;
                }
            }
        } else if (mCurrentMode == MODE_KEYWORDS_MATCH) {
            mHistoryExist = mHistoryOrAssociativeSearchList != null && 
            mHistoryOrAssociativeSearchList.size() > 0;
            if (mHistoryExist) {
                itemCount += mHistoryOrAssociativeSearchList.size();
            }
        } else if (mCurrentMode == MODE_RESULT) {
            // 結果是標題加text顯示搜尋詞語
            return itemCount += 2;
        }

        return itemCount;
    }

選單的設定

    private MenuItem mSearchItem;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        mSearchItem = menu.findItem(R.id.action_search);

        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);

        final SearchView.SearchAutoComplete searchEditText = (SearchView.SearchAutoComplete) 
        searchView.findViewById(R.id.search_src_text);

        searchView.setQueryHint("Search");

        // 將搜尋按鈕放到搜尋輸入框的外邊
        searchView.setIconifiedByDefault(false);

        // 設定輸入框底部的橫線的顏色
        View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
        searchPlate.setBackgroundResource(R.mipmap.ic_searchview_plate);
      searchPlate.getBackground().setColorFilter(getResources().getColor(R.color.material_a
      ccent), PorterDuff.Mode.SRC_ATOP);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 提交查詢條件
                DebugLog.e("提交查詢:" + query);
                submitKeywords(query);
                searchEditText.setText("");
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // 這裡做搜尋聯想
                if (mAdapter.getCurrentMode() == SearchAdapter.MODE_RESULT) {
                    DebugLog.e("搜尋結果模式,不做處理");
                    return true;
                }
                if (newText.isEmpty()) {
                    DebugLog.e("搜尋為空,顯示歷史和熱詞");
                    showHistoryAndHotKeywords();
                } else {
                    DebugLog.e("搜尋詞不為空,顯示匹配");
                    mHotSearchList.clear();
                    mHistoryOrAssociativeSearchList.clear();
                    insertMatchKeywordsToRecycleView(mHistoryOrAssociativeSearchList, newText);
                    mAdapter.setCurrentMode(SearchAdapter.MODE_KEYWORDS_MATCH);
                    mAdapter.setHistoryOrAssociativeSearchList(mHistoryOrAssociativeSearchList);
                    mAdapter.notifyDataSetChanged();
                }

                return true;
            }
        });

        return true;
    }

點選事件的處理

    /**
     * 提交結果
     *
     * @param query
     */
    private void submitKeywords(String query) {
        mAdapter.setCurrentMode(SearchAdapter.MODE_RESULT);
        final SearchKeywords keywords = new SearchKeywords(query, -1, System.currentTimeMillis(), 
        Constant.SEARCH_KEYWORDS_HISTORY);
        insertHistorySearchKeywordsToDb(keywords);
        hideKeybord(MainActivity.this, MainActivity.this.getCurrentFocus());
        mAdapter.setSearchWord(query);
        mAdapter.notifyDataSetChanged();
        MenuItemCompat.collapseActionView(mSearchItem);
    }

    /**
     * 顯示歷史和熱詞
     */
    private void showHistoryAndHotKeywords() {
        mAdapter.setCurrentMode(SearchAdapter.MODE_KEYWORDS_REVIEW);
        mHotSearchList.clear();
        mHistoryOrAssociativeSearchList.clear();
        insertKeywordsToRecyclerView(mHistoryOrAssociativeSearchList, 
        Constant.SEARCH_KEYWORDS_HISTORY);
        insertKeywordsToRecyclerView(mHotSearchList, Constant.SEARCH_KEYWORDS_HOT);
        mAdapter.setHistoryOrAssociativeSearchList(mHistoryOrAssociativeSearchList);
        mAdapter.setHotSearchList(mHotSearchList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (view.getId()) {
            case R.id.iv_delete:
                hideKeybord(MainActivity.this, MainActivity.this.getCurrentFocus());
                DebugLog.e("刪除歷史: " + mHistoryOrAssociativeSearchList.get(position));
                clearSearchKeyWords(mHistoryOrAssociativeSearchList.get(position));
                mHistoryOrAssociativeSearchList.remove(position);
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.ll_search:
                boolean historyExist = mHistoryOrAssociativeSearchList != null && 
                mHistoryOrAssociativeSearchList.size() > 0;
                boolean hotExist = mHotSearchList != null && mHotSearchList.size() > 0;
                String keyword = null;
                if (historyExist) {
                    if (position >= mHistoryOrAssociativeSearchList.size()) {
                        // 點選的是熱搜
                        keyword = mHotSearchList.get(position - 
                        mHistoryOrAssociativeSearchList.size());
                        DebugLog.e("點選的是熱搜: " + keyword);
                    } else {
                        keyword = mHistoryOrAssociativeSearchList.get(position);
                        if (mAdapter.getCurrentMode() == SearchAdapter.MODE_KEYWORDS_MATCH) {
                            DebugLog.e("點選的是聯想的匹配: " + keyword);
                        } else if (mAdapter.getCurrentMode() == SearchAdapter.MODE_KEYWORDS_REVIEW) {
                            DebugLog.e("點選的是歷史: " + keyword);
                        }
                    }
                } else if (hotExist) {
                    keyword = mHotSearchList.get(position);
                    DebugLog.e("點選的是熱搜: " + keyword);
                }
                if (keyword != null) {
                    submitKeywords(keyword);
                    MenuItemCompat.collapseActionView(mSearchItem);
                }
                break;
            case R.id.tv_clear_history:
                DebugLog.e("點選了一鍵清空歷史");
                clearHisotySearchKeyWords();
                mHistoryOrAssociativeSearchList.clear();
                mAdapter.setCurrentMode(SearchAdapter.MODE_KEYWORDS_REVIEW);
                mAdapter.notifyDataSetChanged();
                break;
        }
    }

總結:

可能說得不是很清楚,附上demo,跑一下,看下原始碼估計就差不多了>_<

相關推薦

SearchView+RecyclerView+GreenDao搜尋功能實現2

簡單的介面效果如下 佈局如下,通過一個RecyclerView控制顯示三種情況的顯示 <RelativeLayout xmlns:android="http://schema

lucene 搜尋功能介紹2

準備工作:資料,生成索引         private Integer[] ids = {1,2,3}; private String[] citys = {"beijing","shanghai","guangzh

PHP搜尋功能實現 2 匹配

整體匹配思路: 利用match與against進行全文匹配, 整體程式碼: public $max_results = 100; // 搜尋結果超過這個數值, 超過的部分將被拋棄 publi

基礎資料結構與演算法實現2—二叉搜尋樹BST

import java.util.LinkedList; import java.util.Queue; public class BST <E extends Comparable<E>> { private c

RecyclerView 拖動/滑動多選的實現2

方案三: AndroidDragSelect 前文說到,方案三就是分析了方案一的缺點之後,給出了自己的基於 OnItemTouchListener 的實現方案,耦合度低,可以很容易整合進現有的專案當中。 從自定義 RecyclerView 的方案中可以看到,

在STM32上實現NTFS之5:GPT分區表的C語言實現2GPT實現以及統一方式讀取磁盤分區

tfs 下載 數據 特殊 dpt 屬性列表 handle 系統分區 成了   上一節實現了主GPT頭的信息提取,這一節繼續提取整個的GPT數據,並且將GPT分區表和MBR分區表兩種格式融合成一個模塊,使主調函數(也可以說是使用者)不需要關心磁盤的分區表類型:它太底層了,確實

Spring AOP高級——源碼實現2Spring AOP中通知器Advisor與切面Aspect

color oaf 小麻煩 ntc tro sta ins pack package 本文例子完整源碼地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AO

智能指針原理及實現2- unique_ptr

unique clas 結束 基礎 無法 body 智能指針 周期 文件 只允許基礎指針的一個所有者。 可以移到新所有者(具有移動語義),但不會復制或共享(即我們無法得到指向同一個對象的兩個unique_ptr)。 替換已棄用的 auto_ptr。 相較於 boost::s

【轉】Verilog學習筆記簡單功能實現...............異步FIFO

另一個 gif 多個 可靠 基本原理 drs bar next 不同 基本原理: 1.讀寫指針的工作原理   寫指針:總是指向下一個將要被寫入的單元,復位時,指向第1個單元(編號為0)。   讀指針:總是指向當前要被讀出的數據,復位時,指向第1個單元(編號為0)

軟件工程—WC功能實現 JAVA

要求 目錄 arsc 主類 read 準備 dsc row 源文件 軟件工程—WC功能實現(JAVA) Github項目地址:https://github.com/Ousyoung/wc 項目要求 ? wc.exe 是一個常見的工具,它能統計文本文件的字符數、單詞數和行數。

SpringBoot使用WebSocket實現服務端推送--叢集實現2

書接上文,本文介紹了一種實現叢集管理和訊息傳送方式。 在叢集模式情況下,一般是Nginx反向代理到多臺Tomcat或者SLB代理到多臺Tomcat的方式,怎麼實現給某個人推送訊息?比如WebSocket1連線到Tomcat1,但是在Tomcat2需要給WebSocket1傳送訊息,怎麼辦?一

熟練使用Lua面向物件:基於table的面向物件實現2

myluaootest.lua –1. 基本原理 local Cal = {} function Cal:New(o) o = o or {} setmetatable(o, self) self.__index = self return o end functio

平衡二叉搜尋實現go

/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func sorted

第十二週專案3 - 圖遍歷演算法實現2

/*Copyright (c) 2015, 煙臺大學計算機與控制工程學院 * All rights reserved. * 檔名稱:H1.cpp * 作者:辛志勐 * 完成日期:2015年11月23日 * 版本號:VC6.0 * 問題描述:廣度優先遍歷——BFS * 輸入描述:無 * 程式輸出:圖的

負載均衡演算法---Java簡單實現2

上一篇介紹了負載均衡的輪詢,隨機,跟hash演算法,這邊我們一起了解下,加權的輪詢以及加權的隨機。其實理解好了輪詢跟隨機演算法,再加權的話其實是差不多的。 看面通過程式碼來了解: (1)為了不重複建立一個server列表,我們先建立一個共有的server列表,如下: pu

lucene 搜尋功能介紹1

首先使用搜索功能前需要先建立索引: /** * 建立索引 * @author 王晨 * */public class Indexer { private IndexWriter writer;   //寫索引例項 /** * 構造方

2.遺傳演算法matlab實現2:再加例項兩個一元二元完整作圖,二維圖形,三維圖形以及進化過程圖

(1)直接在命令視窗輸入以下程式碼: figure(1); hold on; lb=1;ub=2; %函式自變數範圍[1,2] ezplot('sin(10*pi*X)/X',[lb,ub]);

JavaScript精華筆記:ES5陣列新增函式的原始碼實現2

本系列文章中,對forEach、filter、map、every、some、reduce和reduceRight等函式,講述瞭如何自己編寫程式碼實現它們的功能。 通過閱讀原始碼,自己編寫原始碼,能瞭解編寫思想、熟悉設計模式,能鍛鍊自己編寫元件、框架的能力。 接上篇文章內容,這裡繼續討論如

java簡單動畫效果的實現2

簡單的java隨機數 java兩種隨機數生成方式(均為偽隨機): 1.Math.random() 生成0-1之間的隨機數,取0不取1; 2. Random ra=new Random(x); int result=ra.nextInt(y); x是種子,給的

b樹的實現2---java版程式碼

原文地址: http://blog.csdn.net/cdnight/article/details/10619599 [java] view plain copy print? 感覺上,b樹的插入及刪除操作都不如RB樹複雜。當年插紅黑樹的各種操作解釋文章都