Lambda表达式语法进阶

Lambda表达式语法进阶—方法引用 ()->{}

一、什么是方法引用?

可以快速的将一个lambda表达式的实现指向一个已经实现的方法,就叫方法引用;

语法
//* 语法:方法的隶属者::方法名

1.案例1:

简单接口

@FunctionalInterface
public interface SingleReturnSingleParameter {
	int test(int a);
}

测试类

public class YiMoChaTest {

	public static void main(String[] args) {
		
		//* 传统方式:
		SingleReturnSingleParameter lambda1 = (a)-> change(a);
		
		//* 方法引用式:
		SingleReturnSingleParameter lambda2 = YiMoChaTest::change;
		
	}
	
	//* 已经实现的方法
	private static int change(int a) {
		return a + 2019;
	}
}

2.案例2-构造方法的引用:

Person 类

package com.entity;

public class Person {

	private String name;
	private int age;

	public Person() {
		System.out.println("Person类的无参数构造方法执行了!");
	}
	
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
		System.out.println("Person类的有参数构造方法执行了!");
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

测试类

public class YiMoChaTest2{

	public static void main(String[] args) {
		
		PersonCreator creator = () -> new Person();
		
		//* 构造方法的引用:
		PersonCreator creator2 = Person::new;
		Person cPerson = creator2.getPerson();
		
		//* 构造方法的引用:
		PersonCreator2 creator22 = Person::new;
		Person dPerson = creator22.getPerson("小明", 10);
	}
}

//*  需求:获得Person对象
interface PersonCreator{
	Person getPerson();
}

//*  需求:获得Person对象有参数的构造方法
interface PersonCreator2{
	Person getPerson(String name,int age);
}

3.案例3-遍历:

测试类

public class YiMoChaTest3{

	public static void main(String[] args) {
		
		List<Person> userList = Arrays.asList(
			new Person("周瑜", 18),
			new Person("诸葛亮", 23),
			new Person("刘备", 34), 
			new Person("关羽", 25), 
			new Person("张飞", 20),
			new Person("小乔", 16), 
			new Person("大乔", 18), 
			new Person("江小麦", 19));
		
		//* 方法的引用式遍历:
		userList.forEach(System.out::println);
		
	}
}

ps:

注意:
1.参数的数量和类型一定要和接口中定义的方法一致
2.返回值类型一定要和接口中定义的方法一致

4.案例4-集合排序:

public class YiMoChaTest4{
	//* 集合排序:
	//* ArrayList中有若干个Person对象

	public static void main(String[] args) {
		//* 需求将这个persons按年龄降序排列,
		List<Person> persons = Arrays.asList(
				new Person("小明", 15), 
				new Person("向红", 20),
				new Person("张珊", 19),
				new Person("袁亮",35),
				new Person("李敏",22),
				new Person("周盼",20),
				new Person("源鑫",16));
		
		persons.sort((o1,o2)->{
			return o1.getAge()-o2.getAge();
		});
		
		persons.forEach(System.out::println);
		
	}
}

5.案例5-集合排序TreeSet:

public class YiMoChaTest5{
	// * 集合排序:
	public static void main(String[] args) {
		// * 需求将这个persons按年龄降序排列,
		// TreeSet
		// 使用Lambda表达式来实现Comparator接口,并实例化一个TreeSet对象
		
		//TreeSet<Person> set = new TreeSet<Person>((o1, o2) -> o1.getAge() - o2.getAge()); // 如果年龄相同会有去重的效果
				
		TreeSet<Person> set = new TreeSet<Person>((o1, o2) ->{
			if (o1.getAge()>=o2.getAge()) {
				return -1;
			}else {
				return 1;
			}
		});

		set.add(new Person("小明", 15));
		set.add(new Person("向红", 20));
		set.add(new Person("张珊", 19));
		set.add(new Person("袁亮", 35));
		set.add(new Person("李敏", 23));
		set.add(new Person("周盼", 20));
		set.add(new Person("源鑫", 16));
		set.add(new Person("源丽", 16));

		set.forEach(System.out::println);
	}
}

6.案例6-集合排序forEach:

import java.util.ArrayList;
import java.util.Collections;

public class YiMoChaTest6{
	// * 集合排序:
	public static void main(String[] args) {
		
		ArrayList<Integer> list = new ArrayList<>();

		Collections.addAll(list, 1,2,3,4,5,6,7,8,9,0);
		
		// 将集合中的每一个方法都带入到accept中
		list.forEach(System.out::println);
	}
}

输出集合中所有的单数

import java.util.ArrayList;
import java.util.Collections;

public class YiMoChaTest6{
	// * 集合排序:
	public static void main(String[] args) {

		ArrayList<Integer> list = new ArrayList<>();

		Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

		// 输出集合中所有的单数
		list.forEach(ele -> { // 加入逻辑。。。
			if (ele % 2 != 0) {
				System.out.println(ele);
			}
		});
	}
}

7.案例7-集合removeIf:

import java.util.ArrayList;
import java.util.ListIterator;

import com.entity.Person;

public class YiMoChaTest7 {
	// * 删除集合中满足条件的元素:
	public static void main(String[] args) {

		ArrayList<Person> list = new ArrayList<>();

		list.add(new Person("小明", 15));
		list.add(new Person("向红", 20));
		list.add(new Person("张珊", 19));
		list.add(new Person("袁亮", 35));
		list.add(new Person("李敏", 23));
		list.add(new Person("周盼", 20));
		list.add(new Person("源鑫", 16));
		list.add(new Person("源丽", 16));

		// 删除集合中年龄大于20的
		// * 以前的写法
//		ListIterator<Person> ite = list.listIterator();
//		while (ite.hasNext()) {
//			Person eleP = ite.next();
//			if (eleP.getAge() > 20) {
//				ite.remove();
//			}
//		}

		// lambda 表达式写法
		// 将集合中的每一个元素都带入到test方法中,如果返回true,则删除这个元素
		list.removeIf(ele -> ele.getAge() > 20);

		list.forEach(System.out::println);

	}
}

8.案例8-线程:

public class ExecuteTest5 {
	// * 需求:开一条线程做输出
	public static void main(String[] args) {

		Thread t = new Thread(()->{
			for (int i = 0; i < 50; i++) {
				System.out.println(i);
			}
		});
		
		t.start();
	}
}
发布了47 篇原创文章 · 获赞 60 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43760328/article/details/93376322
今日推荐