zoukankan      html  css  js  c++  java
  • Java Arrays类方法

    1:概述

    主要谈一谈 Java使用fork/koin类 实现的并发排序 以及对于Stream流的支持的splitetor

    • mismatch()   ->  寻找两个数组 第一次出现数据不一致的下标
    • parallelPrefix() -> 对数组进行,累加求和
    • parallelSetAll() -> 对数组进行置数,
    • parallelSort()   -> 并行排序
    • Spliterator() -> 对数组进行切分(切分后的数据为所有的数据的组合)
      • 奇数 x/2+1     11->6
      • 偶数  x/2    10 ==>5 
    public class Use_Arrays {
          
        @Test
        public void test_mismatch() {
            int []x =new int[] {1,2,3,4};
            int []y =new int[] {1,3,4,5};
            int index = Arrays.mismatch(x, y);
            System.out.println(index);
        }
        
        @Test
        public void test_parallelPrefix() {
            int []x =new int[] {1,2,3,4};
            //f2=f1+f2
            //f3=f2+f3
            Arrays.parallelPrefix(x, (k,v)->k+v);
            System.out.println(Arrays.toString(x));
            
            // 实现1-100累加求和
            int []y =new int[100];
            Arrays.parallelSetAll(y, k->k=1);
            Arrays.parallelPrefix(y, (k,v)->k+v);
            System.out.println(Arrays.toString(y));
            
        }
        @Test
        public void test_parallelSetAll() {
            int []x =new int[100];
            x[0]=1;
            Arrays.parallelSetAll(x, y->y+1);
            System.out.println(Arrays.toString(x));
        }
        
        @Test
        public void test_parallSort() {
              IntStream stream = new Random().ints(0, 1000).limit(1000);
              int[] array = stream.toArray();
              System.out.println(Arrays.toString(array));
              Arrays.parallelSort(array);
              System.out.println(Arrays.toString(array));
        }
        
        @Test
        public void test_spliterator() {
            int []x =new int[11];
            Arrays.parallelSetAll(x, k->k+=1);
            System.out.println(Arrays.toString(x));
            
            Spliterator.OfInt int0_100 = Arrays.spliterator(x);
            
            int [] y=new int[(int) int0_100.estimateSize()];
            int i=0;
            System.out.println(int0_100.estimateSize());
            System.out.println(int0_100.characteristics());
            System.out.println(int0_100.getExactSizeIfKnown());
            
            //spliterator.forEachRemaining((int k)->System.out.println(k));
            
            OfInt int1_50 = int0_100.trySplit();
            OfInt int2_25 = int1_50.trySplit();
            int0_100.forEachRemaining((int k)->System.out.print(k+" "));
            System.out.println();
            int1_50.forEachRemaining((int k)->System.out.print(k+" "));
            System.out.println();
            int2_25.forEachRemaining((int k)->System.out.print(k+" "));
        }
    }

    2:使用Spliterator实现并行输出

        @Test
        public void definied_Sort() {
             IntStream stream = new Random().ints(0, 100).limit(100);
             int[] array = stream.toArray();
             Arrays.sort(array);
             final int NUMS=3;// 切分的次数
             
             ExecutorService thread_pool = Executors.newFixedThreadPool(10);
             
             Spliterator.OfInt cut1 = Arrays.spliterator(array);
             while(!thread_pool.isTerminated()) {
                 thread_pool.submit(()->{
                        OfInt split = cut1.trySplit();
                         thread_pool.shutdown();
                     split.forEachRemaining((int k)->System.out.print(k+" "));
                     System.out.println();
                 });
             }
        }
  • 相关阅读:
    Linux 下的类似Windows下Everything的搜索工具
    windows和linux环境下制作U盘启动盘
    程序调试手段之gdb, vxworks shell
    LeetCode 1021. Remove Outermost Parentheses (删除最外层的括号)
    LeetCode 1047. Remove All Adjacent Duplicates In String (删除字符串中的所有相邻重复项)
    LeetCode 844. Backspace String Compare (比较含退格的字符串)
    LeetCode 860. Lemonade Change (柠檬水找零)
    LeetCode 1221. Split a String in Balanced Strings (分割平衡字符串)
    LeetCode 1046. Last Stone Weight (最后一块石头的重量 )
    LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
  • 原文地址:https://www.cnblogs.com/dgwblog/p/11760438.html
Copyright © 2011-2022 走看看