Basic Syntax Flashcards
for loop
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
number of elements in array
array.length
immutable list w/ literals
List<Integer> list=Arrays.asList(1, 2, 3);</Integer>
char array from String
char[] ch = str.toCharArray();
hash set from list
Set<String> set = new HashSet<>(list);</String>
number of elements in a queue
q.size()
java max function
Math.max
Create a HashMap
private HashMap<Integer, Integer> memo = new HashMap<Integer, Integer>();
Check if HashMap has key
hashMap.containsKey(i)
substring of a string
public String substring(int begIndex, int endIndex);
beginIndex : the begin index, inclusive.
endIndex : the end index, exclusive.
parse string to int
int i=Integer.parseInt(“200”);
number of characters in a string
s.length()
get larges integer values
Integer.MAX_VALUE
insert object into hashset
hashSet.add()
append an element to the end of a list
list.add(E e)
remove element from specific index of a list
list.remove(intindex)
number of elements in a list
list.size()
Removes the mapping for the specified key from this map if present.
HashMap.remove(Object key)
Returns the element at the specified position in this list.
List.get(int index)
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Math.random()
Create a Queue
Queue is an interface. The following classes implement it, PriorityQueue, LinkedList and ArrayDeque
Add element to Queue
offer()
Retrieve but do not remove item from Queue
peek()
Retrieves and removes the head of the queue, or returns null
poll()