Learn to find the union between two arrays in Java using HashSet class. In set theory, the union (denoted by U) of a collection of sets is the set of all elements in the collection. For example, the union of two sets A and B is the set of all the elements which are either […]
Learn to find the intersection between two arrays in Java using HashSet class. An intersection is a group of common items that belong to two different sets. To get the intersection of two arrays, follow these steps: Push first array in a HashSet instance. Use retainAll() method to retain only elements which are present in […]
Learn to remove duplicate elements in Array in Java using different techniques such as LinkedHashSet from Collections framework and using a temporary array.
Learn how to check if an arraylist contains a value in Java with example. Also, learn to check if array contains an element, along with index of element in array. 1. Java ArrayList Contains Example To check if an ArrayList contains an element, use ArrayList.contains(element) method. contains(element) method does not take null argument, and will […]
Learn to create clone of an array in Java with example. Clone creates shallow copy of an array. Also learn to create array deep copies. Array Clone – Shallow Copy In Java, to create clone of array, you should use clone() method of array. It creates a shallow copy of array. Cloning always creates shallow […]
Learn to create deep copy of an array in Java with example. We will be using Apache Commons Lang library and it’s utility class org.apache.commons.lang3.SerializationUtils to array deep copy. Array Deep Copy Example A deep copy of an array will have all objects and it’s referred objects – created fresh. References from old array will […]
Java example to convert string into string array using String.split() method and using java.util.regex.Pattern class. Quick Reference: 1) Convert String to String 1.1) String.split() Use split() method to split string into tokens by passing a delimiter (or regex) as method argument. 1.2) Pattern.split() In Java, Pattern is the compiled representation of a regular expression. Use […]
Java example to print 2d array in string format in console or server logs.
Java example to create subarray from array. Learn to use Java 8 Arrays.copyOfRange() method along with converting the subarray to list object.
Java examples to copy an array to another array object using array clone, System.arrayCopy() and Arrays.copyOf() methods.
Learn to convert byte to String and String to byte in java – with examples. Conversion between byte array and string may be used in many cases including IO operations, generate secure hashes etc.
While working with encryption algorithms, you may get encrypted text as byte, which later need to be converted to String for comparison purpose. Converting byte to String in java is pretty much straightforward and easy. You need to use String class constructor String(byte bytes) or String(byte bytes, String charsetName) and you will get String represenation […]
Java print array content examples to learn about JDK’s existing support to print array content in more informative way, than normal toString() method which does not provide much information. 1) Arrays.toString() – Print array content Recommended way to print the content of an array is using Arrays.toString(). 2) Arrays.deepToString() – Print array of arrays What […]