Day 60 of Sharing What I’ve Learned 🚀 Comparable in Java — Defining Natural Sorting After learning how Comparator gives us custom sorting flexibility, I explored the foundation of sorting in Java — Comparable. 👉 Before customizing sorting… we should understand the default behavior 🔹 What is Comparable? Comparable is an interface in Java used to define natural (default) sorting of objects. 👉 It is implemented inside the class itself 🔹 How does Comparable work? 👉 We override compareTo() method 👉 Returns: ✔ Negative → current object comes before another ✔ Zero → both are equal ✔ Positive → current object comes after another 🔹 Why use Comparable? ✔ Defines default sorting behavior ✔ Simple and clean for single sorting logic ✔ Automatically used by sorting methods 🔹 Real-World Use Cases 👉 Sorting students by ID 👉 Sorting employees by name 👉 Sorting products by default price 🔹 Key Features ✔ Internal sorting logic (inside class) ✔ Only one sorting logic per class ✔ Works with Collections.sort() & Arrays.sort() 🔹 Comparable vs Comparator 👉 Comparable = internal / natural sorting 👉 Comparator = external / custom sorting 🔹 When should we use Comparable? 👉 Use it when: ✔ You need a single default sorting ✔ Sorting logic is fixed ✔ Natural ordering makes sense 🔹 When NOT to use? ❌ When multiple sorting logics are needed ❌ When sorting logic may change dynamically 🔹 Key Insight 💡 Before customizing everything… 👉 Always define a strong default behavior 🔹 Day 60 Realization 🎯 Great developers don’t just customize sorting… 👉 They design a meaningful default order first #Java #Comparable #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day60 Grateful for guidance from, TAP Academy Sharath R kshitij kenganavar
Java Comparable Interface for Natural Sorting
More Relevant Posts
-
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
-
-
Day 61 of Sharing What I’ve Learned🚀 Collections Utility Class in Java — Powerful Helpers for Data Manipulation After learning how sorting works using Comparable and Comparator, I explored something that makes working with collections even easier — the Collections utility class. 👉 Instead of writing logic from scratch… Java already gives us ready-made tools 🔹 What is Collections class? Collections is a utility class in Java that provides static methods to operate on collection objects. 👉 It works with List, Set, and more 👉 No need to create objects — just use methods directly 🔹 Commonly Used Methods ✔ sort() → Sorts elements ✔ reverse() → Reverses order ✔ shuffle() → Randomly shuffles elements ✔ min() / max() → Finds smallest & largest ✔ frequency() → Counts occurrences ✔ binarySearch() → Searches efficiently (on sorted list) 🔹 Why use Collections? ✔ Saves time (no need to write logic manually) ✔ Improves readability ✔ Optimized and efficient ✔ Reduces bugs 🔹 Real-World Use Cases 👉 Sorting student records 👉 Finding highest salary 👉 Randomizing quiz questions 👉 Searching data quickly 👉 Counting duplicates 🔹 Key Features ✔ Works with existing collections ✔ Provides static utility methods ✔ Supports Comparable & Comparator ✔ Part of Java Collections Framework 🔹 Collections vs Collection 👉 Collection = Interface (data structure) 👉 Collections = Utility class (helper methods) 🔹 When should we use it? 👉 Use when: ✔ You want ready-made operations ✔ You need optimized algorithms ✔ You want cleaner code 🔹 Day 61 Insight 💡 Don’t reinvent the wheel… 👉 Java already gives powerful tools — learn to use them effectively 🔹 Day 61 Realization 🎯 Writing less code doesn’t mean doing less work… 👉 It means using smarter tools #Java #Collections #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day61 Grateful for guidance from, TAP Academy Sharath R kshitij kenganavar
To view or add a comment, sign in
-
-
Day 55 of Sharing What I’ve Learned 🚀 LinkedHashSet in Java — Order + Uniqueness Combined After learning how HashSet ensures uniqueness, I explored something even more practical — LinkedHashSet. 👉 It gives the best of both worlds: unique elements + predictable order 🔹 What is LinkedHashSet? LinkedHashSet is an implementation of the Set interface that maintains insertion order while storing unique elements. 👉 It is built on top of HashSet with a linked list to preserve order. 🔹 Why use LinkedHashSet? ✔ Maintains Order Elements are stored in the order they were inserted. ✔ No Duplicates Just like HashSet, duplicates are not allowed. ✔ Predictable Iteration Traversal happens in insertion order. 🔹 Key Features ✔ Stores unique elements ✔ Maintains insertion order ✔ Allows one null value ✔ Slightly slower than HashSet (due to ordering) 🔹 Important Methods ✔ add() ✔ remove() ✔ contains() ✔ size() ✔ isEmpty() 🔹 When should we use LinkedHashSet? 👉 Use LinkedHashSet when: ✔ You want unique elements ✔ You also need insertion order ✔ You want predictable iteration 🔹 When NOT to use? ❌ When order doesn’t matter → use HashSet ❌ When you need sorted order → use TreeSet 🔹 Key Insight 💡 LinkedHashSet is like HashSet with memory — 👉 it remembers the order in which elements were added. 🔹 Day 55 Realization 🎯 Sometimes small improvements (like maintaining order) 👉 can make a data structure much more useful in real-world applications. #Java #LinkedHashSet #DataStructures #CollectionsFramework #Programming #DeveloperJourney #100DaysOfCode #Day55 Grateful for guidance from, Sharath R TAP Academy
To view or add a comment, sign in
-
-
Day 38 – 44 of my Frontlines EduTech (FLM) AI-Powered Java Full Stack Journey Day 38: Started learning Collections in Java. Focused on ArrayList and how it stores dynamic data. Understood how it is different from arrays. Day 39: Learned about Iterator. Used it to traverse elements in collections. It made looping through data more clean and flexible. Day 40: Explored Set interface. Learned that it stores only unique elements. Good for removing duplicates from data. Day 41: Learned Map in Java. Stores data in key-value pairs. Very useful for real-world applications. Day 42: Covered Enum and Command Line Arguments. Also learned Static and Instance Blocks. Understood when and how they are executed. Day 43: Learned Clone, Comparator, and Comparable. Used for copying objects and sorting data. Important for customizing sorting logic. Day 44: Solved problem on frequency of characters. Used logic with collections to count occurrences. Good practice for improving problem-solving skills. Consistent learning, step by step. Fayaz S 🔗 Github: https://lnkd.in/gV_uis3J #Java #Collections #CodingJourney #100DaysOfCode #LearnJava #FullStack 🚀
To view or add a comment, sign in
-
-
🚀 Day 7 – Practicing Java Patterns & Logic Building Today’s learning was very interesting because I focused on improving my logic-building skills using Java. I worked on different problems like checking whether a number is prime or not, and printing various patterns using loops. First, I learned how to check if a number is prime. A prime number is a number that is divisible only by 1 and itself. I used a loop to check divisibility and understood how important optimization is by using Math.sqrt(n) instead of checking all numbers. This helped me write better and efficient code. Next, I practiced star patterns using nested loops. At first, it looked confusing, but once I understood how the outer loop controls rows and the inner loop controls columns, it became easier. I learned how to print increasing and decreasing star patterns step by step. Then, I worked on a half-pyramid number pattern, where numbers increase in each row. This helped me understand how loops and conditions work together to create structured output. After that, I practiced a character pattern, where alphabets like A, B, C are printed in a structured way. It was interesting to see how characters can also be handled like numbers in Java. Finally, I also learned about using the continue statement, which helps skip certain iterations in a loop. This is useful when we want to ignore specific conditions. Overall, today’s practice helped me improve my understanding of loops, conditions, and pattern-based problems. These concepts are very important for coding interviews and problem-solving. 💪 I will keep practicing daily and improve step by step in my coding journey. #Java #Coding #DSA #LearningJourney #Consistency #ApnaCollege
To view or add a comment, sign in
-
-
🚀 Day 47 of My Learning Journey – Java Wrapper Classes Today, I explored Wrapper Classes in Java, an important concept that bridges primitive data types and objects. 🔍 What I Learned: ✔️ Wrapper classes convert primitive data types into objects ✔️ Each primitive type has a corresponding wrapper class (e.g., int → Integer, char → Character, double → Double) ✔️ Enables use of primitives in collections like ArrayList 💡 Key Concepts: 🔹 Autoboxing – Automatic conversion of primitive to object 🔹 Unboxing – Converting object back to primitive 🔹 Useful methods like parseInt(), toString(), and valueOf() 📊 Why It Matters: Wrapper classes make Java more flexible by allowing primitives to be treated as objects, especially when working with frameworks and collections. 📌 Example Use Case: Storing integers in an ArrayList using Integer instead of int. 🎯 Takeaway: Understanding wrapper classes is essential for writing efficient and modern Java programs. #Java #LearningJourney #Programming #Day47 #TechSkills #Developers #Coding
To view or add a comment, sign in
-
-
Day 41 of Learning Java: Method Overriding If method overloading was about flexibility,method overriding is about customization. What is Method Overriding? It’s when a subclass provides its own implementation of a method that is already defined in the parent class. Same method name. Same parameters. But different behavior. 🔹 Simple example- class Parent { void watchTV() { System.out.println("Watching News/Serial"); } } class Child extends Parent { @Override void watchTV() { System.out.println("Watching Music/Sports"); } } Same method → different output depending on the object. • Parent defines a general behavior • Child modifies it based on its own need • This helps in writing more flexible and reusable code 🔹 Key points to remember • Method signature must be the same • Happens during runtime (runtime polymorphism) • Inheritance is required 👉 You cannot override: static methods private methods final methods 🔹 One important concept Parent ref = new Child(); ref.watchTV(); Even though the reference is of Parent, the method of Child gets executed. 👉 This is called dynamic method dispatch 🔹 About @Override It’s not mandatory, but it helps: ✔ Avoid mistakes ✔ Makes code more readable ✔ Ensures you’re actually overriding #Java #OOP #MethodOverriding #LearningInPublic #Programming#sql #branding
To view or add a comment, sign in
-
-
🚀 Day-49 @ Tap Academy | Mastering ArrayDeque & TreeSet in Java Today’s learning was all about understanding two powerful components of the Java Collection Framework: ArrayDeque and TreeSet — both designed to solve different real-world problems efficiently. 🔹 ArrayDeque (Double-Ended Queue) ArrayDeque is a resizable array implementation of the Deque interface, which allows insertion and deletion from both ends. 👉 Key Features: Faster than Stack and LinkedList for queue operations No capacity restrictions (dynamic resizing) Does not allow null elements Can be used as both Stack (LIFO) and Queue (FIFO) 👉 Common Methods: addFirst(), addLast() removeFirst(), removeLast() peekFirst(), peekLast() 👉 Use Case: Efficient for scenarios like task scheduling, undo operations, or sliding window problems. 🔹 TreeSet (Sorted Set Implementation) TreeSet is a part of the SortedSet interface and is backed by a Red-Black Tree. 👉 Key Features: Stores unique elements only Maintains natural sorting order (ascending by default) Does not allow null elements Provides log(n) time complexity for basic operations 👉 Common Methods: add(), remove() first(), last() higher(), lower() 👉 Use Case: Ideal when you need sorted data without duplicates, like ranking systems or leaderboards. 💡 Key Difference: ArrayDeque → Focuses on fast insertion/removal from both ends TreeSet → Focuses on sorted, unique data storage ✨ Learning these concepts strengthens my understanding of how to choose the right data structure for optimized performance. 📌 What’s your go-to collection in Java for performance-critical applications? #Day49 #JavaLearning #TapAcademy #DataStructures #JavaCollections #CodingJourney #SoftwareDevelopment #LearningInPublic #Developers #Programming #TechCareers
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
-
-
🚀 Exploring Method Overloading in Java As part of my journey in mastering Object-Oriented Programming in Java, I recently explored one of the most powerful concepts of Polymorphism — Method Overloading. 💡 What is Method Overloading? Method overloading is the process of creating multiple methods with the same name in a class, but with different parameter lists. It allows the same action to behave differently based on the input — making programs more flexible and readable. 🔹 Three Ways to Achieve Method Overloading A method can be overloaded by changing: 1️⃣ Number of parameters 2️⃣ Data types of parameters 3️⃣ Order/sequence of parameters ❌ Invalid Case If two methods have the same name + same parameters but different return types, it is NOT valid overloading and results in a compile-time error. Example: int area(int, int) float area(int, int) → Compilation Error 🚫 🧠 Why is it called False (Virtual) Polymorphism? To the user, it looks like one method performing multiple tasks (one-to-many). But internally, each call maps to a separate method (one-to-one) — hence the term False Polymorphism. ⚡ Type Promotion in Overloading If an exact match is not found, Java automatically promotes smaller data types to larger ones: byte → short → int → long → float → double This makes method overloading even more powerful and flexible! 👩💻 Simple Example class AreaCalculator { int area(int l, int b) { return l * b; } double area(double r) { return 3.14 * r * r; } int area(int side) { return side * side; } } TAP Academy ✨ Learning these core OOP concepts is helping me build stronger foundations in Java and improve my problem-solving skills step by step. #Java #OOP #Programming #CodingJourney #ComputerScience #LearningInPublic
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