📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
🧾 Introduction
Writing good Java code is not just about making things work.
It’s about writing clean, short, and smart code that looks professional.
Sometimes, you don’t need 5 lines when one smart line can do the job.
In this guide, you’ll learn 10 powerful Java one-liners that can:
✅ Make your code cleaner
✅ Save time during coding
✅ Impress interviewers and teammates
✅ 1. Swap Two Variables Without Temp Variable (Java 7+)
a = a + b - (b = a);
Explanation:
- Smart arithmetic trick to swap two integers without using a third variable.
- But use carefully to avoid confusion — prefer readability when needed.
✅ Works great for quick algorithm tricks.

✅ 2. Initialize a List in One Line (Java 9+)
List<String> fruits = List.of("Apple", "Banana", "Cherry");
Explanation:
List.of()
quickly creates an immutable list without needingnew ArrayList<>()
and manual adds.
✅ Cleaner and faster setup for small lists.
✅ 3. Find Max of Two Numbers
int max = Math.max(a, b);
Explanation:
- Built-in method — no need for manual
if-else
to find maximum.
✅ Clean and fast.
✅ 4. Read All Lines from a File
List<String> lines = Files.readAllLines(Path.of("file.txt"));
Explanation:
- Java NIO makes reading a whole text file into a list super easy.
✅ No more messy BufferedReader
loops if you just need all lines.
✅ 5. Convert List to Comma-Separated String
String result = String.join(", ", fruits);
Explanation:
- Quickly joins list elements into a single string with commas.
✅ Very useful for logs, APIs, CSV formats.
✅ 6. Remove Null Values from a List
list.removeIf(Objects::isNull);
Explanation:
removeIf
+ method reference removes all null entries in one line.
✅ No need for manual for
loops and checks.
✅ 7. Get a Random Element from a List
String randomFruit = fruits.get(new Random().nextInt(fruits.size()));
Explanation:
- Pick a random item by generating a random valid index.
✅ Quick random selections for games, tests, etc.
✅ 8. Count Items Matching a Condition
long count = list.stream().filter(x -> x > 10).count();
Explanation:
- Stream + filter + count in one clean line.
✅ Great for statistics, data filtering, and analytics.
✅ 9. Sort a List in Descending Order
list.sort(Comparator.reverseOrder());
Explanation:
- Sorts the list in descending order with one simple call.
✅ No need to write custom comparators manually.
✅ 10. Check If a String is a Palindrome
boolean isPalindrome = str.equals(new StringBuilder(str).reverse().toString());
Explanation:
- Reverse the string using
StringBuilder
and compare with the original.
✅ Perfect one-liner for quick coding rounds and interviews.
📌 Quick Recap: 10 Java One-Liners You Should Know

✅ Final Thoughts
You don’t need to write long, complicated code to be impressive.
Sometimes, one smart line is all it takes to show you know what you’re doing.
✅ Master these one-liners.
✅ Use them wisely — keeping code clean, not confusing.
✅ And you’ll look like a pro in interviews, team discussions, and real-world projects.
Good developers write code.
Great developers write smart, clean, efficient code.
Comments
Post a Comment
Leave Comment