This is one line:
List<Bean> beans = service.findBeans(); Collections.sort(beans, new BeanComparator()); return beans;
But more seriously, Java is not exactly the right language for single-line. In addition, just because something is single-line does not mean that it is better. For example, I was initially surprised to find that this:
return condition ? a : b;
Generates longer bytecode than
if( condition )
return a;
else
return b;
But it is both a language and a compiler.
If you insist on your one-liner, Guava Ordering can do this:
return Ordering.from( new BeanComparator() ).sortedCopy( service.findBeans() );
The returned list is modified, serialized and has random access.
- , . . . , , .