<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohammed mhanna</title>
    <description>The latest articles on Forem by Mohammed mhanna (@mohamad_mhana).</description>
    <link>https://forem.com/mohamad_mhana</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F640783%2F54cd21ab-9b13-4624-8e71-c824d8582dd2.png</url>
      <title>Forem: Mohammed mhanna</title>
      <link>https://forem.com/mohamad_mhana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mohamad_mhana"/>
    <language>en</language>
    <item>
      <title>🌳 Mastering Data Structures in Java — Part 8: TreeMap</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Fri, 31 Oct 2025 16:05:16 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-8-treemap-1mb</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-8-treemap-1mb</guid>
      <description>&lt;p&gt;If HashMap is all about speed, then TreeMap is all about order.&lt;br&gt;
It’s a powerful data structure when you need your data to stay sorted automatically — by keys.&lt;/p&gt;

&lt;p&gt;Let’s explore what makes TreeMap unique and when to use it 👇&lt;/p&gt;



&lt;p&gt;🧠 What Is a TreeMap?&lt;/p&gt;

&lt;p&gt;A TreeMap in Java is a sorted map implementation based on a Red-Black Tree (a type of self-balancing binary search tree).&lt;/p&gt;

&lt;p&gt;It stores key-value pairs just like a HashMap,&lt;br&gt;
👉 but it keeps the keys in sorted order — either by natural ordering or by a custom comparator you define.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap&amp;lt;Integer, String&amp;gt; users = new TreeMap&amp;lt;&amp;gt;();

        users.put(3, "Ali");
        users.put(1, "Sara");
        users.put(2, "Omar");

        System.out.println(users);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{1=Sara, 2=Omar, 3=Ali}&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Notice how it sorted the keys automatically 🔥&lt;/p&gt;



&lt;p&gt;⚙️ How It Works Internally&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TreeMap&lt;/code&gt; uses a Red-Black Tree to store entries.&lt;/p&gt;

&lt;p&gt;When you insert a key, it finds its correct position in the tree (based on comparison).&lt;/p&gt;

&lt;p&gt;The tree stays balanced to keep operations efficient.&lt;/p&gt;

&lt;p&gt;This allows &lt;code&gt;O(log n)&lt;/code&gt; time for &lt;code&gt;put()&lt;/code&gt;, &lt;code&gt;get()&lt;/code&gt;, and &lt;code&gt;remove()&lt;/code&gt; operations.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;code&gt;HashMap&lt;/code&gt; uses hashing and provides average &lt;code&gt;O(1)&lt;/code&gt; time, but no order.&lt;/p&gt;



&lt;p&gt;🔍 Example: Natural Ordering (Strings)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TreeMap&amp;lt;String, Integer&amp;gt; grades = new TreeMap&amp;lt;&amp;gt;();

grades.put("Omar", 85);
grades.put("Sara", 92);
grades.put("Ali", 78);

System.out.println(grades);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{Ali=78, Omar=85, Sara=92}&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Keys are sorted alphabetically.&lt;/p&gt;



&lt;p&gt;🧩 Example: Custom Ordering&lt;/p&gt;

&lt;p&gt;You can control the sorting logic using a Comparator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.*;

public class CustomOrderTreeMap {
    public static void main(String[] args) {
        TreeMap&amp;lt;String, Integer&amp;gt; grades = new TreeMap&amp;lt;&amp;gt;(Comparator.reverseOrder());

        grades.put("Omar", 85);
        grades.put("Sara", 92);
        grades.put("Ali", 78);

        System.out.println(grades);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{Sara=92, Omar=85, Ali=78}&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now it’s in descending order.&lt;/p&gt;



&lt;p&gt;⚡ Performance Comparison&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;HashMap&lt;/th&gt;
&lt;th&gt;TreeMap&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Insertion&lt;/td&gt;
&lt;td&gt;O(1) avg&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Search&lt;/td&gt;
&lt;td&gt;O(1) avg&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deletion&lt;/td&gt;
&lt;td&gt;O(1) avg&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ordering&lt;/td&gt;
&lt;td&gt;❌ Unordered&lt;/td&gt;
&lt;td&gt;✅ Sorted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Null Keys&lt;/td&gt;
&lt;td&gt;✅ Allowed&lt;/td&gt;
&lt;td&gt;❌ Not Allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So if you don’t need ordering, go with &lt;code&gt;HashMap&lt;/code&gt;.&lt;br&gt;
If order matters, &lt;code&gt;TreeMap&lt;/code&gt; is the right choice.&lt;/p&gt;



&lt;p&gt;🌍 Where TreeMap Is Used in Real-World Projects&lt;/p&gt;

&lt;p&gt;Leaderboards / Rankings&lt;/p&gt;

&lt;p&gt;Sort players by score or performance dynamically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TreeMap&amp;lt;Integer, String&amp;gt; leaderboard = new TreeMap&amp;lt;&amp;gt;(Comparator.reverseOrder());
leaderboard.put(95, "Sara");
leaderboard.put(87, "Ali");
leaderboard.put(92, "Omar");
System.out.println(leaderboard);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigable Structures (like Calendar Apps)&lt;/p&gt;

&lt;p&gt;Quickly find the next event or previous event using methods like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;higherKey()&lt;/code&gt;, &lt;code&gt;lowerKey()&lt;/code&gt;, &lt;code&gt;ceilingKey()&lt;/code&gt;, &lt;code&gt;floorKey()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Range Queries&lt;/p&gt;

&lt;p&gt;Get sub-maps of data between two keys:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TreeMap&amp;lt;Integer, String&amp;gt; map = new TreeMap&amp;lt;&amp;gt;();
map.put(1, "A"); map.put(3, "B"); map.put(5, "C"); map.put(7, "D");

System.out.println(map.subMap(3, 7)); // {3=B, 5=C}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configuration Management&lt;/p&gt;

&lt;p&gt;When property keys must be stored in order (for readability or file export).&lt;/p&gt;




&lt;p&gt;🧰 Common Methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstKey()
lastKey()
higherKey(K key)
lowerKey(K key)
subMap(K fromKey, K toKey)
headMap(K toKey)
tailMap(K fromKey)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TreeMap&amp;lt;Integer, String&amp;gt; map = new TreeMap&amp;lt;&amp;gt;();
map.put(1, "A");
map.put(2, "B");
map.put(3, "C");

System.out.println(map.firstKey()); // 1
System.out.println(map.lastKey());  // 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;💡 Pro Tips&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;TreeMap&lt;/code&gt; when you care about sorted keys.&lt;/p&gt;

&lt;p&gt;For custom sorting, always pass a Comparator in the constructor.&lt;/p&gt;

&lt;p&gt;If performance is your top priority, prefer HashMap.&lt;/p&gt;

&lt;p&gt;Avoid null keys — they’ll throw &lt;code&gt;NullPointerException&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;🧩 Summary&lt;/p&gt;

&lt;p&gt;TreeMap brings order to your mappings — it’s ideal when you need your data to stay sorted and searchable by key.&lt;br&gt;
While it’s slower than HashMap, its power lies in navigation and range operations.&lt;/p&gt;

&lt;p&gt;Think of it like a map that sorts itself — perfect for leaderboards, calendars, or any data where order matters.&lt;/p&gt;




</description>
      <category>programming</category>
      <category>architecture</category>
      <category>performance</category>
      <category>java</category>
    </item>
    <item>
      <title>🔥 Mastering Data Structures in Java - Part 7: HashMap</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Thu, 30 Oct 2025 18:40:28 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-7-hashmap-2pdm</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-7-hashmap-2pdm</guid>
      <description>&lt;p&gt;If you’ve ever needed to store key–value pairs (like usernames and passwords, or IDs and names) — you’ve already been thinking about a HashMap.&lt;br&gt;
It’s one of the most powerful and widely used data structures in Java.&lt;/p&gt;

&lt;p&gt;Let’s break it down completely 👇&lt;/p&gt;



&lt;p&gt;🧠 What Is a HashMap?&lt;/p&gt;

&lt;p&gt;A HashMap in Java is a part of the Collections Framework that stores data as key-value pairs.&lt;br&gt;
It allows fast access, insertion, and deletion based on keys.&lt;/p&gt;

&lt;p&gt;✅ Keys are unique&lt;br&gt;
✅ One null key is allowed&lt;br&gt;
✅ Multiple null values are allowed&lt;br&gt;
✅ Not thread-safe by default&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.HashMap;

public class HashMapDemo {
    public static void main(String[] args) {
        HashMap&amp;lt;Integer, String&amp;gt; users = new HashMap&amp;lt;&amp;gt;();

        users.put(101, "Mohammed");
        users.put(102, "Ali");
        users.put(103, "Omar");

        System.out.println(users.get(102)); // Output: Ali
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;⚙️ How HashMap Works Internally&lt;/p&gt;

&lt;p&gt;At its core, a HashMap uses an array of buckets.&lt;br&gt;
Each bucket holds a linked list (or a tree since Java 8) of entries that share the same hash.&lt;/p&gt;

&lt;p&gt;When you put(key, value):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java calculates the hash code of the key.&lt;/li&gt;
&lt;li&gt;It finds the correct bucket index in the array.&lt;/li&gt;
&lt;li&gt;It stores the key–value pair there.&lt;/li&gt;
&lt;li&gt;If another entry has the same hash, it’s linked in the same bucket (collision).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you get(key):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java finds the hash bucket for the key.&lt;/li&gt;
&lt;li&gt;Then checks for equality using equals() to find the correct value.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HashMap&amp;lt;String, Integer&amp;gt; scores = new HashMap&amp;lt;&amp;gt;();
scores.put("Alice", 90);
scores.put("Bob", 85);

System.out.println(scores.get("Alice")); // 90

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;💡 Tip: Always override both &lt;code&gt;equals()&lt;/code&gt; and &lt;code&gt;hashCode()&lt;/code&gt; when using custom objects as keys!&lt;/p&gt;



&lt;p&gt;🧩 Example: Custom Key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Employee {
    int id;
    String name;

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Employee)) return false;
        Employee e = (Employee) obj;
        return this.id == e.id;
    }
}

public class Main {
    public static void main(String[] args) {
        HashMap&amp;lt;Employee, Double&amp;gt; salaries = new HashMap&amp;lt;&amp;gt;();
        salaries.put(new Employee(1, "Ali"), 3000.0);
        salaries.put(new Employee(2, "Sara"), 3500.0);

        System.out.println(salaries.get(new Employee(1, "Ali"))); // 3000.0
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Even though we created a new &lt;code&gt;Employee&lt;/code&gt; object, it works because of correctly overridden &lt;code&gt;equals()&lt;/code&gt; and &lt;code&gt;hashCode()&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;⚡ Performance&lt;/p&gt;

&lt;p&gt;Average complexity for &lt;code&gt;put()&lt;/code&gt;, &lt;code&gt;get()&lt;/code&gt;, &lt;code&gt;remove()&lt;/code&gt; → &lt;code&gt;O(1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Worst case (lots of hash collisions) → O(n)&lt;/p&gt;

&lt;p&gt;Java 8 improved this by converting long linked lists into balanced trees when collisions become too large.&lt;/p&gt;




&lt;p&gt;🧱 When to Use HashMap&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;th&gt;HashMap?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Need to map keys to values&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Need fast lookup by key&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Need order preserved&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Need thread-safety&lt;/td&gt;
&lt;td&gt;❌ No (Use &lt;code&gt;ConcurrentHashMap&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Need sorting by key&lt;/td&gt;
&lt;td&gt;❌ No (Use &lt;code&gt;TreeMap&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🌍 Where HashMap Is Used in Real-World Projects&lt;/p&gt;

&lt;p&gt;Caching Systems&lt;br&gt;
Store recently used data for quick access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HashMap&amp;lt;String, String&amp;gt; cache = new HashMap&amp;lt;&amp;gt;();
cache.put("user:101", "Ali");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Database Result Mapping&lt;br&gt;
When reading from a database, mapping IDs to entities.&lt;/p&gt;

&lt;p&gt;Configurations &amp;amp; Settings&lt;br&gt;
Store app settings as key-value pairs.&lt;/p&gt;

&lt;p&gt;Counting Frequency of Words or Items&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HashMap&amp;lt;String, Integer&amp;gt; wordCount = new HashMap&amp;lt;&amp;gt;();
for (String word : sentence.split(" ")) {
    wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Session Management in Web Apps&lt;br&gt;
Track active user sessions using userId as a key.&lt;/p&gt;



&lt;p&gt;🧰 Common Methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;put(K key, V value)
get(Object key)
remove(Object key)
containsKey(Object key)
containsValue(Object value)
keySet()
values()
entrySet()
clear()
size()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (Map.Entry&amp;lt;Integer, String&amp;gt; entry : users.entrySet()) {
    System.out.println(entry.getKey() + " -&amp;gt; " + entry.getValue());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🧩 Pro Tips&lt;/p&gt;

&lt;p&gt;Use LinkedHashMap if you need predictable iteration order.&lt;/p&gt;

&lt;p&gt;Use ConcurrentHashMap for thread-safe operations.&lt;/p&gt;

&lt;p&gt;Avoid mutable keys — changing them breaks the map’s structure.&lt;/p&gt;

&lt;p&gt;Load Factor (default 0.75) controls when to resize the hash table.&lt;/p&gt;




&lt;p&gt;💡 Summary&lt;/p&gt;

&lt;p&gt;HashMap is the backbone of many Java systems — caches, databases, configurations, and beyond.&lt;br&gt;
It’s fast, flexible, and incredibly powerful — but only when you understand how hashing really works.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>coding</category>
      <category>beginners</category>
    </item>
    <item>
      <title>🧠 Mastering Data Structures in Java — Part 6: HashSet</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Wed, 29 Oct 2025 13:12:42 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-6-hashset-4c0c</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-6-hashset-4c0c</guid>
      <description>&lt;p&gt;🔍 What Is a HashSet?&lt;/p&gt;

&lt;p&gt;A HashSet is a collection in Java that stores unique elements — no duplicates allowed.&lt;br&gt;
It’s part of the java.util package and is backed by a HashMap internally.&lt;/p&gt;

&lt;p&gt;Think of it like a bag of unique cards 🎴 — if you try to add the same card again, it just ignores it.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;⚙️ Quick Example

import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        HashSet&amp;lt;String&amp;gt; countries = new HashSet&amp;lt;&amp;gt;();

        countries.add("Japan");
        countries.add("Canada");
        countries.add("Japan"); // duplicate ignored

        System.out.println(countries); // [Japan, Canada]
        System.out.println("Contains Canada? " + countries.contains("Canada")); // true
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;✅ Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Japan, Canada]
Contains Canada? true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 Notice how “Japan” appears only once — &lt;code&gt;HashSet&lt;/code&gt; automatically handles duplicates.&lt;/p&gt;




&lt;p&gt;⚙️ How It Works Internally&lt;/p&gt;

&lt;p&gt;Under the hood, a HashSet uses a HashMap where:&lt;/p&gt;

&lt;p&gt;Elements are stored as keys.&lt;/p&gt;

&lt;p&gt;A dummy value (usually a static constant) acts as the value.&lt;/p&gt;

&lt;p&gt;When you call add(element):&lt;/p&gt;

&lt;p&gt;The element’s &lt;code&gt;hashCode()&lt;/code&gt; is computed.&lt;/p&gt;

&lt;p&gt;It’s placed into a bucket based on that hash.&lt;/p&gt;

&lt;p&gt;If another element has the same hash (collision), it’s stored in a linked list or tree within that bucket.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;equals()&lt;/code&gt; method ensures duplicates are not added.&lt;/p&gt;




&lt;p&gt;⚙️ Common Methods&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;add(E e)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds element if not already present&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;remove(Object o)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes element&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;contains(Object o)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Checks if element exists&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;isEmpty()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns true if empty&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;size()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns number of elements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;clear()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes all elements&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🧮 Time Complexity&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Average&lt;/th&gt;
&lt;th&gt;Worst Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;add()&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;remove()&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;contains()&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iteration&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;⚡ HashSet provides constant-time performance for most operations — that’s why it’s widely used for lookups and membership checks.&lt;/p&gt;




&lt;p&gt;💡 Key Features&lt;/p&gt;

&lt;p&gt;🚫 No duplicates — each element is unique.&lt;/p&gt;

&lt;p&gt;⚡ Fast lookup using hashing.&lt;/p&gt;

&lt;p&gt;🔀 No guaranteed order — iteration order may differ from insertion order.&lt;/p&gt;

&lt;p&gt;📦 Allows one null element.&lt;/p&gt;

&lt;p&gt;❌ Not thread-safe — use &lt;code&gt;Collections.synchronizedSet()&lt;/code&gt; for concurrency.&lt;/p&gt;




&lt;p&gt;🔍 Example: Removing Duplicates from a List&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.*;

public class RemoveDuplicates {
    public static void main(String[] args) {
        List&amp;lt;Integer&amp;gt; numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
        HashSet&amp;lt;Integer&amp;gt; unique = new HashSet&amp;lt;&amp;gt;(numbers);

        System.out.println(unique); // [1, 2, 3, 4, 5]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 This is one of the most common and elegant uses of a HashSet.&lt;/p&gt;




&lt;p&gt;🔍 Example: Checking Membership Efficiently&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HashSet&amp;lt;String&amp;gt; users = new HashSet&amp;lt;&amp;gt;();
users.add("mohammed");
users.add("ahmed");

if (users.contains("mohammed")) {
    System.out.println("Welcome back!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This operation is instant (O(1)) — much faster than searching a list.&lt;/p&gt;




&lt;p&gt;🧩 HashSet vs TreeSet vs LinkedHashSet&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;HashSet&lt;/th&gt;
&lt;th&gt;LinkedHashSet&lt;/th&gt;
&lt;th&gt;TreeSet&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Order&lt;/td&gt;
&lt;td&gt;No order&lt;/td&gt;
&lt;td&gt;Maintains insertion order&lt;/td&gt;
&lt;td&gt;Sorted order&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Speed&lt;/td&gt;
&lt;td&gt;Fastest&lt;/td&gt;
&lt;td&gt;Slightly slower&lt;/td&gt;
&lt;td&gt;Slower (due to sorting)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backed By&lt;/td&gt;
&lt;td&gt;HashMap&lt;/td&gt;
&lt;td&gt;LinkedHashMap&lt;/td&gt;
&lt;td&gt;TreeMap&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Allows Null&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use Case&lt;/td&gt;
&lt;td&gt;Unordered unique items&lt;/td&gt;
&lt;td&gt;Preserve insertion order&lt;/td&gt;
&lt;td&gt;Sorted unique items&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🌍 Real-World Uses of HashSet&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;🧾 Removing Duplicates&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cleaning up data — for example, user emails or IDs that appear multiple times.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;🔐 Checking Membership&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Efficient “already exists?” checks (e.g., registered usernames, cached results).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;🧠 Tracking Unique Events&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Game achievements, visited URLs, or user actions that should happen only once.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;🗃 Efficient Lookup Tables&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Fast lookups for values without caring about order — like tags, keywords, or filters.&lt;/p&gt;




&lt;p&gt;⚙️ Example: Unique Visitors in a Web App&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.HashSet;

public class UniqueVisitors {
    public static void main(String[] args) {
        HashSet&amp;lt;String&amp;gt; visitors = new HashSet&amp;lt;&amp;gt;();

        visitors.add("user123");
        visitors.add("guest456");
        visitors.add("user123"); // duplicate

        System.out.println("Unique visitors: " + visitors.size());
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Output:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Unique visitors: 2&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;⚠️ Common Mistakes&lt;/p&gt;

&lt;p&gt;❌ Expecting insertion order — HashSet does not maintain it.&lt;/p&gt;

&lt;p&gt;❌ Forgetting to override hashCode() and equals() in custom objects.&lt;/p&gt;

&lt;p&gt;❌ Using mutable objects as keys — changing them breaks hash consistency.&lt;/p&gt;



&lt;p&gt;🧩 Example: Custom Object in HashSet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.*;

class Student {
    String name;
    int id;

    Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student)) return false;
        Student s = (Student) o;
        return id == s.id;
    }
}

public class CustomHashSet {
    public static void main(String[] args) {
        HashSet&amp;lt;Student&amp;gt; set = new HashSet&amp;lt;&amp;gt;();

        set.add(new Student("Ali", 1));
        set.add(new Student("Ali", 1)); // duplicate ignored

        System.out.println("Total students: " + set.size()); // 1
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without overriding &lt;code&gt;hashCode()&lt;/code&gt; and &lt;code&gt;equals()&lt;/code&gt;, this would incorrectly count as 2 students.&lt;/p&gt;




&lt;p&gt;💡 Final Thought&lt;/p&gt;

&lt;p&gt;The HashSet is your go-to structure when you want uniqueness and speed without worrying about order.&lt;br&gt;
It’s one of the fastest and most memory-efficient collections in Java — a real workhorse behind the scenes of many high-performance systems. ⚡&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>java</category>
    </item>
    <item>
      <title>🧠 Mastering Data Structures in Java — Part 5: Queue</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Tue, 28 Oct 2025 17:42:20 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-4-queue-2o5g</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-4-queue-2o5g</guid>
      <description>&lt;p&gt;🔍 What Is a Queue?&lt;/p&gt;

&lt;p&gt;A Queue is a First-In, First-Out (FIFO) data structure.&lt;br&gt;
That means the first element added is the first one removed — just like waiting in line at a coffee shop ☕.&lt;/p&gt;

&lt;p&gt;Think of it as a pipeline:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enqueue → add to the end&lt;/li&gt;
&lt;li&gt;Dequeue → remove from the front&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Java, the Queue is part of the &lt;code&gt;java.util&lt;/code&gt; package and is represented by the Queue interface.&lt;/p&gt;



&lt;p&gt;⚙️ Basic Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {
    public static void main(String[] args) {
        Queue&amp;lt;String&amp;gt; orders = new LinkedList&amp;lt;&amp;gt;();

        orders.add("Order #1");
        orders.add("Order #2");
        orders.add("Order #3");

        System.out.println("Orders: " + orders); // [Order #1, Order #2, Order #3]

        System.out.println("Serving: " + orders.poll()); // removes Order #1
        System.out.println("Next in line: " + orders.peek()); // Order #2
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧩 Methods:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;add(e)&lt;/code&gt; → Add element to queue&lt;/p&gt;

&lt;p&gt;&lt;code&gt;poll()&lt;/code&gt; → Remove and return front element (returns null if empty)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;peek()&lt;/code&gt; → View front element (without removing)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;remove()&lt;/code&gt; → Remove front element (throws exception if empty)&lt;/p&gt;




&lt;p&gt;⚙️ Queue Implementations in Java&lt;/p&gt;

&lt;p&gt;Java offers several ways to implement a Queue — each with different use cases:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Class&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LinkedList&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non-blocking&lt;/td&gt;
&lt;td&gt;Basic FIFO queue&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PriorityQueue&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non-blocking&lt;/td&gt;
&lt;td&gt;Orders elements by priority&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ArrayDeque&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non-blocking&lt;/td&gt;
&lt;td&gt;Double-ended queue (Deque)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ConcurrentLinkedQueue&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Thread-safe&lt;/td&gt;
&lt;td&gt;Non-blocking queue for concurrency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;BlockingQueue&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Thread-safe&lt;/td&gt;
&lt;td&gt;Used in producer-consumer patterns&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;⚙️ Example: PriorityQueue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.PriorityQueue;

public class PriorityExample {
    public static void main(String[] args) {
        PriorityQueue&amp;lt;Integer&amp;gt; pq = new PriorityQueue&amp;lt;&amp;gt;();

        pq.add(30);
        pq.add(10);
        pq.add(20);

        while (!pq.isEmpty()) {
            System.out.println(pq.poll());
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
20
30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elements are automatically ordered — smallest comes first!&lt;/p&gt;




&lt;p&gt;🔁 Real-World Use Cases of Queues&lt;/p&gt;

&lt;p&gt;Queues are everywhere — in both system-level and application-level designs:&lt;/p&gt;

&lt;p&gt;🖥 1. Task Scheduling&lt;/p&gt;

&lt;p&gt;OS schedulers and thread pools use queues to manage jobs in order.&lt;/p&gt;

&lt;p&gt;📬 2. Messaging Systems&lt;/p&gt;

&lt;p&gt;Message brokers (like Kafka, RabbitMQ, or ActiveMQ) rely on queues to store and process messages sequentially.&lt;/p&gt;

&lt;p&gt;🌐 3. Web Servers&lt;/p&gt;

&lt;p&gt;Web servers manage client requests using queues to handle high traffic efficiently.&lt;/p&gt;

&lt;p&gt;🎮 4. Gaming&lt;/p&gt;

&lt;p&gt;Queues manage player actions, events, and network messages to ensure order.&lt;/p&gt;

&lt;p&gt;🧮 5. Producer–Consumer Pattern&lt;/p&gt;

&lt;p&gt;A background thread produces data, another consumes it — synchronized via a queue.&lt;/p&gt;




&lt;p&gt;⚙️ Example: Producer–Consumer with Queue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.LinkedList;
import java.util.Queue;

public class ProducerConsumer {
    public static void main(String[] args) {
        Queue&amp;lt;Integer&amp;gt; queue = new LinkedList&amp;lt;&amp;gt;();

        // Producer
        for (int i = 1; i &amp;lt;= 5; i++) {
            queue.add(i);
            System.out.println("Produced: " + i);
        }

        // Consumer
        while (!queue.isEmpty()) {
            System.out.println("Consumed: " + queue.poll());
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🧮 Time Complexity&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Time Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Enqueue&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dequeue&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Peek&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Search&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;⚠️ Common Mistakes&lt;/p&gt;

&lt;p&gt;❌ Using add() and not handling exceptions when queue is full.&lt;/p&gt;

&lt;p&gt;❌ Confusing poll() and remove() — poll() is safer.&lt;/p&gt;

&lt;p&gt;❌ Using wrong queue type for concurrency.&lt;/p&gt;

&lt;p&gt;❌ Expecting random access (queues don’t support indexes).&lt;/p&gt;




&lt;p&gt;⚖️ Stack vs Queue&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Stack&lt;/th&gt;
&lt;th&gt;Queue&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Principle&lt;/td&gt;
&lt;td&gt;LIFO&lt;/td&gt;
&lt;td&gt;FIFO&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Example&lt;/td&gt;
&lt;td&gt;Browser Back Button&lt;/td&gt;
&lt;td&gt;Task Scheduling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Key Methods&lt;/td&gt;
&lt;td&gt;push(), pop(), peek()&lt;/td&gt;
&lt;td&gt;add(), poll(), peek()&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;💡 Final Thought&lt;/p&gt;

&lt;p&gt;The Queue is the backbone of many real-world systems — from task schedulers to message queues.&lt;br&gt;
It models the flow of data, requests, and actions in a natural and organized way.&lt;/p&gt;

&lt;p&gt;Mastering Queues helps you think like a system designer — not just a programmer. 🧠💪&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>java</category>
    </item>
    <item>
      <title>🧠 Mastering Data Structures in Java — Part 4: Stack</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Mon, 27 Oct 2025 14:20:25 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-3-stack-3p7g</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-3-stack-3p7g</guid>
      <description>&lt;p&gt;🔍 What Is a Stack?&lt;/p&gt;

&lt;p&gt;A Stack is a Last-In, First-Out (LIFO) data structure.&lt;br&gt;
This means the last element added is the first one to be removed — just like a stack of plates 🍽️.&lt;/p&gt;

&lt;p&gt;👉 You can only access the top plate (element) at any given time.&lt;/p&gt;

&lt;p&gt;In Java, the Stack is part of the &lt;code&gt;java.util&lt;/code&gt; package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.Stack;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;⚙️ Example: Using Stack in Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack&amp;lt;String&amp;gt; browserHistory = new Stack&amp;lt;&amp;gt;();

        browserHistory.push("Google");
        browserHistory.push("YouTube");
        browserHistory.push("GitHub");

        System.out.println(browserHistory); // [Google, YouTube, GitHub]

        System.out.println("Top: " + browserHistory.peek()); // GitHub

        browserHistory.pop();
        System.out.println("After back: " + browserHistory); // [Google, YouTube]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧩 Operations:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;push()&lt;/code&gt; → Add element to top&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pop()&lt;/code&gt; → Remove top element&lt;/p&gt;

&lt;p&gt;&lt;code&gt;peek()&lt;/code&gt; → View top element&lt;/p&gt;

&lt;p&gt;&lt;code&gt;empty()&lt;/code&gt; → Check if stack is empty&lt;/p&gt;

&lt;p&gt;&lt;code&gt;search()&lt;/code&gt; → Find position of an element&lt;/p&gt;




&lt;p&gt;⚙️ Internal Working&lt;/p&gt;

&lt;p&gt;Under the hood, Java’s Stack class extends Vector, so it’s synchronized and thread-safe, but slower in single-threaded contexts.&lt;/p&gt;

&lt;p&gt;A modern alternative is Deque, which provides faster stack operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.ArrayDeque;

Deque&amp;lt;Integer&amp;gt; stack = new ArrayDeque&amp;lt;&amp;gt;();
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 Use &lt;code&gt;ArrayDeque&lt;/code&gt; instead of Stack for better performance.&lt;/p&gt;




&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Time Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Push&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pop&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Peek&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Search&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;💡 Real-World Uses of Stack&lt;/p&gt;

&lt;p&gt;Stacks are everywhere in computer science. Here are a few real-world scenarios:&lt;/p&gt;

&lt;p&gt;🧾 1. Undo/Redo Mechanisms&lt;/p&gt;

&lt;p&gt;Applications like text editors use stacks to track user actions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Undo Stack stores previous states.&lt;/li&gt;
&lt;li&gt;Redo Stack re-applies undone actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🌐 2. Browser Navigation&lt;/p&gt;

&lt;p&gt;Every time you visit a page, it’s pushed onto the stack.&lt;br&gt;
When you click “Back,” it pops the last page and shows the previous one.&lt;/p&gt;

&lt;p&gt;🧮 3. Expression Evaluation&lt;/p&gt;

&lt;p&gt;Stacks are used in:&lt;/p&gt;

&lt;p&gt;Converting infix to postfix expressions&lt;/p&gt;

&lt;p&gt;Evaluating arithmetic expressions&lt;br&gt;
Example: &lt;code&gt;(3 + 5) * 2&lt;/code&gt; → Processed using stacks.&lt;/p&gt;

&lt;p&gt;💻 4. Function Call Stack&lt;/p&gt;

&lt;p&gt;Each method call is pushed onto the call stack.&lt;br&gt;
When the method finishes, it’s popped off. This is handled automatically by the JVM.&lt;/p&gt;

&lt;p&gt;🔁 5. DFS Algorithms&lt;/p&gt;

&lt;p&gt;Depth-First Search (DFS) in graphs uses a stack to keep track of visited nodes.&lt;/p&gt;




&lt;p&gt;⚠️ Common Mistakes&lt;/p&gt;

&lt;p&gt;❌ Forgetting that Stack is synchronized → may impact performance.&lt;/p&gt;

&lt;p&gt;❌ Using Stack where ArrayDeque is better.&lt;/p&gt;

&lt;p&gt;❌ Popping from an empty stack → throws &lt;code&gt;EmptyStackException&lt;/code&gt;.&lt;/p&gt;




&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Stack&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Principle&lt;/td&gt;
&lt;td&gt;LIFO&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Core Operations&lt;/td&gt;
&lt;td&gt;push(), pop(), peek()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;O(1) for most&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implementation&lt;/td&gt;
&lt;td&gt;Vector (synchronized) or ArrayDeque (recommended)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best For&lt;/td&gt;
&lt;td&gt;Backtracking, undo systems, expression parsing, recursion tracking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🧠 Final Thought&lt;/p&gt;

&lt;p&gt;A Stack is one of the simplest yet most powerful data structures.&lt;br&gt;
It represents how programs manage memory, logic, and state.&lt;br&gt;
Once you understand stacks, you understand recursion, backtracking, and control flow — all critical to becoming an advanced Java developer. 💪&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>architecture</category>
      <category>performance</category>
    </item>
    <item>
      <title>🧠 Mastering Data Structures in Java - Part 3 - LinkedList</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Sun, 26 Oct 2025 17:47:36 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-linkedlist-part-3-3dmm</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-linkedlist-part-3-3dmm</guid>
      <description>&lt;p&gt;🔍 What Is a LinkedList?&lt;/p&gt;

&lt;p&gt;A LinkedList in Java is a linear data structure where elements (called nodes) are connected using references (links).&lt;br&gt;
Each node stores:&lt;/p&gt;

&lt;p&gt;The data itself, and&lt;/p&gt;

&lt;p&gt;A reference (or pointer) to the next (and previous) node in the sequence.&lt;/p&gt;

&lt;p&gt;In Java, LinkedList is a doubly-linked list — meaning each node knows both its previous and next neighbor.&lt;/p&gt;

&lt;p&gt;You’ll find it in the Java Collections Framework:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import java.util.LinkedList;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;⚙️ Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.LinkedList;

public class Example {
    public static void main(String[] args) {
        LinkedList&amp;lt;String&amp;gt; tasks = new LinkedList&amp;lt;&amp;gt;();

        tasks.add("Write code");
        tasks.add("Test code");
        tasks.add("Deploy app");

        System.out.println(tasks); // [Write code, Test code, Deploy app]

        tasks.addFirst("Plan feature");
        tasks.addLast("Refactor");
        System.out.println(tasks); // [Plan feature, Write code, Test code, Deploy app, Refactor]

        tasks.remove("Test code");
        System.out.println(tasks); // [Plan feature, Write code, Deploy app, Refactor]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🧩 How It Works Internally&lt;/p&gt;

&lt;p&gt;A LinkedList consists of nodes linked together.&lt;br&gt;
Each node contains:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[ previous | data | next ]&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When adding/removing elements:&lt;/p&gt;

&lt;p&gt;ArrayList shifts elements → O(n)&lt;/p&gt;

&lt;p&gt;LinkedList just changes links → O(1) (if position is known)&lt;/p&gt;

&lt;p&gt;However, random access like get(index) is slower → O(n).&lt;/p&gt;



&lt;p&gt;⚙️ Common Methods&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;add(E e)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds an element at the end&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;addFirst(E e)&lt;/code&gt; / &lt;code&gt;addLast(E e)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Adds at beginning or end&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;remove()&lt;/code&gt; / &lt;code&gt;removeFirst()&lt;/code&gt; / &lt;code&gt;removeLast()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Removes elements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;get(int index)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Retrieves element by index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;getFirst()&lt;/code&gt; / &lt;code&gt;getLast()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Retrieves first or last element&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;size()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns list size&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;clear()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes all elements&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;p&gt;⚖️ LinkedList vs ArrayList&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;ArrayList&lt;/th&gt;
&lt;th&gt;LinkedList&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Structure&lt;/td&gt;
&lt;td&gt;Dynamic array&lt;/td&gt;
&lt;td&gt;Doubly-linked list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access time&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert/remove (middle)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1) if node known&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory usage&lt;/td&gt;
&lt;td&gt;Less&lt;/td&gt;
&lt;td&gt;More (extra pointers)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use case&lt;/td&gt;
&lt;td&gt;Frequent access&lt;/td&gt;
&lt;td&gt;Frequent insert/delete&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;✅ Rule of thumb:&lt;/p&gt;

&lt;p&gt;Choose ArrayList → when you read data often.&lt;/p&gt;

&lt;p&gt;Choose LinkedList → when you insert or remove elements often.&lt;/p&gt;



&lt;p&gt;🌍 Real-World Use Cases&lt;/p&gt;

&lt;p&gt;Music players: Playlist queue (next/previous song navigation)&lt;/p&gt;

&lt;p&gt;Undo/Redo systems: Navigating previous and next actions&lt;/p&gt;

&lt;p&gt;Web browsers: Forward and backward page navigation&lt;/p&gt;

&lt;p&gt;Task scheduling: Maintaining dynamic to-do or job queues&lt;/p&gt;

&lt;p&gt;Memory management systems: Storing free/used memory blocks dynamically&lt;/p&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LinkedList&amp;lt;String&amp;gt; browserHistory = new LinkedList&amp;lt;&amp;gt;();
browserHistory.add("Google");
browserHistory.add("StackOverflow");
browserHistory.add("GitHub");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;⚡ Pro Tips&lt;/p&gt;

&lt;p&gt;Use LinkedList for queues and stacks — it implements both Deque and List interfaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Deque&amp;lt;Integer&amp;gt; queue = new LinkedList&amp;lt;&amp;gt;();
queue.add(1);
queue.add(2);
System.out.println(queue.remove()); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Avoid random access like &lt;code&gt;list.get(1000)&lt;/code&gt; — it’s slow.&lt;/li&gt;
&lt;li&gt;Can contain null elements.&lt;/li&gt;
&lt;li&gt;Not thread-safe, so use &lt;code&gt;Collections.synchronizedList()&lt;/code&gt; if needed.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🧮 Performance Summary&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Time Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Add/Remove First&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add/Remove Last&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add/Remove Middle&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Get by Index&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Iteration&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;💡 Final Thought&lt;/p&gt;

&lt;p&gt;Think of LinkedList as a flexible train — you can easily attach or detach wagons (nodes) without rearranging the entire train.&lt;br&gt;
It might not be as fast to jump between wagons, but it’s perfect when the structure itself keeps changing.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>datastructures</category>
      <category>java</category>
      <category>performance</category>
    </item>
    <item>
      <title>🚀 Mastering Data Structures in Java — Part 2: ArrayList</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Sat, 25 Oct 2025 17:35:50 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-2-arraylist-1co5</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-2-arraylist-1co5</guid>
      <description>&lt;p&gt;If Arrays are the foundation of data storage in Java, then &lt;code&gt;ArrayList&lt;/code&gt;is the evolved version — smarter, flexible, and ready for modern programming needs.&lt;/p&gt;

&lt;p&gt;In this post, we’ll explore how &lt;code&gt;ArrayList&lt;/code&gt;works, how it differs from arrays, when to use it, and how to get the most out of it in real-world applications.&lt;/p&gt;




&lt;p&gt;🔹 What Is an ArrayList?&lt;/p&gt;

&lt;p&gt;An ArrayList is a resizable array — part of Java’s &lt;code&gt;java.util&lt;/code&gt; package — that grows or shrinks automatically as elements are added or removed.&lt;/p&gt;

&lt;p&gt;It’s backed by an internal Array, but you don’t have to worry about resizing or managing indexes manually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.ArrayList;

ArrayList&amp;lt;String&amp;gt; names = new ArrayList&amp;lt;&amp;gt;();
names.add("Mohammed");
names.add("Sara");
names.add("Omar");

System.out.println(names); // [Mohammed, Sara, Omar]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔹 How ArrayList Works Internally&lt;/p&gt;

&lt;p&gt;Under the hood, an &lt;code&gt;ArrayList&lt;/code&gt; maintains:&lt;/p&gt;

&lt;p&gt;A dynamic array to store elements.&lt;/p&gt;

&lt;p&gt;A capacity (current size of the internal array).&lt;/p&gt;

&lt;p&gt;When the array fills up, a new array with a larger capacity is created, and elements are copied over.&lt;/p&gt;

&lt;p&gt;That’s why adding elements can sometimes trigger a resize operation — which is more expensive than a simple insertion.&lt;/p&gt;

&lt;p&gt;🧩 Default capacity grows by 50% each time (since Java 8).&lt;/p&gt;




&lt;p&gt;🔹 Declaring and Initializing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ArrayList&amp;lt;Integer&amp;gt; numbers = new ArrayList&amp;lt;&amp;gt;();
numbers.add(10);
numbers.add(20);
numbers.add(30);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Using type inference (Java 7+):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;String&amp;gt; fruits = new ArrayList&amp;lt;&amp;gt;();&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;✅ Initializing with values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;String&amp;gt; colors = new ArrayList&amp;lt;&amp;gt;(List.of("Red", "Green", "Blue"));&lt;/code&gt;&lt;/p&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;add(E e)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds element to the end&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;add(int index, E e)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Inserts element at a specific index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;get(int index)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns element at index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;set(int index, E e)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Updates element at index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;remove(int index)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes element at index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;size()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns number of elements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;contains(Object o)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Checks if element exists&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;clear()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes all elements&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;📘 Full Reference: &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html" rel="noopener noreferrer"&gt;ArrayList (Java SE 8 Docs)&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🔹 Example: Managing a To-Do List&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.ArrayList;

public class TodoApp {
    public static void main(String[] args) {
        ArrayList&amp;lt;String&amp;gt; tasks = new ArrayList&amp;lt;&amp;gt;();

        tasks.add("Learn Java");
        tasks.add("Build Projects");
        tasks.add("Read about JVM");

        tasks.remove("Read about JVM");
        tasks.add(1, "Practice Data Structures");

        System.out.println("Tasks: " + tasks);
        System.out.println("First task: " + tasks.get(0));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧾 Output:&lt;/p&gt;

&lt;p&gt;Tasks: [Learn Java, Practice Data Structures, Build Projects]&lt;br&gt;
First task: Learn Java&lt;/p&gt;



&lt;p&gt;🔹 Differences Between Array and ArrayList&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Array&lt;/th&gt;
&lt;th&gt;ArrayList&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;Fixed&lt;/td&gt;
&lt;td&gt;Dynamic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Type&lt;/td&gt;
&lt;td&gt;Can hold primitives and objects&lt;/td&gt;
&lt;td&gt;Only objects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Syntax&lt;/td&gt;
&lt;td&gt;&lt;code&gt;int[] arr = new int[5];&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ArrayList&amp;lt;Integer&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Slightly faster for fixed data&lt;/td&gt;
&lt;td&gt;Slightly slower (resize overhead)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Utilities&lt;/td&gt;
&lt;td&gt;No built-in utilities&lt;/td&gt;
&lt;td&gt;Rich methods (&lt;code&gt;add&lt;/code&gt;, &lt;code&gt;remove&lt;/code&gt;, &lt;code&gt;contains&lt;/code&gt;, etc.)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;p&gt;🌍 Where ArrayLists Are Used in Real-World Projects&lt;/p&gt;

&lt;p&gt;ArrayLists are used everywhere in real-world Java applications — especially when you don’t know the size of your data beforehand:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User Management Systems 👥&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To store and manage dynamic user data (names, IDs, profiles).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;User&amp;gt; users = new ArrayList&amp;lt;&amp;gt;();&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;E-Commerce Platforms 🛒&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To hold lists of products in a shopping cart or search results.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;Product&amp;gt; cartItems = new ArrayList&amp;lt;&amp;gt;();&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Mobile Applications 📱&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To display items in a RecyclerView (Android) — typically backed by an ArrayList.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Log and Event Systems 🧾&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To accumulate logs dynamically before writing to a file or database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ArrayList&amp;lt;String&amp;gt; logs = new ArrayList&amp;lt;&amp;gt;();
logs.add("Application started");
logs.add("User logged in");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔹 Performance Insights&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Time Complexity&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Access (get/set)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;Direct index access&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert at end&lt;/td&gt;
&lt;td&gt;Amortized O(1)&lt;/td&gt;
&lt;td&gt;Resizing may occur&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insert/remove at middle&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;Elements shifted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Search&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;Linear search&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Clear&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;Removes all elements&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;🧠 Pro Tip:&lt;br&gt;
If you plan heavy insertion/removal operations, consider using a LinkedList&lt;br&gt;
 instead.&lt;/p&gt;



&lt;p&gt;🔹 Avoid Common Mistakes&lt;/p&gt;

&lt;p&gt;🚫 Forgetting Generics:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ArrayList list = new ArrayList(); // ❌ Not type-safe
list.add("Hello");
list.add(123); // Compiles, fails later
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Correct:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🚫 Using == for value comparison:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (list.get(0) == "Hello") // ❌&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;✅ Use .equals() instead:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (list.get(0).equals("Hello"))&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;Pro Tips to Master ArrayList&lt;/p&gt;

&lt;p&gt;✅ Set an initial capacity if you know approximate data size:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayList&amp;lt;Integer&amp;gt; list = new ArrayList&amp;lt;&amp;gt;(1000);&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This avoids multiple costly resize operations.&lt;/p&gt;

&lt;p&gt;✅ Convert between Array and ArrayList:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String[] arr = {"A", "B", "C"};
ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;&amp;gt;(Arrays.asList(arr));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Use streams with ArrayList:&lt;/p&gt;

&lt;p&gt;list.stream().filter(x -&amp;gt; x.startsWith("A")).forEach(System.out::println);&lt;/p&gt;




&lt;p&gt;💬 Final Thought&lt;/p&gt;

&lt;p&gt;ArrayLists bring the power of dynamic storage while keeping the simplicity of arrays.&lt;br&gt;
Master them, and you’ll unlock a crucial step toward understanding Java Collections Framework.&lt;/p&gt;

&lt;p&gt;Next up in the series:&lt;br&gt;
🔜 LinkedList in Java — When You Need Fast Insertions&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>datastructures</category>
      <category>architecture</category>
    </item>
    <item>
      <title>🧠 Mastering Data Structures in Java — Part 1: Arrays</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Fri, 24 Oct 2025 14:11:52 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-1-arrays-2ia3</link>
      <guid>https://forem.com/mohamad_mhana/mastering-data-structures-in-java-part-1-arrays-2ia3</guid>
      <description>&lt;p&gt;If you’re serious about becoming an excellent Java developer, understanding data structures isn’t optional — it’s essential.&lt;br&gt;
They define how your program organizes and manipulates data, and mastering them will drastically improve your problem-solving and efficiency.&lt;/p&gt;

&lt;p&gt;Let’s start from the foundation of all data structures — the Array.&lt;/p&gt;



&lt;p&gt;🔹 What Is an Array?&lt;/p&gt;

&lt;p&gt;An Array in Java is a container object that holds a fixed number of elements of a single type.&lt;/p&gt;

&lt;p&gt;Each element is accessed using an index, starting from 0.&lt;br&gt;
Think of it like labeled boxes 🧱 — each one stores a value, and you can find it instantly if you know the label.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]); // Output: 10
System.out.println(numbers[4]); // Output: 50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you create an array, its size is fixed — it can’t grow or shrink.&lt;/p&gt;




&lt;p&gt;🔹 How Arrays Work in Memory&lt;/p&gt;

&lt;p&gt;Arrays in Java are objects stored on the heap, but their reference lives on the stack.&lt;/p&gt;

&lt;p&gt;When you create an array like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] arr = new int[5];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;arr → reference variable (on stack)&lt;/p&gt;

&lt;p&gt;new int[5] → actual array object (on heap)&lt;/p&gt;

&lt;p&gt;Each index points to a memory location that holds your data.&lt;/p&gt;




&lt;p&gt;🔹 Types of Arrays&lt;/p&gt;

&lt;p&gt;One-Dimensional Array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String[] fruits = {"Apple", "Banana", "Cherry"};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multi-Dimensional Array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6}
};
System.out.println(matrix[1][2]); // Output: 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jagged Arrays (arrays of arrays with different sizes)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[][] jagged = {
    {1, 2},
    {3, 4, 5},
    {6}
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔹 Why Use Arrays?&lt;/p&gt;

&lt;p&gt;Arrays are fast and efficient for:&lt;/p&gt;

&lt;p&gt;Accessing elements directly by index — O(1)&lt;/p&gt;

&lt;p&gt;Iterating through elements — O(n)&lt;/p&gt;

&lt;p&gt;They’re memory efficient, and they form the foundation of higher-level data structures like &lt;code&gt;ArrayList&lt;/code&gt;, &lt;code&gt;Stack&lt;/code&gt;, and &lt;code&gt;Queue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But they also have limitations:&lt;/p&gt;

&lt;p&gt;❌ Fixed size once created&lt;/p&gt;

&lt;p&gt;❌ Costly insertions or deletions in the middle&lt;/p&gt;

&lt;p&gt;❌ Lack of built-in resizing or dynamic memory handling&lt;/p&gt;

&lt;p&gt;That’s why we have collections like &lt;code&gt;ArrayList&lt;/code&gt; — but to understand those, you must master arrays first.&lt;/p&gt;




&lt;p&gt;🌍 Where Arrays Are Used in Real-World Projects&lt;/p&gt;

&lt;p&gt;Arrays aren’t just for theory or beginner examples — they play a crucial role behind the scenes in almost every Java project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Game Development 🎮&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arrays are used to store:&lt;/p&gt;

&lt;p&gt;Game maps or grids (e.g., tiles in a 2D game)&lt;/p&gt;

&lt;p&gt;Player inventories&lt;/p&gt;

&lt;p&gt;Coordinates and movement history&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;char[][] map = {
    {'#', '#', '#'},
    {'#', 'P', ' '},
    {'#', '#', '#'}
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Data Processing and Analytics 📊&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When processing numeric datasets, arrays hold large volumes of data efficiently for computation.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
Processing sensor readings, financial data, or temperature logs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;double[] temperatures = {22.5, 21.8, 23.1, 24.0};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Image and Audio Processing 🎧&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Images are stored as pixel arrays; sounds are stored as waveform arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[][] image = new int[1080][1920]; // pixel values

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;System-Level Programming ⚙️&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arrays are heavily used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Buffer management&lt;/li&gt;
&lt;li&gt;Networking (byte arrays)&lt;/li&gt;
&lt;li&gt;Reading/writing data streams
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;byte[] buffer = new byte[1024];
inputStream.read(buffer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Machine Learning / AI 🤖&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Libraries often rely on multi-dimensional arrays (tensors) for operations like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Matrix multiplications&lt;/li&gt;
&lt;li&gt;Feature scaling&lt;/li&gt;
&lt;li&gt;Weight storage&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔹 Common Array Operations&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Traversing an Array
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] nums = {1, 2, 3, 4, 5};
for (int num : nums) {
    System.out.println(num);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Finding the Maximum Element
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int max = nums[0];
for (int i = 1; i &amp;lt; nums.length; i++) {
    if (nums[i] &amp;gt; max) {
        max = nums[i];
    }
}
System.out.println("Max: " + max);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Copying Arrays&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;✅ Using System.arraycopy()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] src = {1, 2, 3};
int[] dest = new int[3];
System.arraycopy(src, 0, dest, 0, src.length);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Using Arrays.copyOf()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] copied = Arrays.copyOf(src, src.length);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Sorting Arrays
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.Arrays;

int[] arr = {5, 2, 8, 1, 3};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // [1, 2, 3, 5, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Searching in Arrays
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int index = Arrays.binarySearch(arr, 5);
System.out.println("Found at index: " + index);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔹 Best Practices When Working with Arrays&lt;/p&gt;

&lt;p&gt;✅ Use meaningful names&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] studentScores = new int[10];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Always check array bounds&lt;br&gt;
Accessing an invalid index causes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ArrayIndexOutOfBoundsException

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Use Arrays class for utilities&lt;br&gt;
&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html" rel="noopener noreferrer"&gt;java.util.Arrays&lt;br&gt;
&lt;/a&gt; provides methods like sort(), equals(), copyOf(), and fill().&lt;/p&gt;

&lt;p&gt;✅ Prefer Collections for flexibility&lt;br&gt;
If your data size is not fixed, use &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html" rel="noopener noreferrer"&gt;ArrayList&lt;/a&gt;&lt;br&gt;
 instead.&lt;/p&gt;



&lt;p&gt;🔹 Practical Example: Counting Even Numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class EvenCounter {
    public static void main(String[] args) {
        int[] numbers = {2, 5, 8, 10, 13, 20};
        int count = 0;

        for (int n : numbers) {
            if (n % 2 == 0) count++;
        }

        System.out.println("Even numbers: " + count);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🔹 Common Mistakes to Avoid&lt;/p&gt;

&lt;p&gt;🚫 Forgetting new when creating arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] arr; 
arr = {1, 2, 3}; // ❌ Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] arr = new int[]{1, 2, 3};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🚫 Mixing array types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] arr = {1, 2, "three"}; // ❌ Error

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;💡 Final Thought&lt;/p&gt;

&lt;p&gt;Arrays may seem simple, but mastering them teaches you how Java handles memory and performance at a low level.&lt;br&gt;
Once you get Arrays deeply, you’ll understand why Java Collections are built the way they are — and you’ll use them more intelligently.&lt;/p&gt;




&lt;p&gt;Next up in the series:&lt;br&gt;
🚀 “ArrayList in Java — Dynamic Arrays that Grow with You”&lt;/p&gt;

</description>
      <category>programming</category>
      <category>architecture</category>
      <category>java</category>
      <category>performance</category>
    </item>
    <item>
      <title>🧬 Java Supports Single Inheritance Only — But!</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Thu, 23 Oct 2025 18:16:21 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/java-supports-single-inheritance-only-but-3gp3</link>
      <guid>https://forem.com/mohamad_mhana/java-supports-single-inheritance-only-but-3gp3</guid>
      <description>&lt;p&gt;If you’ve ever heard that “Java supports only single inheritance”, you might think Java is limited compared to languages like C++ that allow multiple inheritance.&lt;/p&gt;

&lt;p&gt;But the truth is... Java gives you the best of both worlds — simplicity and flexibility — without the messy diamond problems. Let’s break it down 👇&lt;/p&gt;




&lt;p&gt;🧩 What Is Inheritance in Java?&lt;/p&gt;

&lt;p&gt;Inheritance allows one class to acquire the properties and behaviors (fields and methods) of another.&lt;/p&gt;

&lt;p&gt;It’s one of the core pillars of Object-Oriented Programming (OOP).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking...");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();  // Inherited
        d.bark(); // Own behavior
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Dog inherits from Animal.&lt;br&gt;
✅ We get code reuse and a clear hierarchy.&lt;/p&gt;



&lt;p&gt;🚫 Why Only Single Inheritance?&lt;/p&gt;

&lt;p&gt;In Java, a class cannot inherit from more than one class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class A {}
class B {}
class C extends A, B {} // ❌ Compilation error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reason?&lt;br&gt;
To avoid ambiguity and complexity that come from multiple inheritance.&lt;/p&gt;

&lt;p&gt;Imagine both A and B have a method show().&lt;br&gt;
If C inherits from both, which show() should it call?&lt;/p&gt;

&lt;p&gt;That’s the famous “Diamond Problem” 💎&lt;/p&gt;



&lt;p&gt;💡 But Wait — Java Isn’t Limited!&lt;/p&gt;

&lt;p&gt;Even though Java restricts multiple inheritance for classes,&lt;br&gt;
it allows multiple inheritance through interfaces ✅&lt;/p&gt;



&lt;p&gt;⚙️ Example: Multiple Inheritance via Interfaces&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface CanFly {
    void fly();
}

interface CanSwim {
    void swim();
}

class Duck implements CanFly, CanSwim {
    public void fly() {
        System.out.println("Flying...");
    }

    public void swim() {
        System.out.println("Swimming...");
    }
}

public class Main {
    public static void main(String[] args) {
        Duck d = new Duck();
        d.fly();
        d.swim();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ A class can implement multiple interfaces&lt;br&gt;
✅ Avoids ambiguity because interfaces only declare methods — they don’t provide full implementations (unless using &lt;code&gt;default&lt;/code&gt; methods, handled explicitly).&lt;/p&gt;




&lt;p&gt;🧠 Why Interfaces Solve the Problem&lt;/p&gt;

&lt;p&gt;Interfaces give Java a way to provide multiple behavior types safely:&lt;/p&gt;

&lt;p&gt;They specify what a class should do, not how.&lt;/p&gt;

&lt;p&gt;The implementing class defines the actual behavior.&lt;/p&gt;

&lt;p&gt;No confusion — no diamond problem.&lt;/p&gt;

&lt;p&gt;And since Java 8, interfaces can also have default methods,&lt;br&gt;
but if there’s a conflict, the implementing class must override it — giving full control to the developer.&lt;/p&gt;




&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Multiple Inheritance Allowed?&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Classes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;class C extends A, B&lt;/code&gt; (not allowed)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Interfaces&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;class C implements X, Y&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Mix (Class + Interfaces)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;class C extends A implements X, Y&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🧭 The Real Takeaway&lt;/p&gt;

&lt;p&gt;Java chose clarity over chaos.&lt;br&gt;
You still get multiple behaviors through interfaces,&lt;br&gt;
but without the dangers of conflicting implementations.&lt;/p&gt;

&lt;p&gt;👉 So yes — Java supports single inheritance of classes,&lt;br&gt;
but multiple inheritance of type definitions (interfaces).&lt;/p&gt;




&lt;p&gt;💬 Final Thought&lt;/p&gt;

&lt;p&gt;Think of it this way:&lt;/p&gt;

&lt;p&gt;Java lets you inherit structure once,&lt;br&gt;
but inherit abilities many times. 💪&lt;/p&gt;




&lt;p&gt;❓Question for You&lt;/p&gt;

&lt;p&gt;Do you prefer using interfaces for flexibility or abstract classes for structured hierarchies?&lt;br&gt;
Share your thoughts in the comments 👇&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>architecture</category>
      <category>coding</category>
    </item>
    <item>
      <title>🔒public, private, and protected in Java</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Wed, 22 Oct 2025 18:00:36 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/public-private-and-protected-in-java-1omj</link>
      <guid>https://forem.com/mohamad_mhana/public-private-and-protected-in-java-1omj</guid>
      <description>&lt;p&gt;n Java, access modifiers control who can see or use your class members (fields, methods, or constructors).&lt;/p&gt;

&lt;p&gt;They’re one of the pillars of encapsulation, helping you protect your code and define clear boundaries between classes.&lt;/p&gt;

&lt;p&gt;Let’s break down the three most important ones — public, private, and protected — and when to use each 👇&lt;/p&gt;




&lt;p&gt;🧩 What Are Access Modifiers?&lt;/p&gt;

&lt;p&gt;Access modifiers define the visibility of a class or its members across your project.&lt;/p&gt;

&lt;p&gt;In Java, we mainly use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public&lt;/code&gt; → visible everywhere&lt;/p&gt;

&lt;p&gt;&lt;code&gt;private&lt;/code&gt; → visible only inside the same class&lt;/p&gt;

&lt;p&gt;&lt;code&gt;protected&lt;/code&gt; → visible in the same package + subclasses&lt;/p&gt;

&lt;p&gt;There’s also default (package-private) when no modifier is specified — visible only inside the same package.&lt;/p&gt;




&lt;p&gt;🌍 public: Accessible Everywhere&lt;/p&gt;

&lt;p&gt;A public member can be accessed from anywhere — even from another package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Car {
    public String brand;

    public void start() {
        System.out.println("Car started!");
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.brand = "Toyota";   // ✅ Allowed
        car.start();            // ✅ Allowed
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ When to use:&lt;/p&gt;

&lt;p&gt;For methods or classes that need to be used by many other parts of your program (like APIs or utility methods).&lt;/p&gt;

&lt;p&gt;When exposing controlled access to other packages.&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html" rel="noopener noreferrer"&gt;Java public Access Modifier Docs&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🔒 private: Hide and Protect&lt;/p&gt;

&lt;p&gt;A private member is only accessible inside the same class.&lt;br&gt;
This is the strongest level of protection and a cornerstone of encapsulation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Account {
    private double balance;

    public void deposit(double amount) {
        if (amount &amp;gt; 0)
            balance += amount;
    }

    public double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        Account acc = new Account();
        // acc.balance = 1000; ❌ Error
        acc.deposit(500);       // ✅ Allowed
        System.out.println(acc.getBalance()); // ✅ 500.0
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ When to use:&lt;/p&gt;

&lt;p&gt;For sensitive or internal data you don’t want accessed directly.&lt;/p&gt;

&lt;p&gt;To maintain control over how values are read or changed (through getters/setters).&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html#encapsulating" rel="noopener noreferrer"&gt;Encapsulation in Java&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🛡 protected: Share with Subclasses&lt;/p&gt;

&lt;p&gt;A protected member is accessible:&lt;/p&gt;

&lt;p&gt;Inside the same package, and&lt;/p&gt;

&lt;p&gt;By subclasses, even if they’re in another package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
    protected void makeSound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void bark() {
        makeSound(); // ✅ Accessible because Dog extends Animal
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.bark();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ When to use:&lt;/p&gt;

&lt;p&gt;When designing a class meant to be extended, and you want subclasses to reuse some behaviors safely.&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html#protected" rel="noopener noreferrer"&gt;Protected Access in Java&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;⚙️ Summary of Access Levels&lt;/p&gt;

&lt;p&gt;Here’s a simple reference you can copy easily:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Modifier&lt;/th&gt;
&lt;th&gt;Same Class&lt;/th&gt;
&lt;th&gt;Same Package&lt;/th&gt;
&lt;th&gt;Subclass&lt;/th&gt;
&lt;th&gt;Outside Package&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;public&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;protected&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;em&gt;default&lt;/em&gt; (no modifier)&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;private&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🧠 Why Access Modifiers Matter&lt;/p&gt;

&lt;p&gt;They’re not just about restrictions — they’re about control and clarity:&lt;/p&gt;

&lt;p&gt;They make your classes safer by preventing accidental misuse.&lt;/p&gt;

&lt;p&gt;They help you design cleaner APIs.&lt;/p&gt;

&lt;p&gt;They make your code easier to maintain, since you decide what’s exposed and what’s hidden.&lt;/p&gt;




&lt;p&gt;💡 Best Practices&lt;/p&gt;

&lt;p&gt;Start as restrictive as possible (private) and loosen only when necessary.&lt;/p&gt;

&lt;p&gt;Keep fields private — use getters/setters to manage access.&lt;/p&gt;

&lt;p&gt;Use protected only for intended inheritance.&lt;/p&gt;

&lt;p&gt;Use public for APIs or methods meant for other classes/packages.&lt;/p&gt;




&lt;p&gt;🧭 Final Thoughts&lt;/p&gt;

&lt;p&gt;Access modifiers are one of Java’s simplest yet most powerful features.&lt;br&gt;
They help you protect your codebase, control visibility, and build robust, maintainable systems.&lt;/p&gt;

&lt;p&gt;So next time you write a class, think:&lt;/p&gt;

&lt;p&gt;“Who really needs to see this?” 👀&lt;/p&gt;




&lt;p&gt;❓Question for You&lt;/p&gt;

&lt;p&gt;Do you usually design your classes starting with everything private or open them up as you go?&lt;br&gt;
Share your approach in the comments 👇&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>architecture</category>
      <category>beginners</category>
    </item>
    <item>
      <title>🧺 Built-in Data Structures in Java</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Tue, 21 Oct 2025 12:30:56 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/built-in-data-structures-in-java-1fl0</link>
      <guid>https://forem.com/mohamad_mhana/built-in-data-structures-in-java-1fl0</guid>
      <description>&lt;p&gt;When you start writing real-world Java programs, managing data efficiently becomes crucial. That’s where Java’s built-in data structures come into play — they help you store, organize, and manipulate data with ease.&lt;/p&gt;

&lt;p&gt;Let’s explore the most important ones, when to use each, and how they make your programs more powerful 💪&lt;/p&gt;




&lt;p&gt;🧠 What Are Data Structures?&lt;/p&gt;

&lt;p&gt;A data structure is a way of organizing and storing data so it can be accessed and modified efficiently.&lt;/p&gt;

&lt;p&gt;In Java, the Collections Framework and arrays provide ready-to-use data structures like lists, sets, and maps — all part of java.util.&lt;/p&gt;




&lt;p&gt;🧩 1. Arrays&lt;/p&gt;

&lt;p&gt;✅ Fixed size and fast access&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]); // Output: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
When the number of elements is known in advance, and you need fast random access.&lt;/p&gt;

&lt;p&gt;Limitation:&lt;br&gt;
Size cannot be changed once created.&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html" rel="noopener noreferrer"&gt;Java Arrays (Official Docs)&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;📜 2. ArrayList&lt;/p&gt;

&lt;p&gt;✅ Dynamic arrays with resizing&lt;br&gt;
✅ Maintains insertion order&lt;/p&gt;

&lt;p&gt;import java.util.ArrayList;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ArrayList&amp;lt;String&amp;gt; names = new ArrayList&amp;lt;&amp;gt;();
names.add("Ali");
names.add("Omar");
names.add("Sara");
System.out.println(names); // [Ali, Omar, Sara]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
When you need a resizable list that can grow or shrink dynamically.&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html" rel="noopener noreferrer"&gt;ArrayList Docs&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🔁 3. LinkedList&lt;/p&gt;

&lt;p&gt;✅ Doubly linked structure&lt;br&gt;
✅ Efficient for frequent insertions and deletions&lt;/p&gt;

&lt;p&gt;import java.util.LinkedList;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LinkedList&amp;lt;Integer&amp;gt; list = new LinkedList&amp;lt;&amp;gt;();
list.add(10);
list.add(20);
list.addFirst(5);
System.out.println(list); // [5, 10, 20]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
When you frequently insert or remove elements, especially in the middle of the list.&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html" rel="noopener noreferrer"&gt;LinkedList Docs&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🚦 4. HashSet&lt;/p&gt;

&lt;p&gt;✅ Unique elements only&lt;br&gt;
✅ Fast lookups (based on hashing)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.HashSet;

HashSet&amp;lt;String&amp;gt; set = new HashSet&amp;lt;&amp;gt;();
set.add("Java");
set.add("Python");
set.add("Java"); // duplicate ignored
System.out.println(set); // [Java, Python]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
When you need to store unique items and don’t care about order.&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html" rel="noopener noreferrer"&gt;HashSet Docs&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;🗺 5. HashMap&lt;/p&gt;

&lt;p&gt;✅ Stores key-value pairs&lt;br&gt;
✅ Fast retrieval by key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.HashMap;

HashMap&amp;lt;Integer, String&amp;gt; map = new HashMap&amp;lt;&amp;gt;();
map.put(1, "Ali");
map.put(2, "Omar");
System.out.println(map.get(1)); // Ali
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
When you need to associate keys with values — like a dictionary or lookup table.&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html" rel="noopener noreferrer"&gt;HashMap Docs&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;⏫ 6. Stack&lt;/p&gt;

&lt;p&gt;✅ Last In, First Out (LIFO) structure&lt;/p&gt;

&lt;p&gt;import java.util.Stack;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stack&amp;lt;String&amp;gt; stack = new Stack&amp;lt;&amp;gt;();
stack.push("First");
stack.push("Second");
System.out.println(stack.pop()); // Second
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
When you need to process elements in reverse order (e.g., undo operations, expression parsing).&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html" rel="noopener noreferrer"&gt;Stack Docs&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;⏬ 7. Queue&lt;/p&gt;

&lt;p&gt;✅ First In, First Out (FIFO) structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.LinkedList;
import java.util.Queue;

Queue&amp;lt;String&amp;gt; queue = new LinkedList&amp;lt;&amp;gt;();
queue.add("Task1");
queue.add("Task2");
System.out.println(queue.remove()); // Task1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When to use:&lt;br&gt;
When you need to process elements in the same order they were added (e.g., scheduling tasks, print jobs).&lt;/p&gt;

&lt;p&gt;📘 More: &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html" rel="noopener noreferrer"&gt;Queue Docs&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🧭 Choosing the Right Data Structure&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Goal&lt;/th&gt;
&lt;th&gt;Use This&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Fixed number of items&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Array&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dynamic list of elements&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ArrayList&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frequent insertions/removals&lt;/td&gt;
&lt;td&gt;&lt;code&gt;LinkedList&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unique elements&lt;/td&gt;
&lt;td&gt;&lt;code&gt;HashSet&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Key-value pairs&lt;/td&gt;
&lt;td&gt;&lt;code&gt;HashMap&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LIFO (Reverse order)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Stack&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FIFO (Normal order)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Queue&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;🧠 Final Thoughts&lt;/p&gt;

&lt;p&gt;Java’s built-in data structures are designed to make your life easier — you just need to know which one fits the job.&lt;/p&gt;

&lt;p&gt;Start small:&lt;/p&gt;

&lt;p&gt;Understand how each structure stores and accesses data.&lt;/p&gt;

&lt;p&gt;Practice choosing the right one for the right scenario.&lt;/p&gt;

&lt;p&gt;Then explore advanced ones like &lt;code&gt;TreeSet&lt;/code&gt;, &lt;code&gt;PriorityQueue&lt;/code&gt;, and `ConcurrentHashMap.&lt;/p&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;❓Question for You&lt;/p&gt;

&lt;p&gt;Which built-in data structure do you use most in your Java projects — and why?&lt;br&gt;
Share your answer in the comments 👇&lt;/p&gt;

</description>
      <category>programming</category>
      <category>datastructures</category>
      <category>java</category>
      <category>architecture</category>
    </item>
    <item>
      <title>🧩 Understanding the Object Class in Java</title>
      <dc:creator>Mohammed mhanna</dc:creator>
      <pubDate>Mon, 20 Oct 2025 11:34:41 +0000</pubDate>
      <link>https://forem.com/mohamad_mhana/understanding-the-object-class-in-java-37ki</link>
      <guid>https://forem.com/mohamad_mhana/understanding-the-object-class-in-java-37ki</guid>
      <description>&lt;p&gt;If you’ve ever wondered why every class in Java seems to have methods like &lt;code&gt;toString()&lt;/code&gt; or &lt;code&gt;equals()&lt;/code&gt;, the answer lies in the Object class — the ultimate parent of all classes in Java.&lt;/p&gt;

&lt;p&gt;Let’s dive deep into what makes the Object class so fundamental and how to use it effectively.&lt;/p&gt;




&lt;p&gt;🏛 What is the Object Class?&lt;/p&gt;

&lt;p&gt;In Java, every class you create implicitly inherits from the Object class — even if you don’t explicitly say so.&lt;/p&gt;

&lt;p&gt;That means this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyClass {
    // extends Object implicitly
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is the same as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyClass extends Object {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, every class you create automatically gains the core methods defined in Object.&lt;/p&gt;




&lt;p&gt;🧰 Common Methods from Object&lt;/p&gt;

&lt;p&gt;Here are the most commonly used methods that every Java class inherits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;toString()        // Returns a string representation of the object
equals(Object o)  // Compares two objects for equality
hashCode()        // Returns an integer hash code for the object
getClass()        // Returns the runtime class of the object
clone()           // Creates and returns a copy of the object (if supported)
finalize()        // Called before an object is garbage collected (deprecated)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These methods form the foundation of many features in Java — like collections, object comparison, and debugging.&lt;/p&gt;




&lt;p&gt;🔍 Example: Using Object Methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student {
    private String name;
    private int id;

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "Student{name='" + name + "', id=" + id + "}";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Student)) return false;
        Student other = (Student) obj;
        return this.id == other.id &amp;amp;&amp;amp; this.name.equals(other.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Ali", 1);
        Student s2 = new Student("Ali", 1);

        System.out.println(s1);                   // calls toString()
        System.out.println(s1.equals(s2));        // true, because we overrode equals()
        System.out.println(s1.hashCode());        // inherited from Object (can override)
        System.out.println(s1.getClass().getName()); // Student
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 Why the Object Class Exists&lt;/p&gt;

&lt;p&gt;The Object class provides a common type for all Java objects.&lt;/p&gt;

&lt;p&gt;That’s why you can do things like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Object obj = new Student("Ali", 1);&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or store different types in a single collection (before generics were introduced):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Object&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();
list.add("Hello");
list.add(42);
list.add(new Student("Ali", 1));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s what makes Java’s type system unified and consistent.&lt;/p&gt;




&lt;p&gt;🧩 What Makes It Special&lt;/p&gt;

&lt;p&gt;✅ It’s the root of the entire class hierarchy.&lt;/p&gt;

&lt;p&gt;✅ It gives you default behavior for essential methods.&lt;/p&gt;

&lt;p&gt;✅ It allows polymorphism and generic programming.&lt;/p&gt;

&lt;p&gt;✅ It ensures every object in Java can be treated uniformly.&lt;/p&gt;




&lt;p&gt;⚠️ A Note About &lt;code&gt;finalize()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;finalize()&lt;/code&gt; method in Object was once used to clean up resources before an object is destroyed.&lt;/p&gt;

&lt;p&gt;However, it’s now deprecated — unreliable and unnecessary.&lt;br&gt;
👉 Use try-with-resources or explicit cleanup methods instead.&lt;/p&gt;

&lt;p&gt;Learn more here: &lt;a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#finalize()" rel="noopener noreferrer"&gt;Oracle Docs on Finalize (deprecated)&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;💬 Final Thoughts&lt;/p&gt;

&lt;p&gt;The Object class is like the invisible backbone of Java — always there, supporting everything you build.&lt;br&gt;
Understanding it helps you grasp how inheritance, equality, and type systems truly work under the hood.&lt;/p&gt;




&lt;p&gt;❓Question for You&lt;/p&gt;

&lt;p&gt;How often do you override methods from the Object class — like &lt;code&gt;equals()&lt;/code&gt; and &lt;code&gt;toString()&lt;/code&gt; — in your own projects?&lt;br&gt;
Share your experience in the comments 👇&lt;/p&gt;

</description>
      <category>oop</category>
      <category>java</category>
      <category>coding</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
