🚀 Day 29 | Core Java Learning Journey 📌 Topic: TreeSet in Java Today, I learned about TreeSet, an important class in the Java Collections Framework used when we need sorted and unique elements. 🔹 TreeSet in Java ✔ TreeSet is a class that implements NavigableSet ✔ It also indirectly implements SortedSet and Set ✔ Introduced in JDK 1.2 ✔ Stores unique elements only (no duplicates allowed) 🔹 Data Structure Used ✔ Based on Self-Balancing Binary Search Tree (Red-Black Tree) ❗ (important correction) ✔ Elements are stored in sorted order 🔹 Key Features ✔ Does NOT follow insertion order ✔ Follows natural sorting order (default) ✔ Allows custom sorting using Comparator ✔ Does NOT allow null elements ❌ ✔ Stores homogeneous data (same type, for proper comparison) 📌 Important Methods • add() – add element • remove() – delete element • contains() – check element • first() – returns first (smallest) element • last() – returns last (largest) element • higher() – next greater element • lower() – next smaller element 📌 Performance ✔ Operations like add, remove, search → O(log n) 📌 When to Use TreeSet? ✔ When you need: ✅ Sorted data ✅ Unique elements ✅ Range-based operations 💡 Note: Unlike HashSet, TreeSet focuses on sorting rather than speed. 🙏 Special thanks to Vaibhav Barde Sir for the guidance! 🔥 #CoreJava #JavaLearning #JavaDeveloper #TreeSet #SortedSet #NavigableSet #JavaCollections #Programming #LearningJourney
Java TreeSet: Sorted and Unique Elements with NavigableSet
More Relevant Posts
-
Day 40 of Learning Java: Method Overloading Instead of creating different method names for similar tasks, we can use the same method name but change the parameters — and Java figures out which one to call. -So what exactly is Method Overloading? It’s when multiple methods in the same class have: ✔ Same name ✔ Different parameter list That’s it. Simple idea, but very powerful. -Ways to overload a method • Change the type of parameters • Change the number of parameters • Change the order of parameters Example- Think of a login system: Login using username + password Login using mobile + password Both are login actions, right? So instead of writing different method names, we just overload: login(String username, String password) login(long mobile, String password) Same method name → different ways to use it -Another relatable one Payment systems 👇 COD UPI Card Net Banking Instead of: paymentByUPI(), paymentByCard()… We can just do: payment() payment(String upi) payment(long card) payment(String user, String pass) - Important things I learned • Just changing return type won’t work (it gives error) • Overloading happens at compile time • Works with static, private, and even final methods • Yes, even main() can be overloaded (but JVM only runs the standard one) #Java #LearningInPublic #100DaysOfCode #Programming #OOP #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 30 | Core Java Learning Journey 📌 Topic: Map Hierarchy in Java Today, I explored the Map Hierarchy in Java Collections Framework — understanding how different Map interfaces and classes are structured and related. 🔹 What is Map in Java? ✔ Map is an interface that stores key-value pairs ✔ Each key is unique and maps to a specific value ✔ It is part of java.util package 🔹 Map Hierarchy (Understanding Structure) ✔ Map (Root Interface) ⬇ ✔ SortedMap (extends Map) ⬇ ✔ NavigableMap (extends SortedMap) ⬇ ✔ TreeMap (implements NavigableMap) 🔹 Important Implementing Classes ✔ HashMap • Implements Map • Does NOT maintain order • Allows one null key ✔ LinkedHashMap • Extends HashMap • Maintains insertion order ✔ TreeMap • Implements NavigableMap • Stores data in sorted order • Does NOT allow null key ✔ Hashtable • Implements Map • Thread-safe (synchronized) • Does NOT allow null key/value 🔹 Key Differences ✔ HashMap → Fast, no ordering ✔ LinkedHashMap → Maintains insertion order ✔ TreeMap → Sorted data ✔ Hashtable → Thread-safe but slower 📌 When to Use What? ✅ Use HashMap → when performance is priority ✅ Use LinkedHashMap → when insertion order matters ✅ Use TreeMap → when sorting is required ✅ Use Hashtable → when thread safety is needed 💡 Key Takeaway: Understanding Map hierarchy helps in choosing the right data structure based on use-case rather than just coding blindly. 🙏 Special thanks to Vaibhav Barde Sir for the guidance! 🔥 #CoreJava #JavaLearning #JavaDeveloper #Map #HashMap #TreeMap #LinkedHashMap #Hashtable #JavaCollections #Programming #LearningJourney
To view or add a comment, sign in
-
-
🚀 Core Java Learning Journey Explored Constructors in Java and the rules for writing them ☕ 🔹 What is a Constructor? A constructor is a special method used to initialize objects. It is automatically called when an object is created. 📌 Key Features of Constructors: ✅ Same name as the class ✅ No return type (not even "void") ✅ Automatically invoked during object creation ✅ Used to initialize instance variables 🔹 Types of Constructors: ✔️ Default Constructor ✔️ Parameterized Constructor 📌 Rules for Writing Constructors: 🔸 Constructor name must be the same as the class name 🔸 It should not have any return type 🔸 Can be overloaded (multiple constructors in one class) 🔸 Cannot be static, final, or abstract 🔸 If no constructor is written, Java provides a default constructor 💡 Example: class Student { int id; String name; Student(int i, String n) { // Parameterized constructor id = i; name = n; } } 🎯 Key Takeaway: Constructors make object initialization easy and are a fundamental part of Object-Oriented Programming in Java. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Constructors #OOP #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
📘 Day 6 of Java Learning Series 🔹 Control Statements in Java (if-else, loops) Control statements help us control the flow of execution in a program. They allow decision-making and repetition of tasks. 🔸 1. if-else Statement (Decision Making) Used when we want to execute code based on a condition. 💡 Example: int age = 18; if (age >= 18) { System.out.println("You can vote"); } else { System.out.println("You cannot vote"); } 🔸 2. Loops (Repetition) Loops help us execute a block of code multiple times. 👉 for loop (when number of iterations is known) for (int i = 1; i <= 5; i++) { System.out.println(i); } 👉 while loop (runs while condition is true) int i = 1; while (i <= 5) { System.out.println(i); i++; } ✅ Key Takeaways: ✔ if-else → decision making ✔ loops → repetition ✔ for loop → fixed iterations ✔ while loop → condition-based execution 💬 Which loop do you use more – for or while? 👉 Follow me for more Java content 🚀 #Java #Programming #100DaysOfCode #Developers #Learning #CoreJava
To view or add a comment, sign in
-
-
🚀 Day 28 | Core Java Learning Journey 📌 Topic: Set Interface, HashSet & LinkedHashSet Today, I learned about the Set Interface in Java and its important implementations like HashSet and LinkedHashSet. These are part of the Java Collections Framework and are used when we need to store unique elements. 🔹 Set Interface in Java ✔ Part of java.util package ✔ Does NOT allow duplicate elements ✔ Allows at most one null value ✔ Does NOT maintain insertion order (general Set behavior) ✔ Does NOT support indexing (no get(index)) 💡 Important: Order depends on implementation (e.g., HashSet vs LinkedHashSet) 🔹 HashSet ✔ Class that implements Set Interface ✔ Introduced in JDK 1.2 ✔ Underlying Data Structure: Hash Table (not exactly Hashtable class ❗ correction) ✔ Does NOT maintain insertion order ✔ Allows one null value ✔ Not synchronized (not thread-safe) 📌 Performance: ✔ Very fast operations (O(1) average for add, remove, contains) 🔹 LinkedHashSet ✔ Subclass of HashSet ✔ Introduced in JDK 1.4 ✔ Data Structure: Hash Table + Doubly Linked List ✔ Maintains insertion order ✔ Allows one null value 💡 Use when you need uniqueness + order 📌 Key Difference: HashSet vs LinkedHashSet ✔ HashSet → No order, faster ✔ LinkedHashSet → Maintains insertion order, slightly slower 💡 Understanding Set helps in handling unique data efficiently and choosing the right collection based on performance vs ordering needs. Special thanks to Vaibhav Barde Sir for the guidance! #CoreJava #JavaLearning #JavaDeveloper #Set #HashSet #LinkedHashSet #JavaCollections #Programming #LearningJourney
To view or add a comment, sign in
-
-
Exception Handling in Java – My Learning Journey Today, I explored one of the most important concepts in Java – Exception Handling. 📌 What is an Exception? An exception is an unusual activity that occurs during the execution of a program due to faulty input, leading to abrupt termination. 💡 For example: When we divide a number by zero, the program stops immediately, and the remaining lines are not executed. This is called abrupt termination. ⚙️ Key Understanding - Exceptions do not occur at compile time - They occur during runtime (execution time) - When an exception happens: - An exception object is created - It is sent to the runtime system - If no handling is present → Default Exception Handler terminates the program 🛠️ How to Handle Exceptions? We use try-catch blocks to handle exceptions and ensure smooth program execution. ✔️ Risky code is placed inside "try" ✔️ Handling logic is written in "catch" This helps in avoiding abrupt termination and allows the program to continue normally. 🔁 Multiple Catch Blocks We can use multiple "catch" blocks for a single "try". 👉 Important rules: - Only one catch block executes based on the exception - Always write specific exceptions first - Keep generic exception (Exception e) at the end 📚 Types of Exceptions I Learned - Arithmetic Exception (e.g., division by zero) - Negative Array Size Exception - Input Mismatch Exception - Array Index Out of Bounds Exception - Null Pointer Exception 🎯 Key Takeaway Exception handling helps us prevent abrupt termination and ensures the program runs smoothly and safely. 💭 Learning Java step by step and building strong fundamentals in OOP and error handling! #Java #ExceptionHandling #Programming #CodingJourney #OOP #Learning #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 Core Java Learning Journey Explored Variables in Java ☕ 🔹 What is a Variable? A variable is a container used to store data values in a program. Each variable has a name, type, and value. 📌 Types of Variables in Java: ✅ Local Variables - Declared inside methods, constructors, or blocks - Accessible only within that scope - Must be initialized before use ✅ Instance Variables - Declared inside a class but outside methods - Belong to objects - Each object has its own copy ✅ Static Variables - Declared using "static" keyword - Shared among all objects of the class - Memory allocated only once 💡 Example: "int age = 21;" "String name = "Java";" 🎯 Key Takeaway: Variables are the basic building blocks of any Java program used to store and manage data efficiently. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Variables #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored Types of Constructors in Java (In Depth) ☕ 🔹 Constructors are special methods used to initialize objects when they are created. They help in setting up the initial state of an object. 📌 1️⃣ Default Constructor (Implicit) - Provided automatically by Java if no constructor is defined - Does not take any parameters - Initializes instance variables with default values: - "int → 0" - "boolean → false" - "char → '\u0000'" - "reference types → null" - Mainly used for basic object creation without custom initialization 💡 Example: class Student { int id; String name; } 👉 Here, Java internally provides a default constructor --- 📌 2️⃣ No-Argument Constructor (Explicit) - Defined by the programmer without parameters - Used to assign custom default values instead of Java defaults - Improves control over object initialization 💡 Example: class Student { int id; String name; Student() { id = 100; name = "Default"; } } --- 📌 3️⃣ Parameterized Constructor - Accepts parameters to initialize variables - Allows different objects to have different values - Helps in making code more flexible and reusable 💡 Example: class Student { int id; String name; Student(int id, String name) { this.id = id; this.name = name; } } --- 🎯 Key Takeaway: - Default constructor → Automatic initialization - No-argument constructor → Custom default values - Parameterized constructor → Dynamic initialization Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Constructors #OOP #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Day 3 of My Java Learning Journey – Control Statements in Java Today, I learned how programs make decisions and repeat tasks using Control Statements in Java. These are essential for building logic in real-world applications. 🔹 Types of Control Statements: ➤ 1. if-else Statement Used for decision making 👉 Executes code based on conditions if (x > 10) { System.out.println("Greater than 10"); } else { System.out.println("Less than or equal to 10"); } ➤ 2. switch Statement Used when we have multiple choices 👉 Cleaner alternative to multiple if-else switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; default: System.out.println("Invalid day"); } ➤ 3. Loops (Repetition Statements) Used to execute code multiple times ✔ for loop – when number of iterations is known ✔ while loop – when condition is checked before execution ✔ do-while loop – executes at least once for(int i = 1; i <= 5; i++) { System.out.println(i); } 💡 Key Learning: Control statements help in decision-making and repeating tasks, making programs smarter and more dynamic. 📌 Practiced writing programs using if-else, switch, and loops to strengthen my logic-building skills. #Java #Programming #CodingJourney #LearningJava #ControlStatements #100DaysOfCode #Developers 🚀
To view or add a comment, sign in
-
🚀 Starting My Java Learning Journey – Day 10 🔹 Topic: Recursion in Java Recursion is a process where a method calls itself to solve a problem. It is mainly used to break down complex problems into smaller, simpler sub-problems. A recursive function must have: ✔ Base Case → condition to stop recursion ✔ Recursive Call → method calling itself Example: Factorial of a Number public class Main { static int factorial(int n) { if (n == 1) { // base case return 1; } return n * factorial(n - 1); // recursive call } public static void main(String[] args) { int result = factorial(5); System.out.println("Factorial: " + result); } } Output: Factorial: 120 ✔ Recursion solves problems by calling the same method repeatedly ✔ Every recursive method must have a base case ✔ Useful for problems like factorial, Fibonacci, tree traversal #Java #JavaLearning #Programming #BackendDevelopment #CodingJourney #Recursion
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