-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package shuo.laoma.java8.c91; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class AnonymousDemo { | ||
|
||
public static void main(String[] args) { | ||
//列出当前目录下的所有后缀为.txt的文件 | ||
File f = new File("."); | ||
File[] files = f.listFiles(new FilenameFilter(){ | ||
@Override | ||
public boolean accept(File dir, String name) { | ||
if(name.endsWith(".txt")){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
}); | ||
|
||
|
||
Arrays.sort(files, new Comparator<File>() { | ||
|
||
@Override | ||
public int compare(File f1, File f2) { | ||
return f1.getName().compareTo(f2.getName()); | ||
} | ||
}); | ||
|
||
|
||
|
||
ExecutorService executor = Executors.newFixedThreadPool(100); | ||
executor.submit(new Runnable() { | ||
@Override | ||
public void run() { | ||
System.out.println("hello world"); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package shuo.laoma.java8.c91; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class BasicLambda { | ||
|
||
public static void main(String[] args) { | ||
File f = new File("."); | ||
// File[] files = f.listFiles((File dir, String name) -> { | ||
// if (name.endsWith(".txt")) { | ||
// return true; | ||
// } | ||
// return false; | ||
// }); | ||
// | ||
// File[] files = f.listFiles((File dir, String name) -> { | ||
// return name.endsWith(".txt"); | ||
// }); | ||
// | ||
// File[] files = f.listFiles((File dir, String name) -> name.endsWith(".txt")); | ||
|
||
File[] files = f.listFiles((dir, name) -> name.endsWith(".txt")); | ||
|
||
Arrays.sort(files, (f1, f2) -> f1.getName().compareTo(f2.getName())); | ||
|
||
ExecutorService executor = Executors.newFixedThreadPool(100); | ||
executor.submit(()->System.out.println("hello")); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package shuo.laoma.java8.c91; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
public class ListUtil { | ||
|
||
static class Student { | ||
String name; | ||
double score; | ||
|
||
public Student(String name, double score) { | ||
this.name = name; | ||
this.score = score; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public double getScore() { | ||
return score; | ||
} | ||
|
||
public void setScore(double score) { | ||
this.score = score; | ||
} | ||
} | ||
|
||
public static <E> List<E> filter(List<E> list, Predicate<E> pred) { | ||
List<E> retList = new ArrayList<>(); | ||
for (E e : list) { | ||
if (pred.test(e)) { | ||
retList.add(e); | ||
} | ||
} | ||
return retList; | ||
} | ||
|
||
public static <T, R> List<R> map(List<T> list, Function<T, R> mapper) { | ||
List<R> retList = new ArrayList<>(list.size()); | ||
for (T e : list) { | ||
retList.add(mapper.apply(e)); | ||
} | ||
return retList; | ||
} | ||
|
||
public static <E> void foreach(List<E> list, Consumer<E> consumer) { | ||
for (E e : list) { | ||
consumer.accept(e); | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
List<Student> students = Arrays.asList(new Student[] { | ||
new Student("zhangsan", 89d), | ||
new Student("lisi", 89d), | ||
new Student("wangwu", 98d) }); | ||
|
||
// 过滤90分以上的 | ||
students = filter(students, t -> t.getScore() > 90); | ||
|
||
//根据学生列表返回名称列表 | ||
List<String> names = map(students, t -> t.getName()); | ||
|
||
//将学生名称转换为大写 | ||
students = map(students, t -> new Student(t.getName().toUpperCase(), t.getScore())); | ||
|
||
foreach(students, t -> t.setName(t.getName().toUpperCase())); | ||
} | ||
|
||
} |