×

Java :Alternative to lambda expression

 0 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 1★abeyaar 334●2●21●40 accept rate: 30% 1.1k●3●13

 0 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 { 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. 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 1.1k●3●13 accept rate: 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) @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)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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