Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:41:58+00:00 2026-06-04T08:41:58+00:00

I am coding something like this: List<Bean> beans = service.findBeans(); Collections.sort(beans, new BeanComparator()); return

  • 0

I am coding something like this:

List<Bean> beans = service.findBeans();
Collections.sort(beans, new BeanComparator());
return beans;

It works perfectly. What I am looking for is a shortcut to do this with just one line:

return somelibrary.Collections.sort(service.findBeans(), new BeanComparator());

Or:

return somelibrary.newList(service.findBeans(), new BeanComparator());

Note that it is required a mutable list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:42:00+00:00Added an answer on June 4, 2026 at 8:42 am

    This is one line:

    List<Bean> beans = service.findBeans(); Collections.sort(beans, new BeanComparator()); return beans;
    

    But more seriously, Java isn’t really the right language for one-liners. Also, just because something is a one-liner doesn’t mean that it is any better. For example, I was initially surprised to discover that this:

    return condition ? a : b;
    

    Creates a longer bytecode than

    if( condition )
        return a;
    else
        return b;
    

    But that’s just how the language and compiler are.

    If you insist on your one-liner, Guava‘s Ordering can do it:

    return Ordering.from( new BeanComparator() ).sortedCopy( service.findBeans() );
    

    The returned list is modifiable, serializable, and has random access.

    Efficiency-wise I think there’s a bit of a waste in terms of overhead. And also you’re now dependent on a 3rd-party library. You’d be essentially using very powerful tools for a very simple task. It’s overkill if this is all you’re using it for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity which looks something like this: (I'm coding to the web
I'm coding a N'th order markov chain. It goes something like this: class Chain:
I'm trying to wtire unit tests for a function that looks something like this:
I currently have a class something like this [DataContractAttribute] public class someClass { public
I have an activity that has properties that look something like this (they're set
I'm trying to get something like this to work by passing a function pointer.
Background: I'm coming from C#-land, so I'm looking for something like being able to
First off, I'm a complete beginner at C++. I'm coding something using an API,
In Objective-C on Apple there is something called Key-Value Coding that allows you to
When someone codes something for example in MASM, what is it called? Coding in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.