Show Menu
Cheatography

Java cheatsheet Cheat Sheet (DRAFT) by

Cheatsheet for java PL

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Math Library

Math is a final class in java
Which means it cannot be inherited
Math.a­bs(-1)
1 //returns the absolute value
Math.c­eil­(-1.9)
returns: -1.0 //Returns the smallest integer that is greater than or equal to the specified number.
Math.f­loo­r(-1.9)
returns: -2.0 // Returns the largest integer that is less than or equal to the specified number.
Math.m­ax(1, 2)
Returns the larger of the two specified numbers.
Math.m­in(1, 2)
returns: 1
Math.p­ow(2, 3)
returns: 8.0, Returns the value of the first argument raised to the power of the second argument.
Math.r­andom()
returns: 0.0 <= x < 1.0, Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Math.r­oun­d(1.1)
returns: 1, Returns the closest long or int, as indicated by the method's return type, to the argument
Math.r­oun­d(1.5)
returns: 2,
Math.s­qrt(4)
returns: 2.0, Returns the correctly rounded positive square root of a double value.

Treeset

Set<In­teg­er> treeSet = new TreeSe­t<I­nte­ger­>();
Set interface does not have ceiling() method
TreeSe­t<I­nte­ger> treeSet = new TreeSe­t<I­nte­ger­>();
if we use Set interface rather than TreeSet class, we cannot use ceiling() method
treeSe­t.a­dd(10);
adds the element to the treeset
treeSe­t2.a­dd­All­(tr­eeSet);
System.ou­t.p­rin­tln­("tr­eeSet2 after adding elemen­ts" + treeSet2);
print treeeSet
treeSe­t.r­emo­ve(20);
removes the specific element from the treeset
for (Integer item : treeSet) { System.ou­t.p­rin­tln­(item); }
Iterating over tree set items
Iterat­or<­Int­ege­r> iterator = treeSe­t.i­ter­ator();
Iterating using iterator
Iterat­or<­Int­ege­r> descen­din­gIt­erator = treeSe­t.d­esc­end­ing­Ite­rat­or();
returns an iterator over the elements in this set in descending order.
int value1 = treeSe­t.c­eil­ing­(25);
returns the least element in this set greater than or equal to the given element
int value2 = treeSe­t.f­loo­r(25);
returns the greatest element in this set less than or equal to the given element
Set<In­teg­er> descen­dingSet = treeSe­t.d­esc­end­ing­Set();
returns a reverse order view of the elements contained in this set.
Set<In­teg­er> headSet = treeSe­t.h­ead­Set­(30);
returns a view of the portion of this set whose elements are strictly less than toElement.
Set<In­teg­er> headSet2 = treeSe­t.h­ead­Set(30, true);
returns a view of the portion of this set whose elements are less than or equal to toElement.
Set<In­teg­er> tailSet = treeSe­t.t­ail­Set­(30);
returns a view of the portion of this set whose elements are greater than or equal to fromEl­ement.
Set<In­teg­er> tailSet2 = treeSe­t.t­ail­Set(30, false);
returns a view of the portion of this set whose elements are strictly greater than fromEl­ement.
Set<In­teg­er> subSet = treeSe­t.s­ubS­et(20, 40);
returns a view of the portion of this set whose elements range from fromEl­ement, inclusive, to toElement, exclusive.
Set<In­teg­er> subSet2 = treeSe­t.s­ubS­et(20, true, 40, true);
returns a view of the portion of this set whose elements range from fromEl­ement, inclusive, to toElement, inclusive.
Integer pollFirst = treeSe­t.p­oll­Fir­st();
retrieves and removes the first (lowest) element, or returns null if this set is empty.
Integer pollLast = treeSe­t.p­oll­Last();
retrieves and removes the last (highest) element, or returns null if this set is empty.
boolean remove = treeSe­t.r­emo­ve(20);
returns true if this set contained the specified element
boolean isEmpty = treeSe­t.i­sEm­pty();
returns true if this set contains no elements.
int size = treeSe­t.s­ize();
returns the number of elements in this set (its cardin­ality).
boolean contains = treeSe­t.c­ont­ain­s(20);
returns true if this set contains the specified element.
treeSe­t.c­lear();
TreeSet is implem­ented using a tree struct­ure­(re­d-black tree in algorithm book)
TreeSet is ordered, sorted, and navigable
TreeSet does not allow duplicate elements
TreeSet allows null elements

Primitive Data types

byte
Size: 1 byte
Range: -128 to 127
short
Size: 2 bytes
Range: -32,768 to 32,767
int
Size: 4 bytes
Range: -2,147­,48­3,648 to 2,147,­483,647
long
Size: 8 bytes
Range: -9,223­,37­2,0­36,­854­,77­5,808 to 9,223,­372­,03­6,8­54,­775,807
float
Size: 4 bytes
Range: 3.4e−038 to 3.4e+038
double
Size: 8 bytes
Range: 1.7e−308 to 1.7e+308
boolean
Size: 1 bit
Range: true or false
char
Size: 2 bytes
Range: 0 to 65,536

Operators

a * b
Multip­lic­ation
a + b
a - b
a / b
Division
a % b
Modulo
a++
Postin­crement
++a
Preinc­rement
b--
Postde­crement
--b
Predec­rement
a operator= 3
+, -, *, /, %, <<, >>, >>>
a operator 0b1010
&, |, ^
a relati­ona­lOp­erator b
==, !=, <, <=, >, >=
a logica­lOp­erator b
&&, ||,
!x
not operator
TODO: terenary
TODO: shift operator
 

loops

for (int number : numbers) { System.ou­t.p­rin­tln­("El­ement: " + number); }
for (int i = 0; i < number­s.s­ize(); i++) { int element = number­s.g­et(i); System.ou­t.p­rin­tln­("El­ement at index " + i + ": " + element); }

Set

The set interface is present in java.util package
which is extended from collection interface
Interfaces extended from Set interface:
SortedSet, Naviga­bleSet
Classes that implements Set Interface
HashSet, EnumSet, Linked­Has­hSet, TreeSet
SortedSet Interface
contains the methods inherited from the Set interface and adds a feature that stores all the elements in this interface to be stored in a sorted manner
Navigable Interface:
provides the implem­ent­ation to navigate through the Set.
Treeset
implem­ent­ation of a self-b­ala­ncing tree
set is implem­ented using hash table
set is unordered
set does not allow duplicate elements
set allows null elements

Linked­HashSet

create a linked­HashSet of characters
Set<Ch­ara­cte­r> linked­HashSet = new Linked­Has­hSe­t<C­har­act­er>();
add element
linked­Has­hSe­t.a­dd(­'a');
remove the element
linked­Has­hSe­t.r­emo­ve(­'a');
check if the element is present
linked­Has­hSe­t.c­ont­ain­s('a')
size
linked­Has­hSe­t.s­ize()
is empty
linked­Has­hSe­t.i­sEm­pty()
Iterator
Iterator iterator = linked­Has­hSe­t.i­ter­ator();
to Array
linked­Has­hSe­t.t­oAr­ray()
to string
linked­Has­hSe­t.t­oSt­ring()
equals
linked­Has­hSe­t.e­qua­ls(­lin­ked­Has­hSet)
hashCode()
linked­Has­hSe­t.h­ash­Code()
linked­HashSet is a subclass of HashSet
linked­HashSet maintains insertion order
linked­HashSet is slower than HashSet
linked­HashSet is faster than TreeSet
when to use: when you want to maintain insertion order
when not to use: when you want to sort the elements

HashSet

Creating the hashSet
Set<St­rin­g> hashSet = new HashSe­t<S­tri­ng>();
Adding element
hashSe­t.a­dd(­"­ele­men­t1");
Print hashSet
System.ou­t.p­rin­tln­("ha­shset after adding elemen­ts" + hashSet);
remove element
hashSe­t.r­emo­ve(­"­ele­men­t2");
Iterating over hash set items
for (String item : hashSet) {Syste­m.o­ut.p­ri­ntl­n(i­tem);}
// Iterating using iterator
Iterat­or<­Str­ing> iterator = hashSe­t.i­ter­ator();
checking if hashSet contains an element
hashSe­t.c­ont­ain­s("e­lem­ent­1")
checking if hashSet is empty
hashSe­t.i­sEm­pty()
checking size of hashSet
hashSe­t.s­ize()
clearing hashSet
hashSe­t.c­lear();
Hashset is implem­ented using hash table
Hashset is unordered
Hashset does not allow duplicate elements
Hashset allows null elements

Enum hashset

create enumSet
EnumSe­t<D­ays> hashSet = EnumSe­t.o­f(D­ays.MO­NDAY, Days.T­UESDAY, Days.W­EDN­ESDAY, Days.T­HUR­SDAY, Days.F­RIDAY);
[MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]
EnumSe­t<D­ays> workin­gDays = EnumSe­t.r­ang­e(D­ays.MO­NDAY, Days.F­RIDAY);
[SATURDAY, SUNDAY]
EnumSe­t<D­ays> weekEnds = EnumSe­t.c­omp­lem­ent­Of(­wor­kin­gDays);
[MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
EnumSe­t<D­ays> allDays = EnumSe­t.a­llO­f(D­ays.cl­ass);
[]
EnumSe­t<D­ays> noDays = EnumSe­t.n­one­Of(­Day­s.c­lass);
add
noDays.ad­d(D­ays.MO­NDAY);
remove
noDays.re­mov­e(D­ays.MO­NDAY);
check if element is present
noDays.co­nta­ins­(Da­ys.M­ONDAY)
check if enum hashset are equal
weekDa­ys.e­qu­als­(no­Days)
iterator
Iterator iterator = noDays.it­era­tor();
get the size
noDays.size()
 
noDays.to­Array()
 
noDays.cl­ear();
when to use: when you want to use a set of enum constants
when not to use: when you want to use a set of non-enum constants