The Ultimate Java Cheat Sheet

The Ultimate Java Cheat Sheet

String Class

Syntax - Taking s1 if one String and (s1,s2) if two strings are required.

MethodsDescription
char s1.charAt(int index)returns char value for the particular index
int s1.length()returns string length
String s1.substring(int beginIndex)returns substring for given begin index.
String s1.substring(int beginIndex, int endIndex)returns substring for given begin index and end index.
boolean s1.contains(CharSequence s)returns true or false after matching the sequence of char value.
boolean s1.equals(Object another)checks the equality of string with the given object.
boolean s1.isEmpty()checks if string is empty.
String s1.concat(String str)concatenates the specified string.
String s1.replace(char old, char new)replaces all occurrences of the specified char value.
String s1.replace(CharSequence old, CharSequence new)replaces all occurrences of the specified CharSequence.
String s1.equalsIgnoreCase(String another)compares another string. It doesn't check case.
String[] s1.split(String regex)returns a split string matching regex.
String[] s1.split(String regex, int limit)returns a split string matching regex and limit.
int s1.indexOf(int ch)returns the specified char value index.
int s1.indexOf(int ch, int fromIndex)returns the specified char value index starting with given index.
int s1.indexOf(String substring)returns the specified substring index.
int s1.indexOf(String substring, int fromIndex)returns the specified substring index starting with given index.
String s1.toLowerCase()returns a string in lowercase.
String s1.toUpperCase()returns a string in uppercase.
String s1.trim()removes the beginning and ending spaces of this string.
String String.valueOf(int value)converts the given type into string. It is an overloaded method.

Math Class

If the return type is of the type same as an argument then we are using var

MethodsDescription
var Math.abs(a)return the Absolute value of the given value.
var Math.max(a,b)returns the Largest of two values.
long Math.round(float/double x)round off the decimal numbers to the nearest value.
double Math.sqrt(double x)return the square root of a number.
double Math.cbrt(double x)return the cube root of a number.
double Math.pow(double a, double b)returns the value of first argument raised to the power to second argument.
double Math.signum(a) => (a>0): 1.0 / (a<0): -1.0 / (a==0): 0used to find the sign of a given value.
double Math.ceil(double x)used to find the smallest integer value that is greater than or equal to the argument or mathematical integer.
double Math.floor(double a)used to find the largest integer value which is less than or equal to the argument
double Math.random()returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
double Math.log(double x)returns the natural logarithm.
double Math.log10(double x)return the base 10 logarithm of a double value.
double Math.exp(double x)returns E raised to the power of a double value
double Math.sin/cos/tan/asin/acos/atan(double a)return the trigonometric value.
double sinh/tanh/cosh(double x)return the trigonometric Hyperbolic value.

Integer Class

MethodsDescription
int Integer.compare(int x,int y)compare two int x>y => 1 \ x=y => 0 \ x -1
int x.compareTo(int x)compare two int x>y => 1 \ x=y => 0 \ x -1
boolean Integer.equals(Object obj)compares the value of the parameter to the value of the current Integer object and returns boolean ( True or False ).
int object.intValue()returns the value of the specified number as an int.
long object.longValue()returns the value of the specified number as an long.
int Integer.parseInt (String s)parses the String argument as a signed decimal integer object
int Integer.parseInt (String s, int radix)parses the String argument as a signed decimal integer object in the specified radix by the second argument
int Integer.parseInt (CharSequence s,int beginIndex,int endIndex,int radix)parses the CharSequence argument as a signed integer in the specified radix argument, beginning at the specified beginIndex and extending to endIndex - 1.
int Integer.reverse(int i)method returns the numeric value obtained by reversing order of the bits in the specified int value.
int Integer.rotateLeft(int i, int distance)returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits.
int Integer.rotateRight(int i, int distance)returns the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.
int Integer.signum(int i)(a>0): 1.0 / (a<0): -1.0 / (a==0): 0
String Integer.toBinaryString (int i)returns a string representation of the integer argument as an unsigned integer in binary base 2.
String Integer.toHexString (int i)returns a string representation of the integer argument as an unsigned integer in binary base 16.
String Integer.toOctalString (int i)returns a string representation of the integer argument as an unsigned integer in binary base 8.
String a.toString()returns a String object representing the value of the Number Object.(int/float/double/boolean etc)
String Integer.toString(int i)returns a string representation of the int type argument in base 10.
String Integer.toString(int i, int radix)returns a string representation of the int type argument in the specified radix.(First int to radix conversion)
Integer Integer.valueOf(int i)returns the relevant Integer Object holding the value of the argument passed.
Integer Integer.valueOf(String s)returns the relevant Integer Object holding the value of the argument passed.
Integer Integer.valueOf(String s, int radix)convert to int and chnages be base from radix to 10.

*valueOf is present in both Integer and String, Integer.valueOf() give Integer and String.valueOf() gives String

Collections

Alt text

Collection<E> P = new "some type"<E>();
Collection<E> Q = new "some type"<E>();
MethodsDescription
boolean P.add(E element)This method is used to add an object to the collection.
boolean P.addAll(Q);This method adds all the elements in the given collection to this collection.
P.clear()used to clear the Collection upon
boolean P.contains(Object element)returns true if the collection contains the specified element.
boolean P.containsAll(Q)returns true if the collection contains all of the elements in the given collection.
boolean P.equals(Q)This method compares the specified object with this collection for equality.
int P.hashCode()return the hash code value for this collection.
boolean P.isEmpty()This method returns true if this collection contains no elements.
Collections.max(P)used to return the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface.
E P.remove(Object o)This method is used to remove the given object from the collection. If there are duplicate values, then this method removes the first occurrence of the object.
E P.removeAll(Q)This method is used to remove all the objects mentioned in the given collection from the collection.
int P.size()This method is used to return the number of elements in the collection.
Object[] objects = P.toArray();This method is used to return an array containing all of the elements in this collection. toArray() method returns an array of type Object(Object[]). We need to typecast it to Integer before using as Integer objects.

Iterator

Syntax :

Iterator i = Collection.iterator();
MethodsDescription
boolean i.hasNext()returns true if Iterator has more element to iterate.
Object i.next()returns the next element in the collection until the hasNext()method return true.
void i.remove()removes the current element in the collection.

List Iterator

List Iterator in Java is an Iterator which allows users to traverse Collection in both direction.

Syntax:

ListIterator li = list.listIterator();
MethodsDescription
void li.add(Object object)It inserts object immediately before the element that is returned by the next( ) function.
boolean li.hasNext( )returns true if the list has a next element.
boolean li.hasPrevious( ):returns true if the list has a previous element.
Object li.next( )returns the next element of the list.
Object li.previous( )returns the previous element of the list.
void li.remove( )removes the current element from the list.

ArrayList

Syntax:

ArrayList <E> x = new ArrayList<E>();
MethodsDescription
int x.size()returns length of an ArrayList
void x.add(int index, E element)used to insert the specified element at the specified position in a list.
boolean x.add(E e)used to append the specified element at the end of a list.
boolean x.addAll(Collection c)used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
boolean x.addAll(int index, Collection c)used to append all the elements in the specified collection, starting at the specified position of the list.
void x.clear()used to remove all of the elements from this list.
E x.get(int index)used to fetch the element from the particular position of the list.
boolean x.isEmpty()returns true if the list is empty, otherwise false.
Object[] x.toArray()used to return an array containing all of the elements in this list in the correct order.
Object x.clone()used to return a shallow copy of an ArrayList.
boolean x.contains(Object o)returns true if the list contains the specified element
int x.indexOf(Object o)used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
E x.remove(int index)to remove the element present at the specified position in the list.
boolean x.remove(Object o)used to remove the first occurrence of the specified element.
boolean x.removeAll(Collection c)used to remove all the elements from the list.
void x.removeRange(int fromIndex, int toIndex)to remove all the elements lies within the given range.
E x.set(int index, E element)used to replace the specified element in the list, present at the specified position.
void x.sort(Comparator<? super E> c)used to sort the elements of the list on the basis of specified comparator.
List<E> subList(int fromIndex, int toIndex)used to fetch all the elements lies within the given range.

LinkedList

Syntax:

LinkedList<E> x =new LinkedList<E>();

E: data type

MethodsDescription
boolean x.add(E e)to append the specified element to the end of a list.
void x.add(int index, E element)to insert the specified element at the specified position index in a list.
boolean x.addAll(Collection <E> c)used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
boolean x.addAll(int index, Collection<E> c)used to append all the elements in the specified collection, starting at the specified position of the list.
void x.addFirst(E e)used to insert the given element at the beginning of a list.
void x.addLast(E e)used to append the given element to the end of a list.
void x.clear()used to remove all the elements from a list.
Object x.clone()used to return a shallow copy of an ArrayList.
boolean x.contains(Object o)used to return true if a list contains a specified element.
E x.element()used to retrieve the first element of a list.
E x.get(int index)used to return the element at the specified position in a list.
int x.indexOf(Object o)used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element.
boolean x.offer(E e)adds the specified element as the last element of a list.
E x.peek()retrieves the first element of a list
E x.poll()It retrieves and removes the first element of a list.
int x.size()used to return the number of elements in a list.
E x.set(int index, E element)It replaces the element at the specified position in a list with the specified element.
E x.remove(int index)used to remove the element at the specified position in a list.

Map

Alt text

Map<K,V> m=new HashMap<K,V>();
MethodsDescription
void m.put(Object key, Object value)used to insert an entry in the map.
void m.putAll(Map map)used to insert the specified map in the map.
void m.putIfAbsent(K key, V value)It inserts the specified value with the specified key in the map only if it is not already specified.
void m.remove(Object key)used to delete an entry for the specified key.
boolean m.remove(Object key, Object value)It removes the specified values with the associated specified keys from the map.
void m.clear()used to reset the map.
boolean m.containsValue(Object value)This method returns true if some value equal to the value exists within the map, else return false.
boolean containsKey(Object key)This method returns true if some key equal to the key exists within the map, else return false.
boolean equals(Object o)used to compare the specified Object with the Map.
V m.get(Object key)This method returns the object that contains the value associated with the key else returns null
boolean m.isEmpty()This method returns true if the map is empty; returns false if it contains at least one key.
int m.size()This method returns the number of entries in the map.

Queue

Since Queue is an interface, objects cannot be created of the type queue. We always need a class which extends this list in order to create an object.

Syntax:

Queue<E> pQueue = new PriorityQueue<E>();
Queue<E> lQueue = new LinkedList<E>();
Queue<E> pbq = new PriorityBlockingQueue<E>();
MethodsDescription
boolean q.add(object)This method is used to add elements at the tail of queue. More specifically, at the last of linked-list if it is used, or according to the priority in case of priority queue implementation.
boolean q.offer(object)This method is used to insert an element in the queue. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full since it returns false.
Object q.peek()This method is used to view the head(first in) of queue without removing it. It returns Null if the queue is empty.
Object q.element()This method is similar to peek(). It throws NoSuchElementException when the queue is empty.
Object q.remove()This method removes and returns the head of the queue. It throws NoSuchElementException when the queue is empty.
Object q.poll()This method removes and returns the head of the queue. It returns null if the queue is empty.

Did you find this article valuable?

Support Atharva Shah by becoming a sponsor. Any amount is appreciated!