Comparator is an interface in Java used to define a custom way to compare and sort objects.Use it when: You want to sort objects in a custom way. The class does NOT implement Comparable. You want multiple sorting strategies for the same class. int compare(T o1, T o2) Returns: Negative → o1 comes before o2 Positive → o2 comes before o1 0 → equal order
Java Comparator for Custom Sorting
More Relevant Posts
-
🚀 365-Day Java Challenge – Day 306 🚀 ⸻ 🔹 Day 306: Lambda expressions and variable capture Given the code: int factor = 2; java.util.function.Function<Integer,Integer> multiplier = x -> x * factor; factor = 3; int result = multiplier.apply(5); What is the value of result? Options: A) 10 B) 15 C) Compilation error D) 5
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 308 🚀 ⸻ 🔹 Day 308: Default Methods in Interfaces Which of the following statements about default methods in Java interfaces is FALSE? Options: A) A default method can provide a concrete implementation that classes inheriting the interface can use without overriding. B) A class can inherit multiple default methods with the same signature from different interfaces without any conflict. C) A default method can be overridden by a concrete class to provide a specific implementation. D) An abstract class that implements an interface is not required to implement the interface’s default methods.
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 326 🚀 ⸻ 🔹 Day 326: Lambda Expressions and Method References Given the following code snippet: List<String> list = Arrays.asList("a", "b", "c"); list.forEach(s -> System.out.print(s.toUpperCase())); What is printed to the console when this code executes? Options: A) abc B) ABC C) a b c D) aB c
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 302 🚀 ⸻ 🔹 Day 302: Try‑with‑Resources Which of the following statements about the try‑with‑resources statement is correct? try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) { /* ... */ } Options: A) The resource declared in the parentheses must implement the java.io.Closeable interface only. B) The resource is closed automatically at the end of the try block, even if an exception is thrown. C) Multiple resources can be declared, but they must be of the same type. D) The try‑with‑resources statement cannot be combined with a catch or finally block.
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 328 🚀 ⸻ 🔹 Day 328: Effect of final on method parameters Consider the following method: public void update(final int value) { value = value + 10; System.out.println(value); } When this code is compiled, which statement is true? Options: A) The code compiles and prints the original value plus 10. B) The code compiles but throws a runtime exception. C) The code fails to compile because a final parameter cannot be reassigned. D) The code compiles, but the printed value is always zero.
To view or add a comment, sign in
-
✅ Java Features – Step 20: Sealed Classes (Java 17) 🔒 Java 17 introduced Sealed Classes, which allow you to control which classes can extend or implement a class/interface. Before this, any class could extend a public class. Example: public sealed class Shape permits Circle, Rectangle { } Only the permitted classes can extend Shape. final class Circle extends Shape { } final class Rectangle extends Shape { } Why this matters Gives better control over inheritance Improves code safety and design Useful for domain models with limited variations Real-world idea Imagine a payment system: sealed interface Payment permits CardPayment, UpiPayment, NetBanking { } Now only these payment types are allowed. Key takeaway Sealed classes help developers define strict class hierarchies and prevent unintended extensions. Next up: Pattern Matching for instanceof (Java 17) ⚡
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 301 🚀 ⸻ 🔹 Day 301: String immutability Given the following code, what is printed to the console? String s = "Hello"; s.concat(" World"); System.out.println(s); Options: A) Hello World B) Hello C) null D) Compilation error
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 310 🚀 ⸻ 🔹 Day 310: Stream API – Filtering and Summing What is the output of the following code? List<Integer> list = Arrays.asList(1,2,3,4,5); int sum = list.stream().filter(n -> n%2==0).mapToInt(Integer::intValue).sum(); System.out.println(sum); Options: A) 6 B) 8 C) 10 D) 12
To view or add a comment, sign in
-
Java Insight: "Effectively final" variables Not every variable needs to final to be used in lamda. "Effectively final" - a concept introduced in java 8. A variable is effectively final if - - it is assigned only once - its value is never modified afterwards Example: int count = 5; list.forEach(item -> { System.out.println(count)}); Even though "count" is not declared as "final", Java treats it as final as its value is not changed. Advantage: - thread safety and predictable behaviour in lamda #Java #Java8 #JavaTips
To view or add a comment, sign in
-
Use "Anonymous class " to display " happy Java Class!" Here are the steps to create an anonymous class version in Eclipse: Step 1: Create a New Java Project (if not already created) Open Eclipse If you already have the project from Part 1, you can reuse it Otherwise, go to File → New → Java Project Name it "ButtonCallbackDemo" (or any name you prefer) Click Finish Step 2: Create/Ensure Required Files Make sure you already have: ClickListener.java interface Button.java class If not, create them as shown in the previous guide. Step 3: Create the Main2 Class with Anonymous Class Right-click on the src folder → New → Class Name it Main2 Check the box "public static void main(String[] args)" Click Finish Step 4: Add the Anonymous Class Code Eclipse will generate a basic class. Replace it with: java public class Main2 { public static void main(String[] args) { Button button = new Button(); // Anonymous class implementation button.setClickListener(new ClickListener() { @Override public void onClick() { System.out.println("Happy Java class!"); } }); button.click(); } }
Use "Anonymous class " to display " happy Java Class!
https://www.youtube.com/
To view or add a comment, sign in
More from this author
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