Java Commonly Used Functions

At the Summer of 2022, I converted my main programming language from C++ to Java. This is because some Java’s great features are appealing to me and Java is also widely used in software development industry. In this post, I will put some commonly used Java operations for doing leetCode.


Numerical Operation

Initialize with Max or Min value

int outcome = Integer.MAX_VALUE;

Max(a, b)

Math.max(dp[i + 1][j], dp[i][j - 1])

Character Operation

Check if a character is uppercase

if (Character.isUpperCase(word.charAt(0))) {}

Check if a character is lowercase

if (Character.isLowerCase(word.charAt(i))) {}

Check if a character is a digit

if (Character.isDigit(str.charAt(i))) {}

Array Operation

Initialize an array

Arrays.fill(dp, Boolean.FALSE);

Sort an array

Arrays.sort(nums);
// Under the hood, it uses a Dual-Pivot Quicksort algorithm. Its internal implementation from the JDK 10 is typically faster than traditional one-pivot Quicksort.

// This algorithm offers O(n log(n)) average time complexity. That's a great average sorting time for many collections to have. Moreover, it has the advantage of being completely in place, so it does not require any additional storage.

// Though, in the worst case, its time complexity is O(n^2). 

Copy an array

int[] runArr = Arrays.copyOf(arr, arr.length);

String Operation

Find the index of a character

int posOfE = s.indexOf('E');

SubString

sOutcome = s.substring(i, j + 1); // s.charAt(j + 1) is not included.

String -> char[]

char[] arr = s.toCharArray();

char[] -> String

String content = new String(arr);

int -> String

String s = Integer.toString(x);

Data Structure Operation

ArrayList / HashSet -> int[]

int[] arr = outcome.stream().mapToInt(i -> i).toArray();

Initialize an ArrayList

result.add(Arrays.asList(nums[i], nums[left], nums[right]));

Insert an element at specified index of a list

list.add(index, element);

Append elements in the specified collection to the end of the list

list.addAll(map.values());

Reverse a list

Collections.reverse(list);

Deque

public static void main(String[] args) {
    Deque<String> myDeque = new ArrayDeque<>();

    myDeque.addFirst("First Element");
    myDeque.addLast("Last Element");

    System.out.println(myDeque.size());

    System.out.println(myDeque.getFirst());
    System.out.println(myDeque.getLast());
    
    System.out.println(myDeque.removeLast());
    System.out.println(myDeque.removeFirst());
}

Stack

Stack<Integer> st = new Stack<>();
st.push(i);
st.pop();
st.peek();

Queue

Queue<Integer> que = new LinkedList<>();
que.offer(i);
que.poll();
que.peek();

Priority Queue

PriorityQueue<int[]> pQue = new PriorityQueue<>((arr1, arr2)->arr1[1] - arr2[1]); // small -> big
pQue.add(10);
pQue.poll(); // Get the top element and removing it
pQue.peek(); // Get the top element of PriorityQueue

Map

myMap.containsKey(nums[i])
myMap.put(nums[i], i);
myMap.get(nums[i])

Map.Entry

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {// Loop}
entry.getKey();
entry.getValue();

StringBuilder

sb.append("Hello World!");
sb.deleteCharAt(0); // Delete a character in the string builder
String s = new String(arr); // Using StringBuilder to construct a new string