不可思議的結果

Chapter類

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlValue;

/**
 * 章
 * @author xiaofanku
 */
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class Chapter implements Serializable{
    /**
     * 章的ID
     */
    @XmlAttribute
    @XmlID
    private String chapterId;
    /**
     * 顯示順序
     */
    @XmlAttribute
    private int order;
    /**
     * 標題
     */
    @XmlValue
    private String title;
    /**
     * 作者
     */
    @XmlAttribute
    private String author;

    //ETC set/get
}

目標:嘗試從Collection取符合條件的某一個對象,填充數據

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //Lexicon l=new Lexicon();
        List<Chapter> csl=new ArrayList<>();

        Chapter c1=new Chapter();
        c1.setTitle("the 1 title item");
        c1.setChapterId("123");
        csl.add(c1);

        for(int i=10;i<60;i++){
            Chapter _tmp=new Chapter();
            _tmp.setTitle(StringHelper.randomCharacter(10));
            _tmp.setChapterId(i+"");
            csl.add(_tmp);
            _tmp=null;
        }

        Chapter c2=new Chapter();
        c2.setTitle("the 2 title item");
        c2.setChapterId("123456");
        csl.add(c2);

        for(int i=61;i<151;i++){
            Chapter _tmp=new Chapter();
            _tmp.setTitle(StringHelper.randomCharacter(11));
            _tmp.setChapterId(i+"");
            csl.add(_tmp);
            _tmp=null;
        }
        Chapter c3=new Chapter();
        c3.setTitle("the 3 title item");
        c3.setChapterId("789");
        csl.add(c3);
    }

使用以下三種方法來完成目標,
A) 原生的增強for

    public static Chapter getOneById(List<Chapter> rs,String chapterId){
        Chapter c=null;
        for(Chapter chap:rs){
            if(chap.getChapterId().equalsIgnoreCase(chapterId)){
                c=chap;
                break;
            }
        }
        return c;
    }

B) Apache common collection4

    public static Chapter getOneByIdUsedCommonsCollection4(final List<Chapter> rs,final String chapterId){
        return CollectionUtils.find(rs, new org.apache.commons.collections4.Predicate<Chapter>(){
            @Override
            public boolean evaluate(Chapter t) {
                return t.getChapterId().equalsIgnoreCase(chapterId);
            }

        });
    }

C) Jdk8的stream流

    public static Chapter getOneByIdUsedJdk8(final List<Chapter> rs,final String chapterId){
        Optional<Chapter> data=rs.stream().filter(new java.util.function.Predicate<Chapter>(){
            @Override
            public boolean test(Chapter t) {
                return t.getChapterId().equalsIgnoreCase(chapterId);
            }
        }).findFirst();
        return data.get();
    }

下面是我的配置
Intel E3 + 16G + 機械硬盤

下面是我的環境
Win10 + JDK8 + NetBeans8.1

下面是測試示例結果

        long beginTime = System.nanoTime();
        getOneById(csl,"789");//search element uesed Second:0.000207
        //getOneByIdUsedCommonsCollection4(csl,"789");//search element uesed Second:0.003840
        //getOneByIdUsedJdk8(csl,"789");//search element uesed Second:0.069668
        long endTime = System.nanoTime();
        System.out.printf("search element uesed Second:%f%n",(endTime-beginTime)/1.0e9);
發佈了123 篇原創文章 · 獲贊 10 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章