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

×

Java :Alternative to lambda expression

https://sites.google.com/site/indy256/algo/suffix_array2

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

1★abeyaar
33422140
accept rate: 30%

retagged 03 Feb '15, 15:38

mediocoder's gravatar image

3★mediocoder
1.1k313


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;
              }

              @Override
              public int compare(Integer index1, Integer index2)
              {
                  return Long.compare(array[index1],array[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.
http://stackoverflow.com/questions/21970719/java-arrays-sort-with-lambda-expression
http://stackoverflow.com/questions/5898690/java-sort-array1-based-on-array2
http://stackoverflow.com/questions/18129807/in-java-how-do-you-sort-one-list-based-on-another

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.

link

answered 03 Feb '15, 14:53

mediocoder's gravatar image

3★mediocoder
1.1k313
accept rate: 36%

edited 03 Feb '15, 15:36

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

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

@rohanagarwal
You are right and I am aware of it, but compareTo() has problems with primitive datatypes.
For performance enhancement check http://stackoverflow.com/questions/9150446/compareto-with-primitives-integer-int and this.
Also didn't want to complicate things with boxing/unboxing for the OP. :-)

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

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×1,302
×847
×111
×109
×40
×4

question asked: 03 Feb '15, 13:00

question was seen: 3,771 times

last updated: 03 Feb '15, 20:37