class Worker implements Runnable {
public void run() {
for (int i = 0; i < 100; i++)
doWork();
}
...
}
Worker w = new Worker();
new Thread(w).start();
class LengthComparator implements Comparator<String> {
public int compare(String first, String second) {
return Integer.compare(first.length(), second.length());
}
}
Arrays.sort(strings, new LengthComparator());
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("Thanks for clicking!");
}
});
(String first, String second) -> Integer.compare(first.length(), second.length());
(String first, String second) -> {
if (first.length() > second.length()) {
return 1;
} else if (first.length() == second.length()) {
return 0;
} else {
return -1;
}
};
() -> {
for (int i = 0; i < 1000; i ++) {
doWork();
}
}
Comparator<String> comp = (first, second) // Same as (String first, String second) -> Integer.compare(first.length(), second.length());
// Instead of (event) -> or (ActionEvent event) ->
eventHandler<ActionEvent> listener =
event -> System.out.println("Thanks for clicking!");
(x) -> {
if (x >= 0) {
return 1;
}
}
// expression lambda
Comparator<String> comp1 =
(first, second) -> Integer.compare(first.length(), second.length());
// statement lambda
Comparator<String> comp2 = (first, second) ->
{ return Integer.compare(first.length(), second.length());};
Arrays.sort(words, (first, second) -> Integer.compare(first.length(), second.length()));
Runnable r = () -> {
System.out.println("------");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// catch exception
}
};
Callable<String> c = () -> {
System.out.println("--------");
Thread.sleep(10);
return "";
};
(x) -> System.out.println(x) System.out::println
System.out::println (x) -> System.out.println(x) Math::pow (x, y) -> Math.pow(x, y)
String::compareToIgnoreCase (s1, s2) -> s1.compareToIgnoreCase(s2) 1.5 Constructor Reference
List<String> labels = ...; Stream<Button> stream = labels.stream().map(Button::new);
int[]::new (x) -> new int[x]
public void repeatMessage(String text, int count) {
Runnable r = () -> {
for (int i = 0; i < count; i ++) {
System.out.println(text);
Thread.yield();
}
};
new Thread(r).start();
}
int matches = 0;
for (Path p : files)
new Thread(() -> { if (p has some property) matches++; }).start();
// Illegal to mutate matches
Path first = Paths.get("/usr/bin");
Comparator<String> comp = (first, second) -> Integer.compare(first.length(),
second.length()); // Error: Variable first already defined
public class Application() {
public void doWork() {
Runnable runner = () -> {
...;
System.out.println(this.toString());
...
};
}
}
interface Person {
long getId();
default String getName() { return "John Q. Public"; }
}
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T>
keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1),
keyExtractor.applyAsInt(c2));
}
Arrays.sort(cities, (first, second) -> Integer.compare(first.length(), second.length()));
Arrays.sort(cities, Comparator.comparingInt(String::length));
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
[b]Lambda 示例[/b]
public void runnableTest() {
System.out.println("=== RunnableTest ===");
// 一个匿名的 Runnable
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("Hello world one!");
}
};
// Lambda Runnable
Runnable r2 = () -> System.out.println("Hello world two!");
// 执行两个 run 函数
r1.run();
r2.run();
}
public void runnableTest() {
System.out.println("=== RunnableTest ===");
// 一个匿名的 Runnable
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("Hello world one!");
}
};
// Lambda Runnable
Runnable r2 = () -> System.out.println("Hello world two!");
// 执行两个 run 函数
r1.run();
r2.run();
}
public class Person {
private String givenName;
private String surName;
private int age;
private Gender gender;
private String eMail;
private String phone;
private String address;
}
public class Person {
private String givenName;
private String surName;
private int age;
private Gender gender;
private String eMail;
private String phone;
private String address;
}
public class ComparatorTest {
public static void main(String[] args) {
List<Person> personList = Person.createShortList();
// 使用内部类实现排序
Collections.sort(personList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getSurName().compareTo(p2.getSurName());
}
});
System.out.println("=== Sorted Asc SurName ===");
for (Person p : personList) {
p.printName();
}
// 使用 Lambda 表达式实现
// 升序排列
System.out.println("=== Sorted Asc SurName ===");
Collections.sort(personList, (Person p1, Person p2) -> p1.getSurName().compareTo(p2.getSurName()));
for (Person p : personList) {
p.printName();
}
// 降序排列
System.out.println("=== Sorted Desc SurName ===");
Collections.sort(personList, (p1, p2) -> p2.getSurName().compareTo(p1.getSurName()));
for (Person p : personList) {
p.printName();
}
}
}
public class ComparatorTest {
public static void main(String[] args) {
List<Person> personList = Person.createShortList();
// 使用内部类实现排序
Collections.sort(personList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getSurName().compareTo(p2.getSurName());
}
});
System.out.println("=== Sorted Asc SurName ===");
for (Person p : personList) {
p.printName();
}
// 使用 Lambda 表达式实现
// 升序排列
System.out.println("=== Sorted Asc SurName ===");
Collections.sort(personList, (Person p1, Person p2) -> p1.getSurName().compareTo(p2.getSurName()));
for (Person p : personList) {
p.printName();
}
// 降序排列
System.out.println("=== Sorted Desc SurName ===");
Collections.sort(personList, (p1, p2) -> p2.getSurName().compareTo(p1.getSurName()));
for (Person p : personList) {
p.printName();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有