Closure Conversion
A closure can be assigned to a variable of compatible interface type. A compatible interface type must have a single method with a compatible return type and compatible argument types. For example:
interface IntPlus { int add(int x, int y); } IntPlus plus = { int x, int y => x + y };
In such case, a closure is converted to an instance of anonymous class that implements the given interface:
IntPlus plus = new IntPlus() { public int add(int x, int y) { return x + y; } };
Then, we call the closure by the interface method:
int sum = plus.add(5, 6);
This allows us to use a closure instead of anonymous subclass of interface with a single method. For example, the closure conversion can create an instance of Runnable:
new Thread({ => System.out.println("hi"); }).start();
Sometimes we can use the closure conversion even if the interface has more than one method. It is in case when all but one
method are implemented in Object. The closure is then converted to a single remaining method.
For example, interface Comparator contains two methods:
compare()
and equals()
.
Method equals()
is in Object and so the closure is converted
to method compare()
.
String[] girls = { "Jane", "Eva", "Sarah", "Alice" }; Arrays.sort(girls, { String s1, String s2 => int r = s1.length() - s2.length(); r == 0 ? s1.compareTo(s2) : r });
Exercises
- Declare interface HashCalculator.
HashCalculator hc = { String s1, String s2 => s1.hashCode() ^ s2.hashCode() }; System.out.println(hc.hash("elephant", "giraffe"));
- Use a closure to define Comparator which sort numbers descendingly.
Integer[] wages = { 1200, 4500, 850, 1500, 990 }; Arrays.sort(wages, );
- Sort an array of strings and count the number of comparisons.
Solutions
interface HashCalculator { int hash(String s1, String s2); }
Integer[] wages = { 1200, 4500, 850, 1500, 990 }; Arrays.sort(wages, { Integer i, Integer j => j.compareTo(i) });
String[] countries = { "France", "Czech Republic", "Poland", "Hungary", "Germany" }; int c = 0; Arrays.sort(countries, { String s1, String s2 => c++; s1.compareTo(s2) }); System.out.println("number of comparisons: " + c);
10 comments:
Wow, i think your examples are easier to follow than Neal Grafter's own. Thanks.
Vyas, Anirudh
I'm trying to rewrite:
EventQueue.invokeLater(new Runnable()
{
public void run()
{
//set some text.
}
});
As:
EventQueue.invokeLater(new Thread(
{ =>
//set some text
}
).start();
);
Running into compile time errors(after I downloaded the new prototype from javac.info), and I tried to follow your example from above. Can you spot the error please?
EventQueue.invokeLater() takes a Runnable as argument:
EventQueue.invokeLater({ => System.out.println("hi"); });
Thanks a lot! It works :).
Normally adding a method to an interface is backwards compatible. With closure conversion, adding a method to an interface with a single method is no longer backwards compatible since it can break code when its used with closures.
Perhaps I do not understand what you want to say because adding a method to an interface is NOT backward compatible.
Very much useful article. Kindly keep blogging
Java Training in Chennai
Java Online Training India
Jual Obat Aborsi Asli | Obat Cytotec Asli | 082241083319
jual obat aborsi pills cytotec asli manjur untuk menggugurkan kandungan usia 1 - 6 bulan gugur tuntas.CYTOTEC OBAT ASLI sangat efektif mengatasi TELAT DATANG BULANdan menjadikan anda gagal hamil , CYTOTEC adalah OBAT ABORSI 3 JAM BERHASIL GUGUR TUNTAS dengan kwalitas terbaik produk asli pfizer nomor 1 di dunia medis
JUAL OBAT ABORSI DI TEGAL
JUAL OBAT ABORSI DI SLAWI
JUAL OBAT ABORSI DI TEMANGGUNG
JUAL OBAT ABORSI DI WONOGIRI
JUAL OBAT ABORSI DI WONOSOBO
JUAL OBAT ABORSI DI BANDUNGAN
CARA MENGGUGURKAN KANDUNGAN
I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
evs full form
raw agent full form
full form of tbh in instagram
dbs bank full form
https full form
tft full form
pco full form
kra full form in hr
tbh full form in instagram story
epc full form
Hey friend, it is very well written article, thank you for the valuable and useful information you provide in this post. Keep up the good work! FYI, Pet Care adda
Sita Warrior Of Mithila Pdf Download , IDFC First Select Credit Card Benefits,Poem on Green and Clean Energy
Post a Comment