Stream性能的简单测试

java8-Stream性能的一个简单测试
生成两个List集合,对第一个集合取出50-60之间的数,再在这些数中取出前三个,通过实体类封装,装入集合。对第二个集合取出60-70之间的数,再在这些数中,取出出前两个外的所有数,也通过实体类封装进同一个集合中
实体类代码:

/**
 * @Author xlyu
 * @Date 2019/9/26
 * @Description
 */
package com.ces.java8.stream;

import java.util.Objects;

/**
 * @program: ces
 * @description:
 * @author: xlYu
 * @create: 2019-09-26 17:05
 **/
public class IntegerEntity {
    @Override
    public String toString() {
        return "IntegerEntity{" +
                "integer=" + integer +
                '}';
    }

    private Integer integer;

    public IntegerEntity(){}

    public IntegerEntity(Integer integer) {
        this.integer = integer;
    }

    public Integer getInteger() {
        return integer;
    }

    public void setInteger(Integer integer) {
        this.integer = integer;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        IntegerEntity that = (IntegerEntity) o;
        return Objects.equals(integer, that.integer);
    }

    @Override
    public int hashCode() {
        return Objects.hash(integer);
    }
}

测试代码:

/**
 * @Author xlyu
 * @Date 2019/9/26
 * @Description
 */
package com.ces.java8.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @program: ces
 * @description: 
 * @author: xlYu
 * @create: 2019-09-26 17:08
 **/
public class TraditionalAndStream {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        int forNumber = 50;
        for (int i = 0; i < forNumber; i++) {
            list1.add((int) (Math.random() * 100 + 1));
            list2.add((int) (Math.random() * 100 + 1));
        }
        traditionMethod(list1, list2);
        streamMethod(list1, list2);

    }

    private static void traditionMethod(List<Integer> list1, List<Integer> list2) {
        long start = System.currentTimeMillis();
        //50-60
        List<Integer> listOne = new ArrayList<>();
        for (Integer integer : list1) {
            if (integer >= 50 && integer < 60) {
                listOne.add(integer);
            }
        }
        //前三个
        List<Integer> listOneByTwo = new ArrayList<>();
        for (int i = 0; i < listOne.size(); i++) {
            if (i < 3) {
                listOneByTwo.add(listOne.get(i));
            }
        }
        //60-70
        List<Integer> listTwo = new ArrayList<>();
        for (Integer integer : list2) {
            if (integer >= 60 && integer <= 70) {
                listTwo.add(integer);
            }
        }
        //不要前两个
        List<Integer> listTwoByTwo = new ArrayList<>();
        for (int i = 0; i < listTwo.size(); i++) {
            if (i > 1) {
                listTwoByTwo.add(listTwo.get(i));
            }
        }
        //IntegerEntity封装
        List<IntegerEntity> generate = new ArrayList<>();
        for (int i = 0; i < listOneByTwo.size(); i++) {
            generate.add(new IntegerEntity(listOneByTwo.get(i)));
        }
        for (int i = 0; i < listTwoByTwo.size(); i++) {
            generate.add(new IntegerEntity(listTwoByTwo.get(i)));
        }
        System.out.println(generate);
        long end = System.currentTimeMillis();
        System.out.println(end - start);

    }

    private static void streamMethod(List<Integer> list1, List<Integer> list2) {
        long start = System.currentTimeMillis();
        //50-60
        //前三个
        Stream<Integer> stream1 = list1.stream().filter(number -> number >= 50).filter(number -> number < 60).limit(3);
        //60-70
        //不要前两个
        Stream<Integer> stream2 = list2.stream().filter(number -> number >= 60).filter(number -> number <= 70).skip(2);
        //IntegerEntity封装
        Stream<Integer> concat = Stream.concat(stream1, stream2);
        List<IntegerEntity> generate = new ArrayList<>();
        concat.forEach((number) -> generate.add(new IntegerEntity(number)));
        System.out.println(generate);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

运行结果:
Stream性能的简单测试_第1张图片
结论:Stream的使用很多程度上简化开发流程,但是效率不见得比传统的处理方式要高

你可能感兴趣的:(idea,java)