java.util.*
package contains so many utilities like collection framework, event model, date facility, time facility and so on.
Java doesn’t provide simple compare utility for primitive types like
- Compare String Arrays
- Compare Integer Arrays
- Simple String compares
- and so on…
If you have below questions then below tutorial will help you.
- How to compare two arrays in Java?
- Comparing two integer arrays in java
- In Java compare arraylist
- In Java compare collections
- In Java compare arrays for equality
In this tutorial we will go over all above 3 utilities with simple Java Method Overloading
concept.
What is Method Overloading in Java?
Method overloading means you could have number of methods
with the same name as far as you have different parameters for each.
There are two ways
you could overload method in java:
- By having number of
different arguments
- By having the
different data types
NOTE:
Java Method Overriding Tutorial
In our tutorial crunchifyCompare
is a utility and overloading method
which will perform compare operation based on type of arguments.
package crunchify.com.tutorial; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * @author Crunchify.com * Version: 1.0 * Updated: 12.27.2015 */ public class CrunchifyCompareUtility { public static void main(String[] args) { // Let's compare two Strings log("crunchifyCompare(\"Crunchify\", \"Crunchify\"): " + crunchifyCompare("Crunchify", "Crunchify")); log("crunchifyCompare(\"Yahoo\", \"Google\"): " + crunchifyCompare("Yahoo", "Google")); String arr1[] = { "Crunchify, Yahoo, Google" }; String arr2[] = { "Crunchify, Yahoo, Google" }; String arr3[] = { "Crunchify, Facebook, Google" }; // Compare StringArrays log("\ncrunchifyCompare(arr1, arr2): " + crunchifyCompare(arr1, arr2)); log("crunchifyCompare(arr1, arr3): " + crunchifyCompare(arr1, arr3)); int arr4[] = { 11, 12, 13 }; int arr5[] = { 11, 12, 13 }; int arr6[] = { 12, 13, 14 }; // Compare IntegerArrays log("\ncrunchifyCompare(arr4, arr5): " + crunchifyCompare(arr4, arr5)); log("crunchifyCompare(arr4, arr6): " + crunchifyCompare(arr4, arr6)); } private static void log(String log) { System.out.println(log); } /** * String Compare Utility * * @param newData * @param intialData * @return true / false */ public static boolean crunchifyCompare(String newData, String intialData) { if (newData != intialData) { if (newData == null || intialData == null) return false; return newData.equals(intialData); } return true; } /** * StringArray Compare Utility * * @param newData * @param intialData * @return true / false */ public static boolean crunchifyCompare(String[] newData, String[] intialData) { if (newData.length != intialData.length) return false; Set<String> crunchifySet = new HashSet<>(Arrays.asList(intialData)); for (String currentValue : newData) if (!crunchifySet.contains(currentValue)) return false; return true; } /** * IntegerArray Compare Utility * * @param newData * @param intialData * @return true / false */ public static boolean crunchifyCompare(int[] newData, int[] intialData) { if (newData.length != intialData.length) return false; Set<Integer> crunchifySet = getCrunchifySet(intialData); for (int currentValue : newData) if (!crunchifySet.contains(currentValue)) return false; return true; } public static Set<Integer> getCrunchifySet(int[] crunchifyData) { Set<Integer> crunchifySet = new HashSet<>(); for (int data : crunchifyData) crunchifySet.add(data); return crunchifySet; } }
Result:
crunchifyCompare("Crunchify", "Crunchify"): true crunchifyCompare("Yahoo", "Google"): false crunchifyCompare(arr1, arr2): true crunchifyCompare(arr1, arr3): false crunchifyCompare(arr4, arr5): true crunchifyCompare(arr4, arr6): false
We will add more tutorials and compare utilities like two JSON Object Compares, two CSV file compare, etc. So stay tuned.
NOTE:
some of the utilities are already available in Apache Commons IO
library. If you have hard dependency on NOT to include any third party library into your project then you could include above utility.