java functions list
java functions list
Here are some common Java functions that you can use:
String Functions:
toLowerCase()
: converts a string to lowercase. toUpperCase()
: converts a string to uppercase. concat(String s)
: concatenates a string with another string. substring(int beginIndex, int endIndex)
: returns a subset of characters from the original string. indexOf(String str)
: finds the index of the first occurrence of a specified value within an array. lastIndexOf(String str)
: finds the index of the last occurrence of a specified value within an array. split(String regex)
: splits this string around matches of the given regular expression. trim()
: removes whitespace from both ends of a string. replace(String oldPattern, String newPattern)
: replaces all occurrences of the first argument with the second.
Math Functions:
abs(int a)
: returns the absolute value of an integer. ceil(double a)
: returns the smallest integer that is greater than or equal to the input value. floor(double a)
: returns the largest integer that is less than or equal to the input value. max(double a, double b)
: returns the maximum value of two numbers. min(double a, double b)
: returns the minimum value of two numbers. pow(double a, double b)
: calculates the power of one number raised to another. random()
: returns a pseudorandom number in the range 0.0 through 1.0. sqrt(double a)
: calculates the square root of a number.
Array Functions:
length()
: returns the size of an array. get(int index)
: returns the value at the specified position in an array. set(int index, int value)
: assigns a new value to the element at the specified position. equals(Object obj)
: checks whether two arrays are equal.
Date and Time Functions:
format(String format)
: formats a date according to a given pattern. parse(String text)
: parses a string into a Date object. getTime()
: returns the current system time in milliseconds. sleep(long milliseconds)
: causes the calling thread to sleep for a specified period of time.
File and Directory Functions:
listFiles()
: returns an array of files that can be found within this directory. mkdir(String dir)
: creates a new directory with the specified name. delete()
: deletes the file or directory denoted by this abstract path name. exists()
: checks whether a file or directory exists.
Miscellaneous Functions:
className()
: returns the name of the class of which an invocation is part. getClass()
: returns the runtime class of this Java object. notify()
: resets the lock on this object and test whether it should be notified. wait(long timeout)
: causes the current thread to wait until either another thread invades the Object's critical section or a specified amount of real time has elapsed.
This is not an exhaustive list, but it gives you an idea of the types of functions available in Java.
function<> in java
I'd be happy to explain the function
keyword in Java!
In Java, function
is an outdated term that was used in earlier versions of the language (pre-Java 8). Prior to Java 8, methods were not first-class citizens, meaning they could not be passed around as arguments or returned from other methods like functions.
However, starting from Java 8, the Function
interface was introduced, which allowed developers to create lambda expressions and method references that behaved similarly to functions in functional programming languages. This enabled more expressive code and improved support for functional programming concepts.
The Function
interface is part of the java.util.function
package and has a single abstract method (SAM) called apply(T t)
that takes an input parameter t
and returns a result.
Here are some key features of Java's function
keyword:
Function<T, R>
interface, which can be used as a replacement for traditional anonymous inner classes.
Method References: You can create method references using theFunction square = (x) -> x * x;
Function
interface, which can be used to invoke a target method.
Higher-Order Functions: Java'sFunction upperCase = String::toUpperCase;
function
keyword supports higher-order functions, which are methods that take other methods as arguments or return methods from other methods.
Here's an example of using a Function
to compose two methods:
Function doubleThenSquare = x -> Function.compose(x -> x * 2, (y) -> y * y);
In this example, the doubleThenSquare
function first doubles its input and then squares the result.
function
keyword also supports method chaining, which allows you to chain multiple method calls together using the andThen()
or compose()
methods.
Function upperCaseThenLower = upperCase.andThen(String::toLowerCase);
In this example, the upperCaseThenLower
function first converts a string to uppercase and then converts it back to lowercase.
Overall, Java's function
keyword has made the language more expressive and flexible, allowing developers to write more concise and functional code.