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









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:


Updated 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].indexOf(x)!=-1){
                findWordsContaining.add(i);
            }
        }
        return findWordsContaining;
    }
}