0% found this document useful (0 votes)
92 views

Java_Cheat_Sheet_Page

This cheat sheet provides a quick reference for Java data structures and utilities, including initialization examples, common methods for strings, lists, sets, maps, stacks, and queues. It also covers tree and graph implementations, stream operations, utility functions, and time complexities for various data structures. The document serves as a concise guide for developers to efficiently utilize Java's data handling capabilities.

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)
92 views

Java_Cheat_Sheet_Page

This cheat sheet provides a quick reference for Java data structures and utilities, including initialization examples, common methods for strings, lists, sets, maps, stacks, and queues. It also covers tree and graph implementations, stream operations, utility functions, and time complexities for various data structures. The document serves as a concise guide for developers to efficiently utilize Java's data handling capabilities.

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/ 1

Java Data Structures & Utilities - One Page Cheat Sheet

Initialization Examples

String str = "hello"; StringBuilder sb = new StringBuilder();


List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>();
Map<Integer, Integer> map = new HashMap<>(); Hashtable<Integer, String> ht = new Hashtable<>();
Queue<Integer> queue = new LinkedList<>(); Stack<Integer> stack = new Stack<>();

String Methods

str.length(); str.charAt(0); str.substring(1, 3); str.indexOf("e"); str.contains("ell");


str.startsWith("he"); str.endsWith("lo"); str.equals("hello"); str.split(" "); str.replace("e", "a");

StringBuilder Methods

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

List, Set, Map, Stack, Queue Methods

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


map.put(1, "one"); map.get(1); map.containsKey(1); map.remove(1);
stack.push(1); stack.pop(); stack.peek(); queue.offer(2); queue.poll();

Tree

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


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);
Queue<Integer> q = new LinkedList<>(); Set<Integer> visited = new HashSet<>();
q.offer(start); while (!q.isEmpty()) { int node = q.poll(); for (int n : graph.getOrDefault(node, new ArrayList<>()))
{ if (!visited.contains(n)) { visited.add(n); q.offer(n); } } }

Streams

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


list.stream().map(x -> x * 2).collect(Collectors.toList()); // double values
list.stream().distinct().sorted().collect(Collectors.toList()); // unique + sort

Utilities & Conversions

Math.max(a,b); Math.sqrt(16); Math.random(); Collections.sort(list); Collections.reverse(list);


String str = Integer.toString(123); int n = Integer.parseInt("123");
List<String> list = Arrays.asList("a", "b"); String joined = String.join(",", list);

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) |
| 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