1.使用Lambda表达式进行集合遍历
❌ 未使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
for (String fruit : list) {
System.out.println(fruit);
}
✅使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
list.forEach(fruit->System.out.println(fruit));
2.使用Lambda表达式进行排序
❌ 未使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
Collections.sort(list,new Comparator(){
public int compare(String s1,String s2){
return s1.copmpare(s2);
}
})
✅使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
Collections.sort(list,(s1,s2)->s1.compare(s2);
3.使用Lambda 表达式进行过滤
❌ 未使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
List filteredList=new ArrayList();
for(String fruit : list){
if(fruit.startsWith("a")){
filteredList.add(fruit);
}
}
✅使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
List filteredList=list.stream().filter(fruit->fruit.startsWith("a))
.collect(Collectors.toList());
4.使用Lambda表达式进行映射
❌ 未使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
List lengths=new ArrayList();
for(String fruit:list){
lengths.add(fruit.length);
}
✅使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
List<String> lengths=list.stream().map(fruit->fruit.length())
.collect(Collectors.toList());
5.使用Lambda表达式进行归约
❌ 未使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
int sum=0;
for(int i :list){
sum+=i;
}
✅使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
int sum=list.stram().reduce(0,(a,b)->a+b);
6.使用Lambda表达式进行分组
❌ 未使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
Map<Integer,List<String>> grouped=new Hahmap<>();
for(String fruit :list){
int length=fruit.length();
if(!grouped.containsKey(length)){
grouped.put(length,new ArrayList<String>());
}
grouped.get(length).add(fruit);
}
✅使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
Map<Integer,List<String>> grouped=list.stram()
.collect(Collectors.groupingBy(fruit->fruit.length()));
7.使用Lambda表达式进行函数式接口的实现
❌ 未使用Lambda表达式:
java
public interface MyInterface{
public void doSomething(String fruit);
}
MyInterface myObject=new MyInterface(){
public void doSomething(String input){
System.out.doSomething(input);
}
};
myObject.doSomething("Hello World");
✅使用Lambda表达式:
java
MyInterface myObject=input->System.out.println(input);
myObject.doSomething("Hello world");
8.使用Lambda表达式进行线程的创建
❌ 未使用Lambda表达式:
java
Thread thread=new Thread(new Runnable(){\
puhlic void run(){
System.out.println("Thread is Running");
}
});
thread.start();
✅使用Lambda表达式:
java
Thread thread=new Thread(()->System.out.println("Thread is Running."));
thread.start();
9.使用Lambda表达式进行Optional的操作
❌ 未使用Lambda表达式:
java
String str="Hello World";
if(str!=null){
System.out.println(str.toUppercase());
}
✅使用Lambda表达式:
java
Optional str=Optional.ofNullable("Hello World");
str.map(String::toUppercase).ifPresent(System.out.println);
10.使用Lambda表达式进行Stream的流水线操作
❌ 未使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
List<String> filteredList=new ArrayList<>();
for(String fruit:list){
if(fruit.startWith("a")){
filteredList.add(fruit.toUppercase());
}
}
Collections.sort(filteredList);
✅使用Lambda表达式:
java
List list = Arrays.aslist("apple", "banana", "orange");
List<String> filteredList=list.stram().filter(fruit->fruit.startsWith("a"))
.map(String::toUppercase)
.sorted().Collect(Collectors.toList);