Mint Java Library

Introduction


Mint Java Library is a collection of useful methods for Arrays, Numbers and much more, which comes in handy everyday.

Using Mint with Maven


To use mint with maven, you can use mint library available in Maven Central by adding the following dependency:

<dependencies>
	<dependency>
		<groupId>com.github.manjotsidhu</groupId>
		<artifactId>mint</artifactId>
		<version>1.0</version>
		<scope>compile</scope>
	</dependency>
</dependencies>

Arrays com.github.manjotsidhu.mint.MintArrays

Inorder to use mintArrays, import the class by:

import com.github.manjotsidhu.mint.MintArrays;

Contains contains(E[] array, E element)

Checks if array contains the given element.

Integer[] myArray = {40, 76, 45, 90};
System.out.println(MintArrays.contains(myArray, 4));
System.out.println(MintArrays.contains(myArray, 40));
=> false
=> true


Drop drop(E[] array, int index)

Drops the specified element at index and returns the rest of the array.

Integer[] myArray = {38, 64, 33, 41, 12, 60};
System.out.println(MintArrays.toString(MintArrays.drop(myArray, 2)));
=> [38, 64, 41, 12, 60]


Find find(E[] array, E element)

Finds first occurance of the given element and returns the index of the found element.
If nothing found then returns -1.

Integer[] myArray = {3, 7, 9, 5, 7, 5, 1, 0, 7};
System.out.println(MintArrays.find(myArray, 9));
=> 2


First first(E[] array)

First method returns the first element of the given array.

Integer[] myArray = {85, 93, 14, 78, 79, 66, 16, 90, 21};
System.out.println(MintArrays.first(myArray));
=> 85


First first(E[] array, int n)

First method returns the first n elements of the given array.

Integer[] myArray = {81, 59, 51, 10, 42, 70, 99};
System.out.println(MintArrays.toString(MintArrays.first(myArray, 3)));
=> [81, 59, 51]


Initial initial(E[] array)

Initial returns everything but not the last element of the array.

Integer[] myArray = {46, 29, 53, 82, 6, 18};
System.out.println(MintArrays.toString(MintArrays.initial(myArray)));
=> [29, 53, 82, 6]


Initial initial(E[] array, int n)

Initial returns everything but not the n last elements.

String[] myArray = {"ab", "Az", "ac", "ae", "ss"};
System.out.println(MintArrays.toString(MintArrays.initial(myArray, 3)));
=> [ab, Az]


IsFirst isFirst(E[] array, E element)

isFirst checks if the specified element is the first element of array or not.

Integer[] myArray = {37, 62, 37, 27, 77, 27, 39, 93};
System.out.println(MintArrays.isFirst(myArray, 39));
=> false


IsLast isLast(E[] array, E element)

isLast checks if the specified element is the last element of array or not.

Integer[] myArray = {37, 62, 37, 27, 77, 27, 39, 93};
System.out.println(MintArrays.isLast(myArray, 39));
=> false


Intersection intersecton(E[] array1, E[] array2)

Intersection returns an array of elements containing common members of array1 and array2.

Integer[] array1 = mintArrays.randomIntArray(10, 10);
Integer[] array2 = mintArrays.randomIntArray(10, 10);

System.out.println("array1 : " + mintArrays.toString(array1));
System.out.println("array2 : " + mintArrays.toString(array2));
System.out.println(mintArrays.toString(mintArrays.intersection(array1, array2)));
=> array1 : [3, 9, 6, 8, 8, 9]
=> array2 : [5, 6, 0, 6, 0, 8, 0, 6]
=> [6, 8, 8]


Last last(E[] array)

Last returns the last element of the array.

Integer[] myArray = {39, 89, 63, 30, 73, 95, 32, 25, 76};
System.out.println(MintArrays.last(myArray));
=> 76


Last last(E[] array, int n)

Last returns the last n elements of the array.

String[] myArray = {"ab", "Az", "ac", "ae", "ss"};
System.out.println(MintArrays.toString(MintArrays.last(myArray, 3)));
=> [ac, ae, ss]


RandomIntArray randomIntArray()

Generates random integer array of length less than 10 and numbers less in the range [0, 100].

System.out.println(MintArrays.toString(MintArrays.randomIntArray()));
=> [35, 20, 14, 13]


RandomIntArray randomIntArray(int n, int m)

Generates random integer array of length less than n and numbers in the range [0, m].

System.out.println(MintArrays.toString(MintArrays.randomIntArray(20, 9999)));
=> [1285, 9323, 4857, 1324, 9172, 479, 4317, 8779, 9379, 3562]


Resize resize(E[] array, int n)

Resizes the given array as argument and returns a new array of length n.

Integer[] myArray = {2, 3, 74, 37};
System.out.println(MintArrays.toString(resize(myArray, 2)));
=> [2, 3]


Reverse reverse(E[] arr)

Reverses array arr of almost any datatype E.


String[] myArray = {"manjot", "singh", "sidhu"};
System.out.println(MintArrays.toString(MintArrays.reverse(myArray)));
=> [sidhu, singh, manjot]


Sort sort(E[] arr)

Sorts Generic number object array in ascending order.

Integer[] myArray = {6, 4, 2, 1, 3, 5, 8, 1, 1222, 99};
MintArrays.sort(myArray);
System.out.println(MintArrays.toString(myArray));
=> [1, 1, 2, 3, 4, 5, 6, 8, 99, 1222]


Sort sortD(E[] arr)

Sorts Generic number object array in descending order.

Integer[] myArray = {6, 4, 2, 1, 3, 5, 8, 1, 99, 1};
MintArrays.sortD(myArray);
System.out.println(MintArrays.toString(myArray));
=> [99, 8, 6, 5, 4, 3, 2, 1, 1, 1]


Sort sort(int[] arr)

Sorts array arr of only int[] datatype in alphabetical order.

int[] myArray = {6, 4, 2, 1, 3, 5, 8, 1, 1222, 99};
MintArrays.sort(myArray);
System.out.println(MintArrays.toString(myArray));
=> [1, 1, 2, 3, 4, 5, 6, 8, 99, 1222]


SortD sortD(int[] arr)

Sorts array arr of only int[] datatype in descending order.

int[] myArray = {6, 4, 2, 1, 3, 5, 8, 1, 99, 1};
MintArrays.sortD(myArray);
System.out.println(MintArrays.toString());
=> [99, 8, 6, 5, 4, 3, 2, 1, 1, 1]


Sort sort(String[] arr)

Sorts array arr of only String[] datatype in ascending order.

String[] myArray = {"ab", "Az", "ac", "ae", "ss"};
MintArrays.sort(myArray);
System.out.println(MintArrays.toString(myArray));
=> [ab, ac, ae, Az, ss]


SortD sortD(String[] arr)

Sorts array arr of only String[] datatype in descending alphabetical order.

String[] myArray = {"ab", "Az", "ac", "ae", "ss"};
MintArrays.sortD(myArray);
System.out.println(MintArrays.toString(myArray));
=> [ss, Az, ae, ac, ab]


ToString toString(E[] array)

toString returns the formal representation of the array in a string.

Integer[] myArray = {56, 53, 53, 96, 27};
System.out.println(MintArrays.toString(myArray));
=> [56, 53, 53, 96, 27]


Union union(E[] array1, E[] array2)

Union returns an array of elements of array1, in array2 or common in both array1 and array2.

Integer[] array1 = mintArrays.randomIntArray(10, 10);
Integer[] array2 = mintArrays.randomIntArray(10, 10);
	
System.out.println("array1 : " + mintArrays.toString(array1));
System.out.println("array2 : " + mintArrays.toString(array2));
System.out.println(mintArrays.toString(mintArrays.union(array1, array2)));
=> array1 : [3, 6]
=> array2 : [1, 6, 3, 8, 5, 4, 1]
=> [3, 6, 1, 8, 5, 4, 1]


Numbers com.github.manjotsidhu.mint.MintNumbers

Inorder to use mintNumbers, import the class by:

import com.github.manjotsidhu.mint.MintNumbers;

Ceil <E extends Number> int ceil(final E number)

Ceil inputs any real number X of datatype E and returns the least integer greater than or equal to x.

System.out.println(MintNumbers.ceil(9.7549));
=> 9


Floor <E extends Number> int floor(E number)

Floor inputs any real number X of datatype E and returns the greatest integer less than or equal to x.

System.out.println(MintNumbers.floor(2.123));
=> 3


Max <T extends Number> T max(T ...x)

Computes Max of all passed numbers as arguments.

System.out.println(MintNumbers.max(23, 45, 67, 99, 0, -0.99));
=> 99


Min <T extends Number> T min(T ...x)

Computes Min of all passed numbers as arguments.

System.out.println(MintNumbers.min(23, 45, 67, 99, 0, -0.99));
=> -0.99


RandomInt randomInt(int n)

RandomInt generates random integer in the range of 0-n numbers.

System.out.println(MintNumbers.randomInt(100));
=> 40


Strings com.github.manjotsidhu.mint.MintStrings

Inorder to use mintStrings, import the class by:

import com.github.manjotsidhu.mint.MintStrings;

IsUpper public static boolean isUpper(String input)

Checks if a given string contains all upper characters or not.

System.out.println(MintStrings.isUpper("LoL! I will return False"));
=> false


IsLower public static boolean isLower(String input)

Checks if a given string contains all lower characters only.

System.out.println(MintStrings.isLower("lol! i will return true"));
=> true


IsAlpha public static boolean isAlpha(String input)

Checks if a given string contains only alphabets.

System.out.println(MintStrings.isAlpha("HelloWorld"));
System.out.println(MintStrings.isAlpha("Hello World"));
=> true
=> false


IsAlphaNum public static boolean isAlphaNum(String input)

Checks if a given string is alphanumeric or not.

System.out.println(MintStrings.isAlphaNum("Hello World"));
System.out.println(MintStrings.isAlphaNum("Hello World!"));
=> true
=> false


Changelog

  • v1.0

    • Initial Release (18 methods in mintArrays, 5 methods in mintNumbers).