Day - 13 : Sorting an arraylist using comparator operator. Example : import java.util.*; class mycomparator implements Comparator <Integer>{ public int compare (Integer O1, Integer O2){ return O2 - O1; } } public class java { public static void main (String [ ] args){ Arraylist <Integer> list = new Arraylist <>( ); list.add(20); list.add(45); list.add(48); list.add(37); list.add(76); list.add(55); list.add(15); list.sort( new mycomparator ( ) ); System.out.println(list); } } This prints the arraylist in descending order. #java #Arraylist #programming #learning #advancedjava #javacore #Sorting EchoBrains
Sorting ArrayList with Comparator in Java
More Relevant Posts
-
Collection vs. Collection Framework in Java: What's the Difference? Are you new to Java or brushing up on the basics? One of the most common areas of confusion is the difference between a Collection and the Collection Framework. Here is a quick breakdown to clear things up: Collection: A single, fundamental interface (java.util.Collection) that represents a group of individual objects. It is the root of the hierarchy. Think of it as a single type of storage box. Collection Framework: A comprehensive architecture encompassing multiple interfaces (List, Set, Map), concrete implementations (ArrayList, HashSet), and utility algorithms (sorting, searching). Think of it as an entire warehouse management system! Check out the infographic below for a side-by-side comparison of their definitions, scope, and key characteristics. 👇 #Java #JavaDeveloper #Programming #SoftwareEngineering #TechTips #Coding #LearnJava
To view or add a comment, sign in
-
-
💻 Java Practice – String Logic Today I worked on strengthening my understanding of string operations in Java. Practiced small exercises like: • Counting characters using length() • Printing each character using charAt() • Counting vowels and consonants • Removing spaces from a string using replace() These simple problems help build stronger logic and a deeper understanding of how strings are handled in Java. #Java #ProgrammingFundamentals #LearningInPublic #DeveloperJourney #Consistency
To view or add a comment, sign in
-
## Just wrapped up an incredibly detailed YouTube playlist on Java Multithreading: Synchronization, Locks, Executors, Deadlock, CountdownLatch & CompletableFuture. # Here are some of my key takeaways: 1. What is Multithreading? It's the concurrent execution of multiple threads (smaller units of a process) for maximum CPU utilization and smoother app performance. 2. Core Concepts I Learned: - Threads and processes can execute truly in parallel across cores using the OS scheduler. - Daemon Threads are background tasks the JVM doesn't wait for when shutting down. - Synchronization ensures safe access to shared resources, preventing race conditions using locks. - Reentrant Locks offer manual control and fairness policies, perfect for fine-tuned concurrency. - Thread Safety ensures consistent and correct data handling across concurrent executions. 3. Executors Framework (introduced in Java 5) - Executor - ExecutorService - ScheduledExecutorService It allows us to submit tasks, schedule jobs, and manage thread pools efficiently, improving performance and scalability. => Also explored CountDownLatch, CyclicBarrier, and CompletableFuture - some of the most elegant tools for coordination and asynchronous programming in Java. A big thanks to Vipul Tyagi Sir for this goldmine of a video. 🙏 If you're diving into advanced Java or preparing for interviews, I'd highly recommend checking it out! #Java #Multithreading #Executors #Concurrency #LearningJourney #CompletableFuture #CountDownLatch #CyclicBarrier #JavaDevelopers #ThankfulPost
To view or add a comment, sign in
-
-
📌 wait(), notify(), notifyAll() in Java – Thread Communication In multithreading, sometimes threads need to coordinate with each other instead of just locking resources. Java provides three important methods for communication: • wait() • notify() • notifyAll() 1️⃣ wait() • Causes the current thread to release the lock • Moves the thread into waiting state • Must be called inside synchronized block 2️⃣ notify() • Wakes up one waiting thread • Does NOT release the lock immediately • The awakened thread waits until lock is available 3️⃣ notifyAll() • Wakes up all waiting threads • Only one will acquire the lock next 4️⃣ Important Rules • These methods belong to Object class • Must be called inside synchronized context • Used for inter-thread coordination 5️⃣ Why They Are Needed Used in scenarios like: • Producer–Consumer problem • Task scheduling • Resource pooling 🧠 Key Takeaway synchronized controls access. wait/notify control communication. Together, they enable proper coordination between threads in Java. #Java #Multithreading #Concurrency #ThreadCommunication #CoreJava
To view or add a comment, sign in
-
Strengthening My Java Fundamentals! While learning Java, I explored the differences between: 🔹 String 🔹 StringBuffer 🔹 StringBuilder 🔹 StringTokenizer Here’s what I understood: ✅ String – Immutable (cannot be changed once created). Efficient for fixed data but creates new objects when modified. ✅ StringBuffer – Mutable and Thread-Safe. Best for multi-threaded environments where data consistency is important. ✅ StringBuilder – Mutable but Not Thread-Safe. Faster than StringBuffer and suitable for single-threaded applications. ✅ StringTokenizer – Used to break a string into tokens (words) based on delimiters. Understanding these concepts helped me improve my knowledge about memory management, performance optimization, and multithreading behavior in Java. #Java #CoreJava #LearningJourney #Programming #SoftwareDevelopment
To view or add a comment, sign in
-
-
💡 String vs StringBuilder vs StringBuffer in Java All three are used to work with text in Java, but they behave differently. 🔹 String Immutable — once created, the value cannot change. 🔹 StringBuilder Mutable and faster for modifying strings. 🔹 StringBuffer Similar to StringBuilder but thread-safe (synchronized). 📌 Simple rule: String → constant text StringBuilder → single-threaded modifications StringBuffer → multi-threaded environments Understanding these differences helps write more efficient Java code 🚀 #Java #JavaDeveloper #Programming #BackendDevelopment
To view or add a comment, sign in
-
-
🔹 Java Concept of the Day 📌 Can we use string as a variable name in Java? Yes ✅ Java is case-sensitive, so: String → Predefined class (used to store text) string → Just a normal variable name Example: int string = 10; System.out.println(string); ✔ Output: 10 But ⚠ this is not a good practice because it creates confusion between the String class and the variable name. 💡 Best Practice: Always use meaningful and clear variable names like: int number = 10; 🧠 Key Learning: In Java, identifiers are case-sensitive, but good naming conventions make your code more readable and professional. #Java #Programming #CodingBasics #DSA #LearningInPublic #JavaDeveloper
To view or add a comment, sign in
-
🧵 Multithreading Today I explored some important concepts of Multithreading in Java. Multithreading allows a program to execute multiple tasks simultaneously, improving performance and efficient CPU utilization. 🔹 Two Ways to Achieve Multitasking • Process-based multitasking – Multiple programs run simultaneously. • Thread-based multitasking – Multiple threads run inside a single program. 🔹 Two Ways to Create Threads 1️⃣ Extending Thread Class – Create a class that extends Thread and override the run() method. 2️⃣ Implementing Runnable Interface – Create a class that implements Runnable and pass it to a Thread object. 🔹 Important Thread Control Methods • Thread.sleep(milliseconds) – Pauses the current thread for a specific time. • Object.wait() – Makes the thread wait until another thread notifies it. • Thread.join() – Makes a thread wait until another thread finishes execution. • Thread.yield() – Temporarily pauses the current thread to allow other threads of the same priority to execute. • Thread.suspend() – Temporarily stops a thread (⚠ Deprecated in Java). 🔹 Synchronization Synchronization is used to control the access of multiple threads to shared resources and prevent data inconsistency. 💡 Understanding multithreading concepts helps build efficient and high-performance Java applications. #Java #Multithreading #JavaProgramming #LearningJava #CodingJourney #SoftwareDevelopment
To view or add a comment, sign in
-
-
Many developers ask: Why do Java Collections not support primitive data types? The reason is that Java Collections work with objects, not primitives. To handle primitive values, Java uses Wrapper Classes like Integer, Double, and Character. Example: int → Integer double → Double char → Character This process is called Autoboxing and Unboxing. Understanding such small concepts can make a big difference in mastering Java. 🚀 #CoreJava #JavaTips #Programming #JavaDeveloper
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