Back to Cheat SheetsLanguage · Beginner

Java cheat sheet

Collections, strings, streams and exception handling used daily in automation code.

Collections

List

Ordered, allows duplicates

List<String> l = new ArrayList<>();

Set

Unique elements, no order guarantee

Set<String> s = new HashSet<>();

Map

m.put(k,v) / m.getOrDefault(k,0)

Map<String,Integer> m = new HashMap<>();

Iterate map

for (var e : m.entrySet()) { e.getKey(); e.getValue(); }

Strings

Split

"a,b".split(",")

Reverse

new StringBuilder(s).reverse().toString()

Compare ignoring case

Never use == for strings

a.equalsIgnoreCase(b)

Format

String.format("id=%s", id)

Streams

Filter + collect

list.stream().filter(x -> x.length() > 3).toList()

Map

list.stream().map(String::toUpperCase).toList()

Any match

list.stream().anyMatch(s -> s.contains("QA"))

Exceptions

Try/catch/finally

try { ... } catch (IOException e) { ... } finally { ... }

Custom exception

class TestDataException extends RuntimeException { }

Checked vs unchecked

Checked must be declared or handled

IOException vs NullPointerException

Go beyond the reference

Book a free demo class and apply these commands on live project work.