Javatpoint标志
Javatpoint标志

Java并行数组排序

Java在Array类中提供了一个新的附加功能,用于并行排序数组元素。java.util.Arrays包中添加了新的方法,这些方法使用JSR 166 Fork/Join并行公共池来并行地提供数组排序。这些方法称为parallelSort(),并为所有基本数据类型和Comparable对象重载。

下表包含数组重载排序方法。

方法 描述
并行排序(byte[] a) 它将指定的数组按升序排序。
并行排序(byte[] a, int fromIndex, int toIndex) 它将数组的指定范围按升序排序。要排序的范围从包含fromIndex的索引扩展到不包含toIndex的索引。如果fromIndex == toIndex,要排序的范围为空。
并行排序(char[] a) 它将指定的数组按升序排序。
并行排序(char[] a, int fromIndex, int toIndex) 它将数组的指定范围按升序排序。要排序的范围从包含fromIndex的索引扩展到不包含toIndex的索引。如果fromIndex == toIndex,要排序的范围为空。
public static void parallelSort(double[] a) 它将指定的数组按升序排序。
public static void parallelSort(double[] a, int fromIndex, int toIndex) 它将数组的指定范围按升序排序。要排序的范围从包含fromIndex的索引扩展到不包含toIndex的索引。如果fromIndex == toIndex,要排序的范围为空。
public static void parallelSort(float[] a) 它将指定的数组按升序排序。
public static void parallelSort(float[] a, int fromIndex, int toIndex) 它将数组的指定范围按升序排序。要排序的范围从包含fromIndex的索引扩展到不包含toIndex的索引。如果fromIndex == toIndex,要排序的范围为空。
并行排序(int[] a) 它将指定的数组按升序排序。
public static void parallelSort(int[] a,int fromIndex, int toIndex) 它将数组的指定范围按升序排序。要排序的范围从包含fromIndex的索引扩展到不包含toIndex的索引。如果fromIndex == toIndex,要排序的范围为空。
public static void parallelSort(long[] a) 它将指定的数组按升序排序。
public static void parallelSort(long[] a, int fromIndex, int toIndex) 它将数组的指定范围按升序排序。要排序的范围从包含fromIndex的索引扩展到不包含toIndex的索引。如果fromIndex == toIndex,要排序的范围为空。
并行排序(short[] a) 它将指定的数组按升序排序。
public static void parallelSort(short[] a,int fromIndex,int toIndex) 它将数组的指定范围按升序排序。要排序的范围从包含fromIndex的索引扩展到不包含toIndex的索引。如果fromIndex == toIndex,要排序的范围为空。
public static > void parallelSort(T[] a) 根据元素的自然顺序,将指定的对象数组按升序排序。数组中的所有元素都必须实现Comparable接口。此外,数组中的所有元素必须相互比较(也就是说,e1. compareto (e2)不能对数组中的任何元素e1和e2抛出ClassCastException)。
public static cmp 它根据指定比较器引起的顺序对指定的对象数组进行排序。数组中的所有元素必须通过指定的比较器相互比较(也就是说,c.compare(e1, e2)不能对数组中的任何元素e1和e2抛出ClassCastException)。
public static > void parallelSort(T[] a,int fromIndex, int toIndex) 它根据元素的自然顺序,将指定对象数组的指定范围按升序排序。要排序的范围从索引frommindex(包含)扩展到索引toIndex(不包含)。(如果fromIndex==toIndex,要排序的范围为空。)此范围内的所有元素都必须实现Comparable接口。此外,此范围内的所有元素必须相互比较(即e1. compareto (e2)不得为数组中的任何元素e1和e2抛出ClassCastException)。
public static void parallelSort(T[] a, int fromIndex, int toIndex, Comparator cmp 它根据指定比较器诱导的顺序对指定对象数组的指定范围进行排序。要排序的范围从索引frommindex(包含)扩展到索引toIndex(不包含)。(如果fromIndex==toIndex,要排序的范围为空。)范围内的所有元素必须通过指定的比较器相互比较(即c.compare(e1, e2)不得对范围内的任何元素e1和e2抛出ClassCastException)。

Java并行数组排序示例

输出:

5 8 1 0 6 9排序后的数组元素0 1 5 6 8 9

Java并行数组排序示例:传递起始和结束索引

在下面的例子中,我们传递数组的起始和结束索引。第一个索引是包含的,结束索引是排他的,即如果我们传递0作为开始索引,4作为结束索引,只有0到3个索引元素将被排序。

它抛出IllegalArgumentException如果开始索引>结束索引。

如果开始索引< 0或结束索引> a.length,则抛出ArrayIndexOutOfBoundsException。

输出:

5 8 10 6 9 50 -3排序后的数组元素0 1 8 6 9 50 -3
下一个话题 Java 8类型推断





Youtube 视频加入我们的Youtube频道:现在加入

反馈


帮助别人,请分享

脸谱网 推特 pinterest

学习最新教程


准备


热门的技术


b .技术/马华






Baidu
map