You are not logged in. Please login at to post your questions!


Java :Alternative to lambda expression

Can anyone provide me the alternative statement to the Arrays.sort line that should work with Java 7. Thanks. :)

asked 03 Feb '15, 13:00

abeyaar's gravatar image

accept rate: 30%

retagged 03 Feb '15, 15:38

mediocoder's gravatar image


You can do this by just using a custom Comparator equivalent to the given lambda expression and then apply Arrays.sort method to make it work in Java7.

For example you maybe wanting to do the following after the line
rank2[i] = ((long) rank[i] << 32) + (i + len < n ? rank[i + len] + 1 : 0);
or you create comparator outside, depends upon you.

class MyComparator implements Comparator<Integer> {
              private final long[] array;
              public MyComparator(long[] array)
                  this.array = array;

              public int compare(Integer index1, Integer index2)

      MyComparator comp = new MyComparator(rank2);
      Arrays.sort(sa, comp);

See this working alternative approach on IDEONE Java7.

See these links at stackoverflow for more information.

You may also want @betlista ♦♦ to answer/comment here, he is very good in general and exceptional in java. He may provide some better idea(s) regarding this and lambda expressions indroduced in Java8.


answered 03 Feb '15, 14:53

mediocoder's gravatar image

accept rate: 36%

edited 03 Feb '15, 15:36

using Integer instead of int will cost you extra time. Autoboxing/ can implement it easily with primitives.

(03 Feb '15, 20:04) rohanagarwal5★

You are right and I am aware of it, but compareTo() has problems with primitive datatypes.
For performance enhancement check and this.
Also didn't want to complicate things with boxing/unboxing for the OP. :-)

(03 Feb '15, 20:37) mediocoder3★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 03 Feb '15, 13:00

question was seen: 3,771 times

last updated: 03 Feb '15, 20:37