0% found this document useful (0 votes)
366 views1 page

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)
366 views1 page

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

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

[Link](); [Link](0); [Link](1, 3); [Link]("e"); [Link]("ell");


[Link]("he"); [Link]("lo"); [Link]("hello"); [Link](" "); [Link]("e", "a");

StringBuilder Methods

[Link]("hello"); [Link](1, "abc"); [Link](1, 3); [Link](0, 2, "xy"); [Link]();

List, Set, Map, Stack, Queue Methods

[Link]("a"); [Link](0); [Link](1, "z"); [Link]("x"); [Link]("x");


[Link](1, "one"); [Link](1); [Link](1); [Link](1);
[Link](1); [Link](); [Link](); [Link](2); [Link]();

Tree

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


void inorder(TreeNode root) { if (root == null) return; inorder([Link]); [Link]([Link]);
inorder([Link]); }

Graph (Adjacency List)

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


[Link](u, k -> new ArrayList<>()).add(v);
Queue<Integer> q = new LinkedList<>(); Set<Integer> visited = new HashSet<>();
[Link](start); while (![Link]()) { int node = [Link](); for (int n : [Link](node, new ArrayList<>()))
{ if (![Link](n)) { [Link](n); [Link](n); } } }

Streams

[Link]().filter(x -> x > 10).collect([Link]()); // > 10


[Link]().map(x -> x * 2).collect([Link]()); // double values
[Link]().distinct().sorted().collect([Link]()); // unique + sort

Utilities & Conversions

[Link](a,b); [Link](16); [Link](); [Link](list); [Link](list);


String str = [Link](123); int n = [Link]("123");
List<String> list = [Link]("a", "b"); String joined = [Link](",", 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