Day 56 of Sharing What I’ve Learned🚀 TreeSet in Java — Sorted & Unique Elements After exploring how LinkedHashSet maintains insertion order, I moved to something even more powerful — TreeSet. 👉 It not only stores unique elements but also keeps them automatically sorted 🔹 What is TreeSet? TreeSet is an implementation of the Set interface that stores unique elements in sorted order. 👉 It is internally based on a Red-Black Tree (self-balancing binary search tree) 🔹 How does TreeSet store & sort data? 👉 Elements are stored in a tree structure, not randomly 👉 While inserting, elements are placed in a way that keeps the tree balanced 👉 Data is always arranged in ascending order (default) ✔ Smallest element → left side ✔ Largest element → right side ✔ In-order traversal → gives sorted output 🔹 Why use TreeSet? ✔ Sorted Data Elements are always in ascending order ✔ No Duplicates Just like other Sets, duplicates are not allowed ✔ Navigable Operations Supports methods like higher(), lower(), ceiling(), floor() 🔹 Key Features ✔ Stores unique elements ✔ Automatically sorts elements ✔ Does NOT allow null values ✔ Slower than HashSet & LinkedHashSet (due to sorting) 🔹 Important Methods ✔ add() ✔ remove() ✔ contains() ✔ first() / last() ✔ higher() / lower() 🔹 When should we use TreeSet? 👉 Use TreeSet when: ✔ You need sorted data ✔ You want range-based operations ✔ You need navigation (greater/smaller elements) 🔹 When NOT to use? ❌ When order doesn’t matter → use HashSet ❌ When insertion order matters → use LinkedHashSet ❌ When performance is critical (faster ops needed) 🔹 Key Insight 💡 TreeSet is like a self-sorting set — 👉 You don’t sort the data, it sorts itself automatically 🔹 Day 56 Realization 🎯 Data structures are not just about storing data… 👉 they define how efficiently you can retrieve, organize, and use it #Java #TreeSet #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day56 Grateful for guidance from, Sharath R TAP Academy
Java TreeSet: Sorted & Unique Elements
More Relevant Posts
-
Day 64 of Sharing What I’ve Learned 🚀 Understanding Multithreading — From Programs to Threads After exploring how Java collections help manage and traverse data efficiently, I started looking into something equally important — how programs actually execute and handle multiple tasks. That’s where multithreading comes in. Before diving deeper into threads, I decided to revisit the fundamentals and build a clearer understanding first: 🔹Program A program is simply a set of instructions written in a language like Java. It’s what we write — but not yet running. 🔹Process When we run a program, it becomes a process. A process is an active instance of a program with its own memory and resources. 🔹Thread A thread is an independent set of code or the smallest unit of execution inside a process. Each process can have multiple threads working simultaneously. 🔹Multithreading Multithreading means running multiple threads within a single process. Instead of doing tasks one by one, we can perform multiple operations at the same time. 🔹Why it matters In real-world applications, tasks often need to happen together: 👉 Handling multiple users in an application 👉 Performing background tasks 👉 Improving performance and responsiveness Without multithreading, everything would run sequentially — slower and less efficient. 🔹Simple perspective Program → What we write Process → What runs Thread → What actually executes 🔹My realization Until now, I focused on how data is stored and traversed. Now it’s time to understand how execution can be optimized and made more efficient. 👉 Collections manage data 👉 Threads manage execution And both together are what make applications powerful. This is just the beginning — next, I’ll dive into how to create and manage threads in Java. #Java #Multithreading #Programming #JavaDeveloper #100DaysOfCode #DeveloperJourney #Day64 Grateful for guidance from TAP Academy Sharath R kshitij kenganavar
To view or add a comment, sign in
-
-
Day 52 of Sharing What I’ve Learned 🚀 LinkedList in Java — Advantages & Disadvantages After exploring how LinkedList powers structures like Queue, I took a step back to understand something important — 👉 When should we actually use LinkedList, and when should we avoid it? 🔹 Advantages of LinkedList ✔ Dynamic Size No need to define size in advance — it grows and shrinks as needed. ✔ Efficient Insertions & Deletions Adding/removing elements is fast, especially at the beginning or middle. (No shifting like arrays!) ✔ Memory Utilization (Flexible Allocation) Memory is allocated as needed, not wasted upfront. ✔ Implements Multiple Structures Can be used as: ✔ List ✔ Queue ✔ Deque 🔹 Disadvantages of LinkedList ❌ More Memory Usage Each node stores extra references (pointers), increasing memory overhead. ❌ Slow Access (No Indexing) Unlike ArrayList, you can’t directly access elements — traversal is required. ❌ Poor Cache Performance Elements are not stored contiguously → slower compared to arrays. ❌ Not Ideal for Searching Searching is O(n), making it inefficient for large datasets. 🔹 LinkedList vs ArrayList (Quick Insight) 👉 Use LinkedList when: ✔ Frequent insertions/deletions ✔ Working with Queue/Deque 👉 Use ArrayList when: ✔ Fast access is needed ✔ More read operations than write 🔹 Key Insight 💡 Every data structure has trade-offs — 👉 The real skill is knowing when to use which one. 🔹 Day 52 Realization 🎯 Understanding limitations is just as important as learning features — That’s what makes you a better problem solver. #Java #LinkedList #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day52 Grateful for guidance from, Sharath R TAP Academy
To view or add a comment, sign in
-
-
Day 58 of Sharing What I’ve Learned 🚀 Map in Java — Storing Data as Key-Value Pairs After learning how PriorityQueue processes elements based on priority, I explored another powerful concept — Map. 👉 It doesn’t store just values… it connects keys with values 🔹 What is a Map? Map is a part of the Java Collections Framework that stores data in key-value pairs. 👉 Each key is unique and maps to a value 🔹 How does Map work? 👉 Data is stored as (key → value) 👉 Keys must be unique 👉 Values can be duplicated ✔ put() → adds key-value pair ✔ get() → retrieves value using key ✔ remove() → deletes entry ✔ containsKey() → checks key existence 🔹 Types of Map ✔ HashMap → Fast, unordered ✔ LinkedHashMap → Maintains insertion order ✔ TreeMap → Sorted by keys 🔹 Why use Map? ✔ Fast lookup using keys ✔ Efficient data organization ✔ Useful for real-world mappings 🔹 Real-World Use Cases 👉 Storing student data (ID → Name) 👉 Caching systems 👉 Frequency counting 👉 Database indexing 🔹 Key Features ✔ No duplicate keys ✔ Allows one null key (HashMap) ✔ Not synchronized (HashMap) ✔ Faster retrieval compared to lists 🔹 When should we use Map? 👉 Use it when: ✔ You need fast lookup by key ✔ You want structured data (pair format) ✔ You need efficient searching 🔹 When NOT to use? ❌ When you only need a list of values ❌ When order matters strictly (use LinkedHashMap/TreeMap carefully) 🔹 Key Insight 💡 Map is not about storing data… 👉 It’s about connecting data 🔹 Day 58 Realization 🎯 Efficient programs don’t just store values… 👉 They organize relationships between them #Java #Map #HashMap #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day58 Grateful for guidance from, Sharath R TAP Academy kshitij kenganavar
To view or add a comment, sign in
-
-
Day 62 of Sharing What I’ve Learned🚀 Iterator in Java — Safe and Efficient Traversal After understanding how collections store and organize data, I revisited an important concept — how to safely traverse them using Iterator. 👉 Accessing data is easy… but doing it correctly and safely matters more. 🔹 What is an Iterator? Iterator is an interface in Java used to traverse elements of a collection one by one. 👉 It provides a standard way to loop through: ArrayList HashSet LinkedList And more… 🔹 Why not just use a for loop? Using a normal loop works… but it has limitations: ❌ Not safe when modifying collection ❌ Can lead to ConcurrentModificationException ❌ Not universal for all collection types 👉 That’s where Iterator comes in ✔ 🔹 Key Methods of Iterator hasNext() → checks if next element exists next() → returns the next element remove() → removes the current element safely 🔹 Example import java.util.*; public class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("C++"); Iterator<String> it = list.iterator(); while(it.hasNext()) { String lang = it.next(); System.out.println(lang); } } } 🔹 Real Advantage 💡 👉 Removing elements while iterating: Iterator<String> it = list.iterator(); while(it.hasNext()) { if(it.next().equals("Python")) { it.remove(); // Safe removal } } ✔ No errors ✔ Clean logic ✔ Interview-friendly concept 🔹 Day 62 Realization Traversing data is not just about loops — it’s about doing it safely and efficiently. 👉 Iterator provides better control and prevents runtime issues 👉 Essential when working with dynamic collections #Java #Collections #DataStructures #CollectionsFramework #Iterator #Programming #DeveloperJourney #100DaysOfCode #Day61 Grateful for guidance from, TAP Academy Sharath R kshitij kenganavar
To view or add a comment, sign in
-
-
Day 73 of #90DaysDSAChallenge Solved LeetCode 451: Sort Characters By Frequency Learned an important Java design concept today. Problem Overview: The task was to sort characters in a string based on descending frequency. What confused me initially: Why create a separate Freq class instead of just using HashMap and PriorityQueue directly? Key Learning: PriorityQueue stores one complete object at a time. For this problem, each item needs two pieces of data together: Character Frequency Example: Instead of storing: e and 2 separately We package them as: Freq('e', 2) That custom class acts like a container holding both values in one object, so PriorityQueue can compare and sort them correctly. Why this matters: This taught me that custom classes in Java are often not about complexity, they simply bundle related data into one manageable unit. Alternative approach: We can also use Map.Entry<Character, Integer> instead of creating a custom class, but building Freq makes the logic easier to understand while learning. Today’s takeaway: Not every class is for business logic — sometimes it exists just to package data cleanly. #Java #90DaysDSAChallenge #LeetCode #PriorityQueue #HashMap #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 ArrayList Deep Dive — Beyond the Basics. (https://lnkd.in/gUuBbkwb) ArrayList, as part of Java’s Collections Framework, represents a resizable array that bridges the gap between fixed-size arrays and dynamic data structures. It allows elements to grow and shrink automatically, providing fast index-based access while handling memory management internally. Here are the key takeaways from the ArrayList session at TAP Academy by Sharath R. Sir: → Why collections can’t store primitives, and how AutoBoxing handles it → Why for-each loops are preferred in real-world scenarios → Iterator vs ListIterator — forward vs bidirectional traversal → Why add() in the middle is O(n), but get() is O(1) → The key difference between retainAll() and removeAll() One interesting takeaway: there’s no direct way to check an ArrayList’s capacity—you rely on internal behavior and documentation. Reinforces how important it is to understand internals, not just APIs. To know everything in detail I Built a small interactive study guide with quizzes and code snippets to reinforce the concepts. visit site: https://lnkd.in/gUuBbkwb #Java #DSA #Collections #LearningInPublic #SoftwareEngineering #TAPTAP Academy #ArrayList
To view or add a comment, sign in
-
-
🚀 Day 56 & 57 – Mastering Maps in Java | Tap Academy Diving deeper into the Java Collections Framework, I explored one of the most powerful concepts — Maps, especially HashMap, LinkedHashMap, and TreeMap. 🔹 Day 56 Highlights – HashMap Understood how Map stores data in key–value pairs Learned internal working: Hashing (Hash Table + Hash Function) Explored default capacity (16) and load factor (75%) Practiced key features: ✔ No duplicate keys ✔ Allows null values ✔ Heterogeneous data support ✔ Fast operations → O(1) 💻 Implemented important methods: put(), get(), containsKey(), containsValue(), entrySet(), keySet(), values() 🔁 Learned how to iterate using: entrySet() (Map → Set conversion) Iterator (cursor-based access) 🔹 Day 57 Highlights – LinkedHashMap & TreeMap 📌 LinkedHashMap Maintains insertion order Same features as HashMap but ordered output 📌 TreeMap Stores data in sorted order (ascending by keys) Uses Tree structure (Red-Black Tree) No null keys allowed Slightly slower → O(log n) 📊 Comparison Insight HashMap → Fastest, no order ⚡ LinkedHashMap → Maintains insertion order 📌 TreeMap → Sorted order 🔄 🎯 Key takeaway: Choosing the right data structure matters based on requirement — speed, order, or sorting. 🎯 This journey is strengthening my problem-solving skills and deepening my understanding of how real-world data is handled efficiently. #Java #CollectionsFramework #HashMap #LinkedHashMap #TreeMap #DataStructures #FullStackJava #TapAcademy #LearningJourney 🚀
To view or add a comment, sign in
-
-
Day 59 of Sharing What I’ve Learned 🚀 Comparator in Java — Custom Sorting Made Easy After learning how Map stores key-value pairs, I explored something powerful — controlling how data is sorted using Comparator. 👉 Default sorting is useful… but real-world problems need custom logic 🔹 What is Comparator? Comparator is an interface in Java used to define custom sorting logic. 👉 It allows us to sort objects based on our own rules 🔹 How does Comparator work? 👉 We override compare(a, b) method 👉 Returns: ✔ Negative → a comes before b ✔ Zero → both are equal ✔ Positive → a comes after b 🔹 Why use Comparator? ✔ Custom sorting logic ✔ Multiple sorting options ✔ Works without modifying the original class 🔹 Real-World Use Cases 👉 Sorting students by marks 👉 Sorting employees by salary 👉 Sorting products by price or rating 🔹 Key Features ✔ External sorting logic (separate from class) ✔ Can create multiple comparators ✔ Works with Collections.sort() & Arrays.sort() 🔹 Comparator vs Comparable 👉 Comparable = default/internal sorting 👉 Comparator = custom/external sorting 🔹 When should we use Comparator? 👉 Use it when: ✔ You need multiple ways to sort data ✔ You don’t want to modify the class ✔ You need dynamic sorting logic 🔹 When NOT to use? ❌ When only one natural sorting is enough ❌ When simple primitive sorting is required 🔹 Key Insight 💡 Sorting is not just arranging data… 👉 It’s about organizing data based on needs 🔹 Day 59 Realization 🎯 Good developers don’t just sort data… 👉 They decide how it should be sorted #Java #Comparator #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day59 Grateful for guidance from, TAP Academy Sharath R kshitij kenganavar
To view or add a comment, sign in
-
-
We are so back 😁 Day 12 of #100DaysOfCode Two weeks of exams slowed me down but today I finally got to do what I have been itching to do the whole time. We are picking up exactly where I left off. Today was a big one, a lot of revision was needed. After my revision, I had to go deeper into these topics: 1. ArrayList, I like to think of these as arrays that we can add or shrink as we add or remove items. Also the wrapper classes, they contain are full objects which allows us to do so much cool stuff like the "add" method which is similar to "append" in python and many others. 2. Generics, when we use generics we are kind off telling the ArrayList to only store a certain type of object. 3. Exceptions, this explains how java handles errors and my role as a developer in that process. I also love how Chris (BroCode) teaches this topic in particular. He continually emphasizes on the importance of letting users understand what exactly went wrong when a program crashes. Now File handling, writing files: ✅ FileWriter, this is perfect for writing small text files. ✅ BufferedWriter, this is much faster and perfect for larger sized text files. ✅ PrintWriter, I learnt this is best for structured data. ✅ FileOutputStream, this is also good for binary files. Reading files was a bit different as I had to combine BufferedReader and FilerReader. The more code I wrote, the more exceptions popped up, and the more knowledge I have gained. Today felt amazing. This is exactly where I want to be. Day 12 done. I will upload the pictures soon. Still studying! #100DaysOfCode #Java
To view or add a comment, sign in
-
🚀 TreeSet in Java Collections Continuing my journey in Set-based collections, I explored TreeSet, which introduces sorting + uniqueness in data storage. 🔹 What is TreeSet? TreeSet is a class in Java Collections Framework that stores unique elements in sorted order By default, it follows natural ordering (ascending order) 🔹 Key Properties ✅ Maintains sorted order (ascending by default) ❌ Does not allow duplicates ❌ Does not allow null values ⚠️ Heterogeneous data allowed only if Comparator is provided Implements SortedSet & NavigableSet 🔹 Internal Working Uses Balanced Binary Search Tree (Red-Black Tree) Automatically keeps elements sorted 👉 Sorting is based on: Comparable (natural sorting) Comparator (custom sorting) 🔹 Constructors TreeSet() TreeSet(Comparator c) TreeSet(Collection c) TreeSet(SortedSet s) 🔹 Important Methods add(E e) remove(Object o) contains(Object o) first() → smallest element last() → largest element headSet(E e) → elements < e tailSet(E e) → elements ≥ e subSet(from, to) → range ceiling(E e) → smallest ≥ e size() 🔹 Traversal (Accessing Elements) For-each loop Iterator Stream API ❌ No indexing ❌ No ListIterator 🔹 Performance Add / Remove / Search → O(log n) Slower than HashSet (because of sorting) 🔹 TreeSet vs HashSet vs LinkedHashSet HashSet → No order LinkedHashSet → Insertion order TreeSet → Sorted order 👉 TreeSet is best when: Sorted data is required Range-based operations are needed 🔹 When to Use TreeSet? When sorted output is required When working with range queries When needing ordered traversal automatically 🔹 Learning Outcome Strong understanding of sorted collections Clear difference between HashSet vs LinkedHashSet vs TreeSet Knowledge of Comparable vs Comparator Ability to choose correct Set implementation 🙌 Special thanks to the amazing trainers at TAP Academy: kshitij kenganavar Sharath R MD SADIQUE Bibek Singh Vamsi yadav Hemanth Reddy Harshit T Ravi Magadum Somanna M G Rohit Ravinder TAP Academy Grateful to Tap Academy for strengthening my Java and data structure foundations 🚀 #TapAcademy #Week13Learning #CoreJava #CollectionsFramework #TreeSet #HashSet #LinkedHashSet #DataStructures #JavaFundamentals #LearningByDoing #FullStackJourney #VamsiLearns
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development