Input and Output Operatinos
Basic Scanner Usage
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int elements = reader.nextInt();
}
}
Fixed-Length Input Reading
Scanner inputReader = new Scanner(System.in);
int count = inputReader.nextInt();
int base = inputReader.nextInt();
for (int i = 0; i < count; i++) {
int parent = inputReader.nextInt();
int leftChild = inputReader.nextInt();
int rightChild = inputReader.nextInt();
}
Varible-Length Input Reading
while (inputReader.hasNext()) {
int firstVal = inputReader.nextInt();
int secondVal = inputReader.nextInt();
System.out.println(firstVal + secondVal);
}
Line Processing
while (inputReader.hasNextLine()) {
String[] tokens = inputReader.nextLine().split(" ");
int total = 0;
for (String token : tokens) {
total += Integer.parseInt(token);
}
System.out.println(total);
}
BufferedReader Template
import java.io.*;
class FastIO {
private BufferedReader buffer;
private BufferedWriter writer;
public FastIO() {
buffer = new BufferedReader(new InputStreamReader(System.in));
writer = new BufferedWriter(new OutputStreamWriter(System.out));
}
public int[] readInts() throws IOException {
return Arrays.stream(buffer.readLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
}
public void write(String output) throws IOException {
writer.write(output);
}
public void close() throws IOException {
writer.close();
buffer.close();
}
}
String and Numeric Operations
StringBuilder Methods
StringBuilder builder = new StringBuilder();
builder.append(10);
builder.insert(3, "text");
builder.delete(1, 4);
String result = builder.toString();
String Utilities
String text = "A1B2C3";
char[] chars = text.toCharArray();
String[] parts = text.split("\\d");
String formatted = String.format("%.2f", 123.456);
Integer Conversions
int value = Integer.parseInt("42");
String binary = Integer.toBinaryString(10);
int reversed = Integer.reverse(0x0F);
Bitwise Operations
int flags = 0b1010 | 0b1100;
int shiftLeft = flags << 2;
int shiftRight = flags >>> 1;
Data Structures
Arrays Sorting
int[] numbers = {5, 2, 9};
Arrays.sort(numbers);
int[][] matrix = {{3,1}, {2,4}};
Arrays.sort(matrix, (a, b) -> a[0] - b[0]);
Custom Sorting
class CustomData implements Comparable<CustomData> {
int width, height;
public int compareTo(CustomData other) {
return this.width != other.width ?
this.width - other.width :
other.height - this.height;
}
}
Collections.sort(dataList);
PriorityQueue
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
maxHeap.add(5);
maxHeap.add(3);
int top = maxHeap.poll();
TreeMap Operations
TreeMap<Integer, String> tree = new TreeMap<>();
tree.put(3, "C");
tree.put(1, "A");
Integer ceilKey = tree.ceilingKey(2);
HashSet with Custom Objects
class Point {
int x, y;
@Override
public boolean equals(Object o) {
Point p = (Point) o;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
Set<Point> coordinates = new HashSet<>();
Utility Classes
Math Funcsions
int absolute = Math.abs(-5);
double upper = Math.ceil(4.3);
double lower = Math.floor(4.7);
Random Numbers
Random rand = new Random();
int randomValue = rand.nextInt(100);
Stream Processing
List<String> items = Arrays.asList("A", "", "B");
long emptyCount = items.stream()
.filter(String::isEmpty)
.count();
int[] intArray = items.stream()
.filter(s -> !s.isEmpty())
.mapToInt(Integer::parseInt)
.toArray();