Maven is a standardized project management & build automation tool that uses lifecycles, phases, and plugins to compile, test, package, and deploy applications.
Tuesday, September 2, 2025
Saturday, August 23, 2025
Java Lists Made Simple: Learn with Mindmaps & Visual Flow
The Java Collection framework gives us two major ways to organize elements: List (ordered, allows duplicates) and Set (unique, no duplicates).
List
-
Nature: Ordered sequence, index-based
-
Duplicates: Allowed
-
Analogy: To-do list, where you may repeat tasks
-
Common Usage: When insertion order and position access matter
Set
-
Nature: Unordered, no guaranteed indexing
-
Duplicates: Not allowed
-
Analogy: A bag of unique items, like distinct voter IDs
-
Common Usage: When uniqueness is more important than order
Methods in List and Set
Working with Lists in Java
When to use Lists or Arrays
When to use Sets and List
Saturday, August 16, 2025
Friday, July 11, 2025
Leetcode 2942. Find Words Containing Character
Problem Statement:
2942. Find Words Containing Character
You are given a 0-indexed array of strings words and a character x.
Return an array of indices representing the words that contain the character x.
Note that the returned array may be in any order.
Initial Code:
class Solution {
public List<Integer> findWordsContaining(String[] words, char x) {
List<Integer> findWordsContaining = new ArrayList();
for (int i = 0; i < words.length; i++) {
if (words[i].contains(x)) {
findWordsContaining.add(i);
}
}
return findWordsContaining;
}
}
Issue in the code -
new ArrayList() | - Should declare the type parameter: new ArrayList<>() (or new ArrayList<Integer>() ) |
words[i].contains(x) | - String.contains() expects a CharSequence , but passed char . Need to convert the char to a String : |
class Solution { public List<Integer> findWordsContaining(String[] words, char x) { List<Integer> findWordsContaining= new ArrayList<>(); for (int i=0;i<words.length; i++){ if(words[i].indexOf(x)!=-1){ findWordsContaining.add(i); } } return findWordsContaining; } }
Leetcode 2769. Find the Maximum Achievable Number
Problem Statement:
2769. Find the Maximum Achievable Number
Given two integers, num and t. A number x is achievable if it can become equal to num after applying the following operation at most t times:Increase or decrease x by 1, and simultaneously increase or decrease num by 1.
Return the maximum possible value of x.
Initial Code:
class Solution {
public List<Integer> findWordsContaining(String[] words, char x) {
List<Integer> findWordsContaining = new ArrayList();
for (int i = 0; i < words.length; i++) {
if (words[i].contains(x)) {
findWordsContaining.add(i);
}
}
return findWordsContaining;
}
}
Issue in the code -
new ArrayList() | - Should declare the type parameter: new ArrayList<>() (or new ArrayList<Integer>() ) |
words[i].contains(x) | - String.contains() expects a CharSequence , but passed char . Need to convert the char to a String : |
Leetcode 1920 – Build Array from Permutation
Problem Statement:
1920. Build Array from Permutation
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.
A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).
Initial Code
class Solution {
public int[] buildArray(int[] nums) {
int[nums.length] numsnew;
for (int i=0;i<nums.length;i++){
numsnew[i]=nums[nums[i]];
}
return numsnew;
}
}
Issues in the code
Line | Issue |
---|---|
int[nums.length] numsnew; | - Invalid Java syntax for array declaration. Should be: int[] numsnew = new int[nums.length]; |
Corrected code
class Solution {
public int[] buildArray(int[] nums) {
int[] numsnew = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
numsnew[i] = nums[nums[i]];
}
return numsnew;
}
}
Leetcode 2894. Divisible and Non-divisible Sums Difference
Problem Statement:
leetcode problem 2894. Divisible and Non-divisible Sums Difference -
Define two integers as follows:num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m.
num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m.
Return the integer num1 - num2.
Initial Code
class Solution { public int differenceOfSums(int n, int m) { int suma = 0;sumb=0; // ERROR: sumb not declared properly for (int i=1; i==n; i++){ // ERROR: i==n (this never iterates) if(i%m==0){ sumb+=i; }else{ suma+=i; } } return suma-sumb; } }
Issues in the code:
- Variable declaration:
sumb
is undeclared. - Loop condition:
for (int i = 1; i == n; i++)
executes only wheni
equalsn
, so it never loops. It must bei <= n
. - Minor style: combining declarations on one line makes it harder to read
Corrected code
class Solution { public int differenceOfSums(int n, int m) { int suma = 0; int sumb = 0; for (int i = 1; i <= n; i++) { if (i % m == 0) sumb += i; else suma += i; } return suma - sumb; } }
Leetcode 3110 - Score of a string problem analysis
Hi Everyone, This was the first attempt answer for the Leetcode problem on score of a string.
https://leetcode.com/problems/score-of-a-string
class Solution { public int scoreOfString(String s) { sum = 0; char[] ssize = s.toCharArray(); for (int i=0;i<ssize.length-1; i++){ sum = sum + abs(ssize(i).asciicode-ssize(i+1).asciicode) } return sum }
Issue | Description |
---|---|
sum not declared - |
sum is used without type declaration. |
Incorrect syntax: abs() - |
Java requires Math.abs() for absolute value. |
Incorrect character access: ssize(i) - |
Use ssize[i] , not parentheses. |
Non-existent property: .asciicode |
Characters in Java already act as ASCII values when cast to int . |
Missing semicolons and braces | Several semicolons and } missing. |
Overall formatting and structure | Needs improvement for clarity and compilation. |
class Solution { public int scoreOfString(String s) { int sum = 0; char[] ssize = s.toCharArray(); for (int i = 0; i < ssize.length - 1; i++) { sum += Math.abs(ssize[i] - ssize[i + 1]); } return sum; } }
Sunday, March 2, 2025
Collections - Working With Stream
What is a Java Stream?
A Stream is a sequence of elements supporting functional-style operations like filtering, mapping, and reducing.
Streams do not modify the original data structure.
They operate lazily, meaning operations are executed only when a terminal operation is applied.
Can be sequential or parallel
Stream Pipeline Structure
A Java Stream Pipeline consists of:
Source - Where the stream comes from (e.g., a List, Set, or Array)
Intermediate Operations - Process and transform elements
Terminal Operation - Produces the final result, closing the stream.
Example of Stream PipelineList<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); List<Integer> result = numbers.stream() // Step 1: Create Stream .filter(n -> n % 2 == 0) // Step 2: Intermediate Operation .map(n -> n * n) // Step 3: Intermediate Operation .collect(Collectors.toList()); // Step 4: Terminal Operation System.out.println(result);
Flow Diagram to Understand Filters
Stream Sources
data structure from which the stream originates. This could be a collection, array, file, or range.List<String> list = Arrays.asList("Apple", "Banana", "Cherry"); Stream<String> stream1 = list.stream(); ---------------------------------------------------------------------- String[] array = {"Red", "Green", "Blue"}; Stream<String> stream2 = Arrays.stream(array); ---------------------------------------------------------------------- Stream<String> stream3 = Stream.of("One", "Two", "Three"); ----------------------------------------------------------------------
Intermediate Operation -
Intermediate operations transform a stream without consuming it.
They return another Stream, allowing further operations to be chained. Stream<Integer> filtered = numbers.stream().filter(n -> n > 5); ----------------------------------------------------------------------- Stream<Integer> squared = numbers.stream().map(n -> n * n); ----------------------------------------------------------------------- Stream<String> sorted = list.stream().sorted(); ----------------------------------------------------------------------- Stream<String> sortedDesc = list.stream().sorted(Comparator.reverseOrder()); ----------------------------------------------------------------------- Stream<Integer> uniqueNumbers = numbers.stream().distinct(); ----------------------------------------------------------------------- Stream<String> limited = list.stream().limit(2); ----------------------------------------------------------------------- Stream<String> skipped = list.stream().skip(2); ----------------------------------------------------------------------- List<List<Integer>> listOfLists = Arrays.asList( Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6) ); List<Integer> flattened = listOfLists.stream() .flatMap(List::stream) .collect(Collectors.toList()); // Output: [1, 2, 3, 4, 5, 6] ------------------------------------------------------------------------
Terminal Operation -
Terminal expressions (or terminal operations) consume the stream and produce a result or side effect. Unlike intermediate operations (which return a stream for further processing), terminal operations close the stream, meaning it cannot be used again.
✅ Consumes the stream – Once a terminal operation is applied, the stream cannot be reused.
✅ Returns a result – Can return a single value, a collection, or perform an action like printing.
✅ Forces execution – Since streams are lazy, terminal operations trigger actual processing.List<String> collected = list.stream().collect(Collectors.toList()); ---------------------------------------------------------------------------------- Set<String> collectedSet = list.stream().collect(Collectors.toSet()); ---------------------------------------------------------------------------------- Map<String, Integer> collectedMap = list.stream().collect(Collectors.toMap(s -> s, String::length)); ---------------------------------------------------------------------------------- list.stream().forEach(System.out::println); ---------------------------------------------------------------------------------- long count = list.stream().filter(s -> s.startsWith("A")).count(); ---------------------------------------------------------------------------------- Optional<String> first = list.stream().findFirst(); Optional<String> any = list.stream().findAny(); ---------------------------------------------------------------------------------- boolean anyMatch = list.stream().anyMatch(s -> s.contains("e")); boolean allMatch = list.stream().allMatch(s -> s.length() > 3); boolean noneMatch = list.stream().noneMatch(s -> s.isEmpty()); --------------------------------------------------------------------------------- Optional<Integer> sum = numbers.stream().reduce((a, b) -> a + b); ---------------------------------------------------------------------------------- int sum = numbers.stream().reduce(0, Integer::sum); ---------------------------------------------------------------------------------- Optional<Integer> min = numbers.stream().min(Integer::compare); Optional<Integer> max = numbers.stream().max(Integer::compare); ----------------------------------------------------------------------------------
Common Implementation
filter() for condition-based filtering.
map() to transform elements.
reduce() for aggregation (sum, max, min).
groupingBy() to classify data.
partitioningBy() for true/false categorization.
sorted() to order elements.
flatMap() to merge multiple collections.
List<String> uppercased = list.stream() .map(String::toUpperCase) .collect(Collectors.toList()); ---------------------------------------------------------------- List<Integer> evens = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList()); ----------------------------------------------------------------- // Use groupingBy() when you need multiple groups List<Employee> employees = Arrays.asList( new Employee("Alice", "HR"), new Employee("Bob", "IT"), new Employee("Charlie", "HR"), new Employee("David", "IT"), new Employee("Eve", "Finance") ); // Group employees by department Map<String, List<Employee>> groupedByDepartment = employees.stream() .collect(Collectors.groupingBy(emp -> emp.department)); System.out.println(groupedByDepartment); ------------------------------------------------------------------ // Use of partitioning to group based on condition List<Integer> numbers = Arrays.asList(10, 15, 20, 25, 30); // Partition numbers into even and odd Map<Boolean, List<Integer>> partitionedNumbers = numbers.stream() .collect(Collectors.partitioningBy(num -> num % 2 == 0)); System.out.println(partitionedNumbers); -------------------------------------------------------------------- List<Integer> numbers = Arrays.asList(10, 15, 20, 25, 30); int sum = numbers.stream() .filter(n -> n % 2 == 0) .reduce(0, Integer::sum); ---------------------------------------------------------------------- List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); String result = names.stream() .collect(Collectors.joining(", ")); ----------------------------------------------------------------------------- //Use of flatmap to create a list from list of list List<List<String>> nestedList = Arrays.asList( Arrays.asList("a", "b"), Arrays.asList("c", "d"), Arrays.asList("e", "f") ); List<String> flatList = nestedList.stream() .flatMap(List::stream) .collect(Collectors.toList());Benefits of Using Streams:
Improves Readability – Eliminates verbose loops and enhances code clarity.
Encourages Functional Programming – Uses lambda expressions for concise processing logic.
Optimized Performance – Lazy evaluation and parallel processing reduce execution time for large datasets.
Official Documentation & References
Oracle Java Streams Documentation 🔗 https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
Java Tutorials by Oracle 🔗 https://docs.oracle.com/javase/tutorial/collections/streams/index.html
Baeldung - Guide to Java Streams 🔗 https://www.baeldung.com/java-8-streams
GeeksforGeeks - Java Streams Guide 🔗 https://www.geeksforgeeks.org/stream-in-java/
Mindmap to Understand Selenium 4 architecture
Selenium is one of the most popular open-source automation testing tools, widely used for web application testing. It is important to know its architecture — the flow of communication between test script, Selenium components, and the browser.
Components of Selenium Architecture
Selenium Client Libraries (Language Bindings)
These are language-specific libraries provided by Selenium. Test scripts use these libraries to interact with the browser using high-level commands. The client libraries translate these high-level commands into W3C WebDriver Protocol requests.
Browser Drivers
Each browser requires a dedicated driver to interpret Selenium commands. Browser drivers act as a bridge between your Selenium script and the actual browser. They receive HTTP commands (W3C Protocol) from Selenium, convert them into native browser actions, and send back responses to Selenium.
Browsers
Real browsers (Chrome, etc.) that open the web page and execute actual user interactions. The browser responds to commands (click, type, navigate) and sends the results back through the browser driver.
W3C WebDriver Protocol in Selenium 4
- Starting from Selenium 4, Selenium uses the W3C WebDriver Protocol by default.
- This is a standard communication protocol defined by the World Wide Web Consortium (W3C).
It ensures:
- Consistent behavior across all browsers.
- Direct communication between Selenium and modern browsers.
- No dependency on legacy JSON Wire Protocol.
Selenium Libraries:
Selenium Libraries are the backbone of Selenium automation, providing a simple and language-friendly API to control browsers via the W3C WebDriver Protocol.
Useful Documentation
https://www.selenium.dev/documentation/
https://w3c.github.io/webdriver/
https://www.selenium.dev/blog/
https://github.com/SeleniumHQ/selenium