- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Arrays compare() Method
Description
The Java Arrays compare(T[] a, T[] b) method compares the two arrays of comparable objects lexicographically. In case of null arrays comparison, arrays are considered equals and a null array is lexicographically less than a not null array.
Declaration
Following is the declaration for java.util.Arrays.compare(T[] a, T[] b) method
public static <T extends Comparable<? super T>> int compare(T[] a, T[] b)
Type Parameters
T − This is the type of comparable array elements.
Parameters
a − This is the first array to be compared.
b − This is the second array to be compared.
Return Value
This method returns the value 0 if the first and second array are equal and contain the same elements in the same order; a value less than 0 if the first array is lexicographically less than the second array; and a value greater than 0 if the first array is lexicographically greater than the second array.
Exception
NA
Java Arrays compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) Method
Description
The Java Arrays compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) method compares the two arrays of comparable objects in the given ranges lexicographically. In case of any array null, a NullPointerException is thrown.
Declaration
Following is the declaration for java.util.Arrays.compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) method
public static <T extends Comparable<? super T>> int compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)
Type Parameters
T − This is the type of comparable array elements.
Parameters
a − This is the first array to be compared.
aFromIndex − This is the index of the first element (inclusive) of first array to be compared.
aToIndex − This is the index of the last element (exclusive) of first array to be compared.
b − This is the second array to be compared.
bFromIndex − This is the index of the first element (inclusive) of second array to be compared.
bToIndex − This is the index of the last element (exclusive) of second array to be compared.
Return Value
This method returns the value 0 if, over the specified ranges, the first and second array are equal and contain the same elements in the same order; a value less than 0 if, over the specified ranges, the first array is lexicographically less than the second array; and a value greater than 0 if, over the specified ranges, the first array is lexicographically greater than the second array.
Exception
IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex
ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex > a.length or if bFromIndex < 0 or bToIndex > b.length
NullPointerException − if either array is null
Java Arrays compare(T[] a, T[] b, Comparator<? super T> cmp) Method
Description
The Java Arrays compare(T[] a, T[] b, Comparator<? super T> cmp) method compares the two arrays of objects lexicographically using a specified comparator. In case of null arrays comparison, arrays are considered equals and a null array is lexicographically less than a not null array.
Declaration
Following is the declaration for java.util.Arrays.compare(T[] a, T[] b, Comparator<? super T> cmp) method
public static <T> int compare(T[] a, T[] b, Comparator<? super T> cmp)
Type Parameters
T − This is the type of comparable array elements.
Parameters
a − This is the first array to be compared.
b − This is the second array to be compared.
cmp − This is the comparator to compare array elements.
Return Value
This method returns the value 0 if the first and second array are equal and contain the same elements in the same order; a value less than 0 if the first array is lexicographically less than the second array; and a value greater than 0 if the first array is lexicographically greater than the second array.
Exception
NullPointerException − if the comparator is null.
Java Arrays compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) Method
Description
The Java Arrays compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) method compares the two arrays of objects in the given ranges lexicographically using the specified comparator. In case of any array null, a NullPointerException is thrown.
Declaration
Following is the declaration for java.util.Arrays.compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) method
public static <T extends Comparable<? super T>> int compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)
Type Parameters
T − This is the type of comparable array elements.
Parameters
a − This is the first array to be compared.
aFromIndex − This is the index of the first element (inclusive) of first array to be compared.
aToIndex − This is the index of the last element (exclusive) of first array to be compared.
b − This is the second array to be compared.
bFromIndex − This is the index of the first element (inclusive) of second array to be compared.
bToIndex − This is the index of the last element (exclusive) of second array to be compared.
cmp − This is the comparator to compare array elements.
Return Value
This method returns the value 0 if, over the specified ranges, the first and second array are equal and contain the same elements in the same order; a value less than 0 if, over the specified ranges, the first array is lexicographically less than the second array; and a value greater than 0 if, over the specified ranges, the first array is lexicographically greater than the second array.
Exception
IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex.
ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex > a.length or if bFromIndex < 0 or bToIndex > b.length.
NullPointerException − if either array or the comparator is null.
Comparing Two Arrays of Same Object Values Example
The following example shows the usage of Java Arrays compare(T[], T[]) method. First, we've created two arrays of same objects, and compared them using compare() method. As per the result, the comparison is printed.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initialize first students array
Student array1[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
// initialize second students array
Student array2[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
int result = Arrays.compare(array1, array2);
if(result > 0) {
System.out.println("First array is greater than second array.");
} else if (result == 0) {
System.out.println("Arrays are same.");
} else {
System.out.println("First array is less than second array.");
}
}
}
class Student implements Comparable<Student> {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
@Override
public int compareTo(Student student) {
return this.rollNo - student.rollNo;
}
}
Output
Let us compile and run the above program, this will produce the following result −
Arrays are same.
Comparing Two Sub-Arrays of Object Values Example
The following example shows the usage of Java Arrays compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) method. First, we've created two arrays of different shorts, and compared them using compare() method. As per the result, the comparison is printed.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initialize first students array
Student array1[] = { new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam") };
// initialize second students array
Student array2[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
int result = Arrays.compare(array1, 0, 2, array2, 0, 2);
if(result > 0) {
System.out.println("First array is greater than second array.");
} else if (result == 0) {
System.out.println("Arrays are same.");
} else {
System.out.println("First array is less than second array.");
}
}
}
class Student implements Comparable<Student> {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
@Override
public int compareTo(Student student) {
return this.rollNo - student.rollNo;
}
}
Output
Let us compile and run the above program, this will produce the following result −
First array is less than second array.
Comparing Two Arrays of Different Object Values Example
The following example shows the usage of Java Arrays compare(T[], T[], Comparator) method. First, we've created two arrays of same objects, and compared them using compare() method. As per the result, the comparison is printed.
package com.tutorialspoint;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayDemo {
public static void main(String[] args) {
// initialize first students array
Student array1[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
// initialize second students array
Student array2[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
RollNoComparator comparator = new RollNoComparator();
int result = Arrays.compare(array1, array2,comparator);
if(result > 0) {
System.out.println("First array is greater than second array.");
} else if (result == 0) {
System.out.println("Arrays are same.");
} else {
System.out.println("First array is less than second array.");
}
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class RollNoComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getRollNo()-o2.getRollNo();
}
}
Output
Let us compile and run the above program, this will produce the following result −
Arrays are same.
Example #4
The following example shows the usage of Java Arrays compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator cmp) method. First, we've created two arrays of different shorts, and compared them using compare() method. As per the result, the comparison is printed.
package com.tutorialspoint;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayDemo {
public static void main(String[] args) {
// initialize first students array
Student array1[] = { new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam") };
// initialize second students array
Student array2[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
RollNoComparator comparator = new RollNoComparator();
int result = Arrays.compare(array1, 0, 2, array2, 0, 2, comparator);
if(result > 0) {
System.out.println("First array is greater than second array.");
} else if (result == 0) {
System.out.println("Arrays are same.");
} else {
System.out.println("First array is less than second array.");
}
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class RollNoComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getRollNo()-o2.getRollNo();
}
}
Output
Let us compile and run the above program, this will produce the following result −
First array is less than second array.