集合遍历-forEach
public class Example3 {
public static void main(String[] args) {
//集合的遍历
ArrayList<Integer> integerList = new ArrayList<>();
Collections.addAll(integerList, 1, 23, 4, 5, 6, 7, 8, 9, 0, 10);
//输出集合中的偶数
integerList.forEach(ele -> {
if(ele%2==0){
System.out.println(ele);
}
});
}
}
集合删除 - removeIf
public class Example4 {
public static void main(String[] args) {
//需求, 删除年龄大于10的人
ArrayList<Person> people = new ArrayList<>();
people.add(new Person("小明", 10));
people.add(new Person("小嘎", 13));
people.add(new Person("小红", 9));
people.add(new Person("小强", 6));
people.add(new Person("老王", 27));
people.add(new Person("老表", 28));
//一般做法
Iterator<Person> iterator = people.iterator();
while (iterator.hasNext()){
Person person = iterator.next();
if(person.age>=10){
iterator.remove();
}
}
//Lambda表达式
people.removeIf(ele -> ele.age >=10);
System.out.println(people);
}
}
线程实例化
public class Example5 {
public static void main(String[] args) {
//需求: 通过lambda表达式, 实现实例化一个线程
Thread thread = new Thread(() -> {
for(int i = 0 ; i < 100 ; i++){
System.out.println(i);
}
});
thread.start();
}
}