1. List Interface (e.g., ArrayList, LinkedList) // Commonly Used
add(E e); // Adds element to the list
add(int index, E element); // Adds element at a specific index
get(int index); // Returns element at the specified index
set(int index, E element); // Replaces element at the specified index
remove(int index); // Removes element at a specific index
remove(Object o); // Removes the first occurrence of the specified element
contains(Object o); // Checks if the list contains the specified element
indexOf(Object o); // Returns the index of the first occurrence of the specified element
lastIndexOf(Object o); // Returns the index of the last occurrence of the specified element
size(); // Returns the number of elements in the list
isEmpty(); // Checks if the list is empty
subList(int fromIndex, int toIndex); // Returns a view of the portion of this list
toArray(); // Converts the list to an array
sort(Comparator<? super T> c); // Sorts the list
Arrays Class (for Array Manipulation)
Arrays.sort(T[] a); // Sorts the array
Arrays.binarySearch(T[] a, T key); // Searches for a key in the array
Arrays.copyOf(T[] original, int newLength); // Copies array to a new array with new length
Arrays.fill(T[] a, T val); // Fills the array with the specified value
Arrays.equals(T[] a, T[] b); // Compares two arrays
Arrays.toString(T[] a); // Returns a string representation of the array
. Utility Methods (Collections Class)
Collections.sort(List<T> list); // Sorts the list in ascending order
Collections.reverse(List<T> list); // Reverses the order of elements
Collections.shuffle(List<?> list); // Randomly shuffles the list
Collections.max(Collection<T> coll); // Returns the maximum element
Collections.min(Collection<T> coll); // Returns the minimum element
Collections.frequency(Collection<?> coll, Object o); // Counts occurrences of an element
Collections.binarySearch(List<? extends Comparable<? super T>> list, T key); // Searches using binary search
Collections.swap(List<?> list, int i, int j); // Swaps elements at two positions
- 🔴⬇️ Convert Array / List → Set /Map
🔹 1. Array Basics
java
CopyEdit
// Declaration
int[] arr = new int[5]; // Default values = 0
int[] arr2 = {1, 2, 3, 4, 5}; // Initialized
// Access elements
int first = arr2[0];
// Length
int n = arr2.length;
🔹 2. Array Input & Output
java
CopyEdit
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
for (int num : arr) System.out.print(num + " ");
🔹 3. Sorting Arrays
java
CopyEdit
Arrays.sort(arr); // Ascending
// Descending
Integer[] arr = {4, 1, 3};
Arrays.sort(arr, Collections.reverseOrder());
🔹 4. Binary Search (Sorted Only)
java
CopyEdit
int idx = Arrays.binarySearch(arr, key);
🔹 5. Find Max / Min in Array
java
CopyEdit
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();