//1.Stream处理普通数据类型
public static void main(String[] args) {
Random random = new Random();
//1测试基本类型的数据;整数
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000000; i++) {
list.add(random.nextInt(Integer.MAX_VALUE));
}
//一.简单数据类型的stream操作
//1.stream
testStream(list);
//2.parallelStream并行
testparallelStream(list);
//3.普通for
testfor(list);
//4.增强for
testStrongfor(list);
//5.迭代器
testIterator(list);
}
//流操作
public static void testStream(List<Integer> list) {
long start = System.currentTimeMillis();
//获取最大值
Optional<Integer> optional = list.stream().max(Integer::compare);
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("stream消耗" + (end - start) + "ms");
}
//并行stream
public static void testparallelStream(List<Integer> list) {
long start = System.currentTimeMillis();
//获取最大值
Optional<Integer> optional = list.parallelStream().max(Integer::compare);
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("并行stream消耗" + (end - start) + "ms");
}
//普通for循环
public static void testfor(List<Integer> list) {
long start = System.currentTimeMillis();
//获取最大值
int max = Integer.MAX_VALUE;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) > max) {
max = list.get(i);
}
}
System.out.println(max);
long end = System.currentTimeMillis();
System.out.println("普通for循环消耗" + (end - start) + "ms");
}
//增强for循环foreach
public static void testStrongfor(List<Integer> list) {
long start = System.currentTimeMillis();
//获取最大值
int max = Integer.MAX_VALUE;
for (Integer integer : list) {
if (integer > max) {
max = integer;
}
}
System.out.println(max);
long end = System.currentTimeMillis();
System.out.println("增强for循环foreach" + (end - start) + "ms");
}
//迭代器
public static void testIterator(List<Integer> list) {
long start = System.currentTimeMillis();
//获取最大值
Iterator<Integer> it = list.iterator();
int max = it.next();
while (it.hasNext()) {
int current = it.next();
if (current > max) {
max = current;
}
}
System.out.println(max);
long end = System.currentTimeMillis();
System.out.println("迭代器消耗" + (end - start) + "ms");
}
//比较复杂的数据Steam中 pararallelStream和普通for循环性能基本接近
public static void main(String[] args) {
//生成复杂数据列表
Random random = new Random();
//2.复杂类型数据类型,对象
List<Product> list = new ArrayList<Product>();
for (int i = 0; i < 1000000; i++) {
list.add(new Product("pro" + i, i, random.nextInt(Integer.MAX_VALUE)));
}
//1.stream
testStream(list);
//2.并行stream
testParallelStream(list);
//3.普通For循环
testfor(list);
//4.增强for循环
testStrongerfor(list);
//5.iterator
testIterator(list);
}
//stream
public static void testStream(List<Product> list) {
long start = System.currentTimeMillis();
//获取最大值
Optional<Product> optional = list.stream().max((x, y) -> x.getHot() - y.getHot());
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("stream" + (end - start) + "ms");
}
//并行stream
public static void testParallelStream(List<Product> list) {
long start = System.currentTimeMillis();
//获取最大值
Optional<Product> optional = list.parallelStream().max((x, y) -> x.getHot() - y.getHot());
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("并行stream" + (end - start) + "ms");
}
//普通for循环
public static void testfor(List<Product> list) {
long start = System.currentTimeMillis();
//获取最大值
Product maxHot = list.get(0);
for (int i = 0; i < list.size(); i++) {
Product current = list.get(i);
if (current.getHot() > maxHot.getHot()) {
maxHot = current;
}
System.out.println(maxHot);
}
long end = System.currentTimeMillis();
System.out.println("普通for循环消耗" + (end - start) + "ms");
}
//增强for循环
public static void testStrongerfor(List<Product> list) {
long start = System.currentTimeMillis();
//获取最大值
Product maxHot = list.get(0);
for (Product product : list) {
if (maxHot.getHot() < product.getHot()) {
maxHot = product;
}
}
System.out.println(maxHot);
long end = System.currentTimeMillis();
System.out.println("增强for循环消耗" + (end - start) + "ms");
}
//iterator
public static void testIterator(List<Product> list) {
long start = System.currentTimeMillis();
//获取最大值
Iterator<Product> it = list.iterator();
Product maxHot = it.next();
while (it.hasNext()) {
Product current = it.next();
if (current.getHot() > maxHot.getHot()) {
maxHot = current;
}
}
System.out.println(maxHot);
long end = System.currentTimeMillis();
System.out.println("iterator消耗" + (end - start) + "ms");
}