- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectStreamField compareTo(Object obj) method
Description
The Java ObjectStreamField compareTo(Object obj) method compares this field with another ObjectStreamField. Return -1 if this is smaller, 0 if equal, 1 if greater. Types that are primitives are "smaller" than object types. If equal, the field names are compared.
It compares two ObjectStreamField objects.
-
It compares based on −
Field name first (lexicographical order).
Field type (if names are equal, but very rare).
Useful when you want to sort fields alphabetically for display, checking order, etc.
Declaration
Following is the declaration for java.io.ObjectStreamField.compareTo(Object obj) method.
public int compareTo(Object obj)
Parameters
obj − The object to be compared.
Return Value
This method returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Exception
NA
Example - Usage of ObjectStreamField compareTo(Object obj) method
The following example shows the usage of ObjectStreamField compareTo(Object obj) method.
ObjectStreamFieldDemo.java
package com.tutorialspoint;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
public class ObjectStreamFieldDemo {
public static void main(String[] args) {
// create a new object stream class for Integers
ObjectStreamClass osc = ObjectStreamClass.lookupAny(Integer.class);
// get the field value from Integer class
ObjectStreamField field = osc.getField("value");
// create a new object stream class for floats
ObjectStreamClass osc2 = ObjectStreamClass.lookupAny(Float.class);
// get the field value from Integer class
ObjectStreamField field2 = osc.getField("value");
// compare with another field
System.out.println("" + field.compareTo(field2));
}
}
Output
Let us compile and run the above program, this will produce the following result −
0
Example - Compare two fields with different names
The following example shows the usage of ObjectStreamField compareTo(Object obj) method. We're comparing fields "age" and "name".
ObjectStreamFieldDemo.java
package com.tutorialspoint;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;
public class ObjectStreamFieldDemo {
public static void main(String[] args) {
ObjectStreamClass osc = ObjectStreamClass.lookup(Person.class);
ObjectStreamField[] fields = osc.getFields();
ObjectStreamField field1 = fields[0]; // age
ObjectStreamField field2 = fields[1]; // name
int result = field1.compareTo(field2);
System.out.println("Comparison result: " + result);
if (result < 0) {
System.out.println(field1.getName() + " comes before " + field2.getName());
} else if (result > 0) {
System.out.println(field1.getName() + " comes after " + field2.getName());
} else {
System.out.println("Fields are equal.");
}
}
static class Person implements Serializable {
private static final long serialVersionUID = 1L;
int age;
String name;
}
}
Output
Let us compile and run the above program, this will produce the following result −
Comparison result: -1 age comes before name
Explanation
"age" comes before "name" alphabetically.
compareTo() returns a negative number (-1 here).
Useful for sorting fields in custom serializers.
Example - Sort fields alphabetically using compareTo()
The following example shows the usage of ObjectStreamField compareTo(Object obj) method. We're sorting a list of ObjectStreamField fields by name.
ObjectStreamFieldDemo.java
package com.tutorialspoint;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.util.Arrays;
public class ObjectStreamFieldDemo {
public static void main(String[] args) {
ObjectStreamClass osc = ObjectStreamClass.lookup(Product.class);
ObjectStreamField[] fields = osc.getFields();
System.out.println("Before sorting:");
for (ObjectStreamField field : fields) {
System.out.println(" - " + field.getName());
}
Arrays.sort(fields, (o1, o2) -> o1.getName().compareTo(o2.getName()));
System.out.println("\nAfter sorting:");
for (ObjectStreamField field : fields) {
System.out.println(" - " + field.getName());
}
}
static class Product implements Serializable {
private static final long serialVersionUID = 1L;
double price;
String name;
int stock;
}
}
Output
Let us compile and run the above program, this will produce the following result−
Before sorting: - price - name - stock After sorting: - name - price - stock
Explanation
Arrays.sort(fields) uses each ObjectStreamField's name's compareTo() method.
The fields get sorted by name (name, price, stock).
Great when you want predictable serialized field order.