0% found this document useful (0 votes)
30 views4 pages

Java_Cheat_Sheet_With_All_BigO

This cheat sheet provides initialization examples and methods for various Java data structures such as Strings, Lists, Sets, Maps, Stacks, and Queues. It also includes tree traversal methods, graph representation using adjacency lists, Java Streams operations, utility methods, conversions, and a summary of Big-O time complexities for different data structures. The document serves as a quick reference for Java developers to understand and use common data structures and their functionalities.

Uploaded by

Ola Alshatnawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Java_Cheat_Sheet_With_All_BigO

This cheat sheet provides initialization examples and methods for various Java data structures such as Strings, Lists, Sets, Maps, Stacks, and Queues. It also includes tree traversal methods, graph representation using adjacency lists, Java Streams operations, utility methods, conversions, and a summary of Big-O time complexities for different data structures. The document serves as a quick reference for Java developers to understand and use common data structures and their functionalities.

Uploaded by

Ola Alshatnawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Data Structures & Utilities Cheat Sheet

Initialization Examples

// Strings
String str = "hello";

// StringBuilder
StringBuilder sb = new StringBuilder();

// List
List<String> list = new ArrayList<>();
List<Integer> list = Arrays.asList(1, 2, 3);

// Set
Set<String> set = new HashSet<>();
Set<Integer> set = new TreeSet<>();

// Map
Map<Integer, Integer> map = new HashMap<>();
Map<String, String> map = new TreeMap<>();

// Hashtable
Hashtable<Integer, String> ht = new Hashtable<>();

// Queue
Queue<Integer> queue = new LinkedList<>();

// Stack
Stack<Integer> stack = new Stack<>();

String Methods

str.length();
str.charAt(0);
str.substring(1, 3);
str.indexOf("e");
str.lastIndexOf("e");
str.equals("hello");
str.equalsIgnoreCase("HELLO");
str.contains("ell");
str.startsWith("he");
str.endsWith("lo");
str.toCharArray();
str.split(" ");
str.replace("e", "a");
str.toUpperCase();
str.toLowerCase();
str.trim();
str.isEmpty();
str.concat("world");
Java Data Structures & Utilities Cheat Sheet

StringBuilder Methods

sb.append("hello");
sb.insert(1, "abc");
sb.delete(1, 3);
sb.replace(0, 2, "xy");
sb.reverse();
sb.length();
sb.toString();

List, Set, Map, Stack, Queue Methods

list.add("a"); list.add(1, "b");


list.remove(0); list.set(1, "z");
list.get(0); list.contains("a");

set.add("x"); set.remove("x"); set.contains("x");

map.put(1, "one"); map.get(1);


map.containsKey(1); map.containsValue("one");
map.remove(1);

stack.push(1); stack.pop(); stack.peek();

queue.offer(2); queue.poll(); queue.peek();

Tree Data Structure

class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
}

// Traversals
void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
System.out.print(root.val);
inorder(root.right);
}

Graph (Adjacency List)

Map<Integer, List<Integer>> graph = new HashMap<>();


graph.computeIfAbsent(u, k -> new ArrayList<>()).add(v);

// BFS
Queue<Integer> q = new LinkedList<>();
Java Data Structures & Utilities Cheat Sheet

Set<Integer> visited = new HashSet<>();


q.offer(start);
while (!q.isEmpty()) {
int node = q.poll();
for (int neighbor : graph.getOrDefault(node, new ArrayList<>())) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
q.offer(neighbor);
}
}
}

Java Streams (With Comments)

list.stream().filter(x -> x > 10).collect(Collectors.toList()); // filter > 10


list.stream().map(x -> x * 2).collect(Collectors.toList()); // double values
list.stream().sorted().collect(Collectors.toList()); // sort list
list.stream().distinct().collect(Collectors.toList()); // remove duplicates
list.stream().mapToInt(Integer::intValue).sum(); // sum of elements
list.stream().anyMatch(x -> x == 5); // any match 5

Utility Methods

Math.max(a, b); Math.min(a, b); Math.abs(x);


Math.pow(2, 3); Math.sqrt(16); Math.random();

Collections.sort(list); Collections.reverse(list);
Collections.max(list); Collections.min(list);

Java Conversions

String str = Integer.toString(123);


int num = Integer.parseInt("123");

String[] arr = {"a", "b"};


List<String> list = Arrays.asList(arr);
String[] back = list.toArray(new String[0]);

List<String> list = Arrays.asList("a", "b");


String joined = String.join(",", list);
List<String> splitList = Arrays.asList(joined.split(","));

int[] intArray = list.stream().mapToInt(Integer::parseInt).toArray();

Big-O Time Complexities

| Data Structure | Access | Search | Insert | Delete |


|------------------|--------|--------|--------|--------|
| Array | O(1) | O(n) | O(n) | O(n) |
| ArrayList | O(1) | O(n) | O(n) | O(n) |
Java Data Structures & Utilities Cheat Sheet

| LinkedList | O(n) | O(n) | O(1) | O(1) |


| Stack | O(n) | O(n) | O(1) | O(1) |
| Queue | O(n) | O(n) | O(1) | O(1) |
| Deque | O(1) | O(n) | O(1) | O(1) |
| HashMap | - | O(1) | O(1) | O(1) |
| TreeMap | - | O(log n)| O(log n)|O(log n)|
| Hashtable | - | O(1) | O(1) | O(1) |
| HashSet | - | O(1) | O(1) | O(1) |
| TreeSet | - | O(log n)|O(log n)|O(log n)|
| PriorityQueue | - | O(log n)|O(log n)|O(log n)|

You might also like