LinkedList

package com.apitest;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class LinkedListStreamDemo {
    public static void main(String[] args) {
        int size = 1 << 16;


        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < size; i++) {
            linkedList.add(i);
        }

        /** * 102 milliseconds */
        long before2 = new Date().getTime();
        List<Integer> list2 = linkedList.parallelStream().filter(i -> i % 2! =0).collect(Collectors.toList());
        long after2 = new Date().getTime();
        System.out.println(after2-before2);
        for (int i: list2) {
            System.out.printf("%d,", i);
        }
        System.out.println();


        /** * 122 milliseconds */
// long before = new Date().getTime();
// List
      
        list = linkedList.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
      
// long after = new Date().getTime();
// System.out.println(after-before);
// for (int i: list) {
// System.out.printf("%d,", i);
/ /}
// System.out.println();
//
        /** * 1216 ms */
// long before1 = new Date().getTime();
// LinkedList
      
        list1 = new LinkedList<>();
      
// for (int i:linkedList) {
// if(i % 2 == 0){
// list1.add(linkedList.get(i));
/ /}
/ /}
// long after1 = new Date().getTime();
// System.out.println(after1-before1);
// for (int i: list1) {
// System.out.printf("%d,", i);
/ /}
// System.out.println();}}Copy the code

ArrayList

package com.apitest;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayListStreamDemo {
    public static void main(String[] args) {
        int size = 1 << 18;


        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            arrayList.add(i);
        }
        /** * 122 milliseconds */
        long before = new Date().getTime();
        List<Integer> list = arrayList.stream().filter(i -> i % 2= =0).collect(Collectors.toList());
        long after = new Date().getTime();
        /** * 20ms */
        long before2 = new Date().getTime();
        List<Integer> list2 = arrayList.parallelStream().filter(i -> i % 2! =0).collect(Collectors.toList());
        long after2 = new Date().getTime();




        /** * 18 milliseconds */
        long before1 = new Date().getTime();
        LinkedList<Integer> list1 = new LinkedList<>();
        for ( int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i) % 2= =0){ list1.add(arrayList.get(i)); }}long after1 = new Date().getTime();
        System.out.println();

        for (int i = 0; i < list.size(); i++) {
            System.out.printf("%d,", list.get(i));
        }
        System.out.println();

        for (int i = 0; i < list1.size(); i++) {
            System.out.printf("%d,", list1.get(i));
        }
        System.out.println();

        for (int i = 0; i < list2.size(); i++) {
            System.out.printf("%d,", list2.get(i)); } System.out.println(); System.out.println(after-before); System.out.println(after2-before2); System.out.println(after1-before1); }}Copy the code

HashMap

package com.apitest;

import java.util.*;
import java.util.stream.Collectors;

public class HashMapStreamDemo {

    public static void main(String[] args) {
        int size = 1 << 16;
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < size; i++) {
            hashMap.put(i, i);
        }

        /** * 46624 ms */
        long before = new Date().getTime();
        Set<Map.Entry<Integer, Integer>> entrySet = hashMap.entrySet().stream().filter(entry -> entry.getKey() % 2= =0).collect(Collectors.toSet());
        long after = new Date().getTime();
        System.out.println(after-before);

        /** * 44545 ms */
        long before1 = new Date().getTime();
        Set<Map.Entry<Integer, Integer>> entrySet1 = new HashSet<>();
        for (Map.Entry<Integer, Integer> entry: hashMap.entrySet()) {
            if (entry.getKey() % 2= =0) { entrySet1.add(entry); }}long after1 = newDate().getTime(); System.out.println(after1-before1); System.out.println(entrySet); System.out.println(entrySet1); }}Copy the code

TreeMap

package com.apitest;

import java.util.*;
import java.util.stream.Collectors;

public class TreeMapStreamDemo {
    public static void main(String[] args) {

        int size = 1 << 16;
        TreeMap<Integer, Integer> treeMap = new TreeMap<>();
        for (int i = 0; i < size; i++) {
            treeMap.put(i, i);
        }

        /** * 48112 ms */
        long before = new Date().getTime();
        Set<Map.Entry<Integer, Integer>> entrySet = treeMap.entrySet().stream().filter(entry -> entry.getKey() % 2= =0).collect(Collectors.toSet());
        long after = new Date().getTime();
        System.out.println(after-before);

        /** * 48210 milliseconds */
        long before1 = new Date().getTime();
        Set<Map.Entry<Integer, Integer>> entrySet1 = new HashSet<>();
        for (Map.Entry<Integer, Integer> entry: treeMap.entrySet()) {
            if (entry.getKey() % 2= =0) { entrySet1.add(entry); }}long after1 = newDate().getTime(); System.out.println(after1-before1); System.out.println(entrySet); System.out.println(entrySet1); }}Copy the code

HashSet

package com.apitest;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class HashSetStreamDemo {
    public static void main(String[] args) {
        int size = 1 << 20;
        HashSet<Integer> hashSet = new HashSet<>();
        for (int i = 0; i < size; i++) {
            hashSet.add(i);
        }

        /** * 1114 milliseconds */
        long before = new Date().getTime();
        Set<Integer> entrySet = hashSet.stream().filter(i->(i % 2= =0)).collect(Collectors.toSet());
        long after = new Date().getTime();
        System.out.println(after-before);

        /** * 60 milliseconds */
        long before1 = new Date().getTime();
        Set<Integer> set = new HashSet<>();
        for (Integer integer: hashSet) {
            if (integer % 2= =0) { set.add(integer); }}long after1 = newDate().getTime(); System.out.println(after1-before1); System.out.println(entrySet.size()); System.out.println(set.size()); }}Copy the code

TreeSet

package com.apitest;

import java.util.*;
import java.util.stream.Collectors;

public class TreeSetStreamDemo {
    public static void main(String[] args) {

        int size = 1 << 20;
        TreeSet<Integer> treeSet = new TreeSet<>();
        for (int i = 0; i < size; i++) {
            treeSet.add(i);
        }

        /** * 863 milliseconds */
        long before = new Date().getTime();
        Set<Integer> entrySet = treeSet.stream().filter(i->(i % 2= =0)).collect(Collectors.toSet());
        long after = new Date().getTime();
        System.out.println(after-before);

        /** * 56 milliseconds */
        long before1 = new Date().getTime();
        Set<Integer> entrySet1 = new HashSet<>();
        for (Integer integer: treeSet) {
            if (integer % 2= =0) { entrySet1.add(integer); }}long after1 = newDate().getTime(); System.out.println(after1-before1); System.out.println(entrySet.size()); System.out.println(entrySet1.size()); }}Copy the code