1. String s = [Link]().
toLowerCase();
String [] str = [Link](" ");
It will split the line by a space and create a array of string
2. While creating function always create outside the main function by using public static return
type function name
3. To get the ith index of string use [Link](i)
4. To get the length of string use [Link]() and for array use [Link]
5. To convert a char to int use : int p = [Link]([Link]([Link](i)));
6. Array declaration
int n=[Link]();
int [][]t=new int[n][n];
to get the max and min use [Link](a,b);
7. String str = "chandrabhushan"; // Convert string to a character array char[] charArray =
[Link](); // Sort the character array [Link](charArray);
8. To check weather a charestor is int or not use [Link](ch);
9. [Link]() is a static method in the [Link] class that provides a simple way to print
an array in a human-readable format. Instead of printing the memory reference of the array, it
converts the elements of the array into a string representation.
int[] numbers = {1, 2, 3, 4, 5};
[Link]([Link](numbers)); // op: [1, 2, 3, 4, 5]
10. To sort the array using opposite way we can use:
[Link]();
i.e : [Link](list,[Link]());
double d = 123.4567;
[Link]("%.3f\n", d);
String formatted = [Link]("%.2f", d);
d = [Link](formatted);
[Link](d);
BigInteger b,a;
b = [Link](4);
a = [Link](6);
[Link](b);
BigInteger c = [Link](a);
BigInteger d = [Link](b);
[Link](d);
[Link](c);
//Similarly we have BigDecimal for large floating point number;
Arrays Class
Commonly Used Methods in the Arrays Class
Sorting Arrays:
[Link](array): Sorts the specified array into ascending numerical order.
[Link](array, fromIndex, toIndex): Sorts the specified range of the array into ascending
order.
Searching Arrays:
[Link](array, key): Searches the specified array for the specified value using the
binary search algorithm. The array must be sorted.
Filling Arrays:
[Link](array, value): Assigns the specified value to each element of the specified array.
[Link](array, fromIndex, toIndex, value): Assigns the specified value to each element of the
specified range of the array.
Copying Arrays:
[Link](original, newLength): Copies the specified array, truncating or padding with zeros
(if necessary) so the copy has the specified length.
[Link](original, from, to): Copies the specified range of the specified array into a
new array.
Comparing Arrays:
[Link](array1, array2): Returns true if the two specified arrays are equal to one another.
[Link](array1, array2): Returns true if the two specified arrays are deeply equal to
one another (used for nested arrays).
Converting Arrays to String:
[Link](array): Returns a string representation of the contents of the specified array.
[Link](array): Returns a string representation of the deep contents of the specified
array (used for nested arrays).
Collections class
List<String> list = new ArrayList<>([Link]("Banana", "Apple", "Cherry")); [Link](list);
List<Integer> list = new ArrayList<>([Link](1, 2, 3, 4, 5));
[Link](list);
[Link](list); // Output: A shuffled list, e.g., [3, 5, 1, 4, 2]
List<Integer> list = new ArrayList<>([Link](1, 2, 3, 4, 5));
[Link](list);
[Link](list); // Output: [5, 4, 3, 2, 1]
List<Integer> list = new ArrayList<>([Link](1, 2, 3, 4, 5));
int min = [Link](list);
[Link]("Min: " + min); // Output: Min: 1
List<Integer> list = new ArrayList<>([Link](1, 2, 3, 4, 5));
int max = [Link](list);
[Link]("Max: " + max); // Output: Max: 5
List<String> list = new ArrayList<>([Link]("Apple", "Banana", "Apple", "Cherry", "Apple"));
int frequency = [Link](list, "Apple");
[Link]("Frequency of 'Apple': " + frequency); // Output: Frequency of 'Apple': 3
List<Integer> list = new ArrayList<>([Link](1, 2, 3, 4, 5));
int index = [Link](list, 3);
[Link]("Index of 3: " + index); // Output: Index of 3: 2
List<String> list = new ArrayList<>([Link]("Apple", "Banana", "Cherry"));
[Link](list, "Date");
[Link](list); // Output: [Date, Date, Date]
List<String> list1 = new ArrayList<>([Link]("Apple", "Banana", "Cherry"));
List<String> list2 = new ArrayList<>([Link]("Date", "Fig", "Grape"));
boolean isDisjoint = [Link](list1, list2);
[Link]("Is disjoint: " + isDisjoint); // Output: Is disjoint: true
List<String> list = new ArrayList<>();
[Link](list, "Apple", "Banana", "Cherry");
[Link](list); // Output: [Apple, Banana, Cherry]
Collections & Framewords:
1. ArrayList
ArrayList<String> list = new ArrayList<>();
// Convert the HashSet to an ArrayList
HashSet<String> set = new HashSet<>();
ArrayList<String> list = new ArrayList<>(set);
Similarly we can convert form other data structure to arrayList;
Add: [Link]("Apple");
Accessing elements (using index): String fruit = [Link](0);
Or using for each : for(String ele: list)
{sout(ele)}
Using Iterator :
Iterator<Integer> it = [Link]();
while([Link]())
{
[Link]([Link]());
}
Updating an element: [Link](1, "Orange"); // Replaces "Banana" with
"Orange"
Removing an element: [Link](2); // Removes the element at index 2
(Mango)
Remove element using value: [Link]([Link](“Orange”))
Contains of Not: [Link](“Apple”);
Getting size: int size = [Link](); // Returns the number of elements in
the list
clear(): Removes all elements from the list.
isEmpty(): Checks if the list is empty.
2. Stack
push(element): Adds an element to the top of the stack.
pop(): Removes and returns the top element of the stack. Throws an EmptyStackException if the stack
is empty.
peek(): Returns the top element without removing it. Also throws an EmptyStackException if empty.
isEmpty(): Checks if the stack is empty, returning true if so, and false otherwise.
search(object): Searches for an element and returns its 1-based position from the top. Returns -1 if the
element isn’t found.
3. Queue
Queue<Integer> queue = new LinkedList<>();
add(element): Inserts an element at the end of the queue. Throws an exception if the queue
is full.
offer(element): Similar to add, but returns false if the queue is full rather than throwing an
exception.
remove(): Removes and returns the front element. Throws a NoSuchElementException if the
queue is empty.
poll(): Removes and returns the front element, or returns null if the queue is empty.
peek(): Retrieves, but does not remove, the front element. Returns null if the queue is empty.
element(): Similar to peek(), but throws a NoSuchElementException if the queue is empty.
4. PriorityQueue
Min-heap: PriorityQueue<Integer> pq = new PriorityQueue<>();
Max-heap: PriorityQueue<Integer> pq = new PriorityQueue<>([Link]());
add(element) / offer(element): Inserts an element into the queue in the correct order.
peek(): Retrieves, but does not remove, the head of the queue (smallest or highest-priority element).
poll(): Retrieves and removes the head of the queue. Returns null if the queue is empty.
remove(): Removes a specific element if present.
5. ArrayDeque
1. Adding Elements
addFirst(E e): Adds the specified element at the front of the deque.
addLast(E e): Adds the specified element at the end of the deque.
offerFirst(E e): Inserts the element at the front; returns true if successful, false if not.
offerLast(E e): Inserts the element at the end; returns true if successful, false if not.
push(E e): Adds the element at the front of the deque (for stack functionality).
offer(E e): Adds the element to the end (same as offerLast).
2. Removing Elements
removeFirst(): Removes and returns the first element. Throws an exception if the deque is
empty.
removeLast(): Removes and returns the last element. Throws an exception if the deque is empty.
pollFirst(): Removes and returns the first element, or returns null if the deque is empty.
pollLast(): Removes and returns the last element, or returns null if the deque is empty.
pop(): Removes and returns the first element (for stack functionality). Throws an exception if the
deque is empty.
poll(): Removes and returns the first element (same as pollFirst).
3. Accessing Elements
getFirst(): Retrieves, but does not remove, the first element. Throws an exception if the deque is
empty.
getLast(): Retrieves, but does not remove, the last element. Throws an exception if the deque is
empty.
peekFirst(): Retrieves, but does not remove, the first element, or returns null if the deque is
empty.
peekLast(): Retrieves, but does not remove, the last element, or returns null if the deque is
empty.
peek(): Retrieves, but does not remove, the first element (same as peekFirst).
4. Checking Elements
contains(Object o): Checks if the deque contains the specified element, returning true if it does,
false otherwise.
isEmpty(): Checks if the deque is empty, returning true if it has no elements, false otherwise.
6. Set
Set<String> hashSet = new HashSet<>();
Set<String> treeSet = new TreeSet<>();
Set<String> linkedHashSet = new LinkedHashSet<>();
Types of Sets in Java
1. HashSet:
o Does not maintain any order of elements.
o Allows null elements.
o Fastest for basic operations (add, remove, contains).
o Backed by a hash table, so the order is unpredictable.
2. LinkedHashSet:
o Maintains insertion order.
o Allows null elements.
o Slightly slower than HashSet due to the ordering overhead.
o Useful when you need a set that keeps elements in the order they were added.
3. TreeSet:
o Maintains elements in sorted (ascending) order.
o Does not allow null elements.
o Implements NavigableSet and is backed by a Red-Black tree.
o Useful when you need elements to be sorted automatically
HashSet<String> set = new HashSet<>();
Add elements: [Link]("Apple");
Check if an element exists: [Link](“Apple”);
Remove: [Link]("Cherry");
size(): Returns the number of elements in the set.
isEmpty(): Returns true if the set is empty.
clear(): Removes all elements from the set.
Traversing hashSet
for (String fruit : set) { [Link](fruit); }
or
Iterator<String> iterator = [Link]();
while ([Link]()) {
String fruit = [Link]();
[Link](fruit);
7. Map
Map<String, Integer> map = new HashMap<>();
// Adding key-value pairs to the HashMap [Link]("Apple", 10);
// Accessing values based on keys [Link]("Value for key 'Apple': " +
[Link]("Apple"));
// Checking if a key exists if ([Link]("Banana")) { [Link]("The
map contains key 'Banana'."); }
// Iterating over the map for ([Link]<String, Integer> entry : [Link]())
{ [Link]("Key: " + [Link]() + ", Value: " + [Link]()); }
// Removing a key-value pair [Link]("Orange");
// Size of the map [Link]("Size of the map: " + [Link]());
Other Implementations of Map
LinkedHashMap: Maintains insertion order.
TreeMap: Maintains a sorted order of the keys
int value = [Link]("Apple");
[Link]("Value for Apple: " + value); // Output: Value for Apple: 10
boolean hasValue10 = [Link](10);
[Link]("Contains value 10: " + hasValue10); // Output: Contains value 10:
true
[Link]("Banana", 20);
Set<String> keys = [Link]();
[Link]("Keys: " + keys); // Output: Keys: [Apple, Banana]
Collection<Integer> values = [Link]();
[Link]("Values: " + values); // Output: Values: [10, 20]
[Link]("Apple", 15);
[Link]([Link]("Apple")); // Output: 15
[Link]("Banana", 20, 25);
[Link]([Link]("Banana")); // Output: 25
[Link] is a method in Java’s Map interface that allows you to retrieve the
value associated with a specific key, or return a default value if the key doesn’t exist in
the map. This can be useful to avoid NullPointerException or needing to check for the
key’s existence before accessing it.
Map<String, Integer> map = new HashMap<>(); [Link]("apple", 3);
[Link]("banana", 2); // Existing key int appleCount = [Link]("apple",
0); // Returns 3 [Link]("Apple count: " + appleCount); // Non-existing key
int orangeCount = [Link]("orange", 0);