How to Use valueOf() and compareTo() in Java 8 with Example

Learn how to use valueOf() and compareTo() in Java 8 with example. Convert String to Integer and compare strings lexicographically.

Differences Between valueOf() and compareTo() in Java 8 :-

  • valueOf() is used to convert a value from one type to another, such as String to Integer. In Java 8, it is commonly used with method references like Integer::valueOf .
  • compareTo() is used to compare two strings or objects.
  • valueOf() returns an object, while compareTo() returns an integer value.
  • compareTo() returns:
  • 0 if both strings are equal
  • a negative value if the first string is smaller
  • a positive value if the first string is greater

Code Explanation (Step-by-Step)

  • A functional interface Converter is created to convert one data type into another. Generic types F and T represent input and output data types.
  • A method reference Integer::valueOf is used to convert a string into an integer.
  • The string “123” is converted into an integer value 123.
  • The converted integer value is printed on the console.
  • A list of strings (java, spring, hibernate) is created.
  • The list is first sorted using a traditional comparator approach.
  • The compareTo() method compares strings in lexicographical (dictionary) order.
  • The sorted list is printed after using the comparator.
  • The list is sorted again using a method reference String::compareTo.
  • The result after method reference sorting is printed.
  • The compareTo() method is used again inside the lambda for comparison.
  • The final sorted list is printed after lambda sorting.
  • All three sorting methods produce the same lexicographically sorted output.
				
					import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@FunctionalInterface
interface Converter<F, T> {
	T convert(F from);
}

public class Test {
	public static void main(String[] args) {

		// Method Reference Example
		Converter<String, Integer> d = Integer::valueOf;
		Integer c1 = d.convert("123");
		System.out.println("convert String to Integer:- " + c1);

		List<String> list = Arrays.asList("java", "spring", "hibernate");

		// Sorting using comparator
		Collections.sort(list, new Comparator<String>() {
			public int compare(String a, String b) {
				return a.compareTo(b);
			}
		});
		System.out.println("from comparator:- " + list);

		// Sorting using compareTo method reference
		Collections.sort(list, String::compareTo);
		System.out.println("using compareTo method reference: " + list);

		// Sorting using compareTo and lambda expression
		Collections.sort(list, (String h1, String h2) -> {
			return h1.compareTo(h2);
		});
		System.out.println("using compareTo and  lambda expression:- " + list);
	}
}

				
			

Output :-
convert String to Integer:- 123
from comparator:- [hibernate, java, spring]
using compareTo method reference: [hibernate, java, spring]
using compareTo and lambda expression:- [hibernate, java, spring]