Java Parallel Array Sorting and Type Inference

Last updated on Dec 27 2022
Prabhas Ramanathan

Java provides a new additional feature in Array class which is used to sort array elements parallel.New methods has added to java.util.Arrays package that use the JSR 166 Fork/Join parallelism common pool to provide sorting of arrays in parallel.The methods are called parallelSort() and are overloaded for all the primitive data types and Comparable objects.
The following table contains Arrays overloaded sorting methods.

Methods Description
public static void parallelSort(byte[] a) It sorts the specified array into ascending numerical order.
public static void parallelSort(byte[] a, int fromIndex, int toIndex) It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.
public static void parallelSort(char[] a) It sorts the specified array into ascending numerical order.
public static void parallelSort(char[] a, int fromIndex, int toIndex) It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.
public static void parallelSort(double[] a) It sorts the specified array into ascending numerical order.
public static void parallelSort(double[] a, int fromIndex, int toIndex) It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.
public static void parallelSort(float[] a) It sorts the specified array into ascending numerical order.
public static void parallelSort(float[] a, int fromIndex, int toIndex) It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.
public static void parallelSort(int[] a) It sorts the specified array into ascending numerical order.
public static void parallelSort(int[] a,int fromIndex, int toIndex) It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.
public static void parallelSort(long[] a) It sorts the specified array into ascending numerical order.
public static void parallelSort(long[] a, int fromIndex, int toIndex) It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.
public static void parallelSort(short[] a) It sorts the specified array into ascending numerical order.
public static void parallelSort(short[] a,int fromIndex,int toIndex) It sorts the specified range of the array into ascending numerical order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive. If fromIndex == toIndex, the range to be sorted is empty.
public static <T extends Comparable<? super T>> void parallelSort(T[] a) Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
public static <T7gt; void parallelSort(T[] a,Comparator<? super T> cmp) It sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
public static <T extends Comparable<? super T>> void parallelSort(T[] a,int fromIndex, int toIndex) It sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in this range must implement the Comparable interface. Furthermore, all elements in this range must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) It sorts the specified range of the specified array of objects according to the order induced by the specified comparator. The range to be sorted extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be sorted is empty.) All elements in the range must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the range).

Table of Contents

Java Parallel Array Sorting Example

1. import java.util.Arrays; 
2. public class ParallelArraySorting { 
3. public static void main(String[] args) { 
4. // Creating an integer array 
5. int[] arr = {5,8,1,0,6,9}; 
6. // Iterating array elements 
7. for (int i : arr) { 
8. System.out.print(i+" "); 
9. } 
10. // Sorting array elements parallel 
11. Arrays.parallelSort(arr); 
12. System.out.println("\nArray elements after sorting"); 
13. // Iterating array elements 
14. for (int i : arr) { 
15. System.out.print(i+" "); 
16. } 
17. } 
18. }

Output:
5 8 1 0 6 9

Array elements after sorting
0 1 5 6 8 9

 

Java Parallel Array Sorting Example: Passing Start and End Index

In the following example, we are passing starting and end index of the array. The first index is inclusive and end index is exclusive i.e. if we pass 0 as start index and 4 as end index, only 0 to 3 index elements will be sorted.
It throws IllegalArgumentException if start index > end index.
It throws ArrayIndexOutOfBoundsException if start index < 0 or end index > a.length.

1. import java.util.Arrays; 
2. public class ParallelArraySorting { 
3. public static void main(String[] args) { 
4. // Creating an integer array 
5. int[] arr = {5,8,1,0,6,9,50,-3}; 
6. // Iterating array elements 
7. for (int i : arr) { 
8. System.out.print(i+" "); 
9. } 
10. // Sorting array elements parallel and passing start, end index 
11. Arrays.parallelSort(arr,0,4); 
12. System.out.println("\nArray elements after sorting"); 
13. // Iterating array elements 
14. for (int i : arr) { 
15. System.out.print(i+" "); 
16. } 
17. } 
18. }

Output:
5 8 1 0 6 9 50 -3
Array elements after sorting
0 1 5 8 6 9 50 -3

Java Type Inference

Type inference is a feature of Java which provides ability to compiler to look at each method invocation and corresponding declaration to determine the type of arguments.
Java provides improved version of type inference in Java 8. the following example explains, how we can use type inference in our code:
Here, we are creating arraylist by mentioning integer type explicitly at both side. The following approach is used earlier versions of Java.
1. List<Integer> list = new ArrayList<Integer>();
In the following declaration, we are mentioning type of arraylist at one side. This approach was introduce in Java 7. Here, you can left second side as blank diamond and compiler will infer type of it by type of reference variable.
1. List<Integer> list2 = new ArrayList<>();

Improved Type Inference

In Java 8, you can call specialized method without explicitly mentioning of type of arguments.
1. showList(new ArrayList<>());

 

Java Type Inference Example

You can use type inference with generic classes and methods.

1. import java.util.ArrayList; 
2. import java.util.List; 
3. public class TypeInferenceExample { 
4. public static void showList(List<Integer>list){ 
5. if(!list.isEmpty()){ 
6. list.forEach(System.out::println); 
7. }else System.out.println("list is empty"); 
8. } 
9. public static void main(String[] args) { 
10. // An old approach(prior to Java 7) to create a list 
11. List<Integer> list1 = new ArrayList<Integer>(); 
12. list1.add(11); 
13. showList(list1); 
14. // Java 7 
15. List<Integer> list2 = new ArrayList<>(); // You can left it blank, compiler can infer type 
16. list2.add(12); 
17. showList(list2); 
18. // Compiler infers type of ArrayList, in Java 8 
19. showList(new ArrayList<>()); 
20. } 
21. }

Output:
11
12

list is empty
You can also create your own custom generic class and methods. In the following example, we are creating our own generic class and method.

 

Java Type Inference Example 2

1. class GenericClass<X> { 
2. X name; 
3. public void setName(X name){ 
4. this.name = name; 
5. } 
6. public X getName(){ 
7. returnname; 
8. } 
9. public String genericMethod(GenericClass<String> x){ 
10. x.setName("John"); 
11. returnx.name; 
12. } 
13. } 
14. 
15. public class TypeInferenceExample { 
16. public static void main(String[] args) { 
17. GenericClass<String> genericClass = new GenericClass<String>(); 
18. genericClass.setName("Peter"); 
19. System.out.println(genericClass.getName()); 
20. 
21. GenericClass<String> genericClass2 = new GenericClass<>(); 
22. genericClass2.setName("peter"); 
23. System.out.println(genericClass2.getName()); 
24. 
25. // New improved type inference 
26. System.out.println(genericClass2.genericMethod(new GenericClass<>())); 
27. } 
28. }

Output:
Peter
peter
John

So, this brings us to the end of blog. This Tecklearn ‘Java Parallel Array Sorting and Type Inference’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable
Java Database Connectivity (JDBC)
• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback
Java Enterprise Edition – Servlets
• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers
Java Server Pages (JSP)
• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management
Hibernate
• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture
POJO (Plain Old Java Object)
• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object
Spring
• Introduction to Spring
• Spring Fundamentals
• Advanced Spring
Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Java Parallel Array Sorting and Type Inference"

Leave a Message

Your email address will not be published. Required fields are marked *