Show Menu
Cheatography

Core Java Cheat Sheet (DRAFT) by

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

java.u­til.Li­st<­E>

ListIt­era­tor­<E> lister­Ope­rator()
returns a list iterator for visiting the elements of the list.
ListIt­era­tor­<E> lister­Ope­rat­or(int index)
returns a list iterator for visiting the elements of the list whose first call to
next
will return the element with the given index.
void add(int i, E element)
adds an element at the specified position.
void addAll(int i, Collec­tio­n<? extend E> elements)
adds all elements from a collection to the specified position.
E remove(int i)
removes and returns the element at the specified position.
E get(int i)
gets the element at the specified position.
E set(int i)
replaces the element at the specified position with a new element and returns the old element.
int indexO­f(O­bject element)
returns the position of the last occurence of an element equal to the specified element, or -1 if no matching element is found.
int lastIn­dex­Of(­Object element)
returns the position of the last occurrence of an element equal to the specified element, or -1 if no matching element is found.

java.u­til.Li­stO­per­ato­r<E>

void add(E newEle­ment)
adds an element before the current position.
void set(E newEle­ment)
replaces the last element visited by
next
or
previous
with a new element. Throws an
Illega­lSt­ate­Exc­eption
if the list structure was modified since the last call to
next
ore
previous
.
boolean hasPre­vious()
returns
true
if there is another element to visit when iterating backwards through the list.
E previous()
returns the previous object. Throws a
NoSuch­Ele­men­tEx­ception
if the beginning of the list has been reached.
int nextIn­dex()
returns the index of the element that would be returned by the next call to
next
.
int previo­usI­ndex()
returns the index of the element that would be returned by the next call to
previous
.

java.u­til.Li­nke­dLi­st<­E>

Linked­List()
constructs an empty linked list.
Linked­Lis­t(C­oll­ection <? extends E> elements)
constructs a linked list and adds all elements from a collec­tion.
void addFirst(E element)
void addLast(E element)
adds an element to the beginning or the end of the list.
E getFirst(E element)
E getLast(E element)
returns the element at the beginning or the end of the list.
E remove­First(E element)
E remove­Last(E element)
removes and returns the element at the beginning or the end of the list.

java.u­til.Ha­shS­et<­E>

HashSet()
constructs an empty hash set.
HashSe­t(C­oll­ect­ion­<? extends E> elements)
constructs a hash set and adds all elements from a collec­tion.
HashSe­t(int initia­lCa­pacity)
constructs an empty hash set with the specified capacity (number of buckets).
HashSe­t(int initia­lCa­pacity, float loadFactor )
constructs an empty hash set with the specified capacity and load factor (a number between 0.0 and 1.0 that determines at what percentage of fullness the hash table will be rehashed into a larger one).
 

java.l­ang.Object

int hashCode()
returns a hash code for this object. A hash code can be any integer, positive or negative. The defini­tions of
equals
and
hashCode
must be compat­ible: if
x.equa­ls(y)
is
true
, then
x.hash­Code()
must be the same value as
y.hash­Code()
.

java.u­til.Tr­eeS­et<­E>

TreeSet()
TreeSe­t(C­omp­ara­tor­<? super E> compar­ator)
constructs an empty tree set.
TreeSe­t(C­oll­ect­ion­<? extends E> elements)
TreeSe­t(S­ort­edS­et<­E> s)
constructs a tree set and adds all elements from a collection or sorted set (int the latter case, using the same ordering).

java.u­til.So­rte­dSe­t<E>

Compar­ato­r<? super E> compar­ator()
returns the comparator used for sorting the elements, or
null
if the elements are compared with the
compareTo
method of the
Comparable
interface.
E first()
E last()
returns the smallest or largest element in the sorted set.

java.u­til.Na­vig­abl­eSe­t<E>

E higher(E value)
E lower(E value)
returns the least element
> value
or the largest element
< value
, or
null
if there is no such element.
E ceiling(E value)
E floor(E value)
returns the least element
>= value
or the largest element
<= value
, or
null
if there is no such element.
E pollFi­rst()
E pollLast()
removes and returns the smallest or largest element in this set, or
null
if the set is empty.
Iterat­or<­E> descen­din­gIt­era­tor()
returns an iterator that traverses this set in descending direction.

java.u­til.Qu­eue­<E>

boolean add(E element)
boolean offer(E element)
adds the given element to the tail of this queue and returns
true
, provided the queue is not full. If the queue is full, the first method throws an
Illega­lSt­ate­Exc­eption
, whereas the second method returns
false
.
E remove()
E poll()
removes and returns the element at the head of this queue, provided the queue is not empty. If the queue is empty, the first method throws a
NoSuch­Ele­men­tEx­ception
, whereas the second method returns
null
.
E element()
E peek()
returns the element at the head of this queue without removing it, provided the queue is not empty. If the queue is empty, the first method throws a
NoSuch­Ele­men­tEx­ception
, whereas the second method returns
null
.