lambda、方法引用、比较器、字符串、基本数据类型与封装类

JDK4大核心lambda接口
1、消费型接口Consumer void accept(T t)
2、供给型接口 Supplier T get()
3、函数型接口 Function<T,R> apply(T t)
4、断言型接口 Predicate boolean test(T t)

public class TestLambda04 {
	public static void main(String[] args) {
		shop(1000,(mon)->System.out.println("买零食花了"+mon));
		shop(1000,(mon)->System.out.println("买衣服花了"+mon));
		
		getStu(()->new Student());
		System.out.println(opeString("wo zai  shang hai ",s->s.trim()));
		ArrayList<String> str= new ArrayList();
		str.add("sss");
		str.add("evghdgc");
		System.out.println(getArray(str,str1->str1.length()>3));
	}
	
	//消费性 Consumer<T> void accept(T)
	public static void shop(int money,Consumer<Integer> con) {
		con.accept(money);
	}
	//供给型 Supplier<T> T get()
	public static Student getStu(Supplier<Student> su) {
		return su.get();
	}
	 //函数型Function<T,R> apply(T)
	public static String opeString(String str,Function<String,String> fun) {
		return fun.apply(str);
	}
	
	//断言型 Predicate<T> boolean test(T)
	public static ArrayList<String> getArray(ArrayList<String> list,Predicate<String> pre){
		ArrayList<String> res=new ArrayList();
		for(String str:list) {
			if(pre.test(str)) {
				res.add(str);
			}
		}
		return res;
	}
}

方法引用
简化了lambda表达式,若lambda体中功能已经有方法提供,可以使用方法引用去调用已实现的功能方法

1、对象引用::成员方法名
2、类名::静态方法名
3、类名::实例方法名
抽象方法的参数列表中,第一个参数是调用内部引用方法的调用者,第二个参数是引用方法的参数

public class Demo01 {
	public static void main(String[] args) {
		test01();
		test02();
		test03();
		test04();
	}
	//1、对象引用::成员方法名 条件:需要函数型接口中抽象方法的参数列表与返回值类型 与 引用的方法的参数列表与返回值类型一致
	public static void test01() {
		Function<String, String> fun1=str->str.toUpperCase();
		Consumer<String> con1=str->System.out.println(str);
		PrintStream ps = System.out;
		Consumer<String> con=ps::println;
		con.accept("今天是周一");
	}
	//2、类名.静态方法名
	public static void test02() {
		Comparator<Integer> com1=(a1,a2)->Math.max(a1, a2);
		Comparator<Integer> com2=Math::max;
		System.out.println(com1.compare(2, 3));
		System.out.println(com2.compare(34, 56));
	}
	//3、类名::实例方法名 抽象方法的参数列表中,第一个参数是调用内部引用方法的调用者,第二个参数是引用方法的参数
	public static void test03() {
		BiPredicate<String, String> bp1=(str1,str2)->str1.equals(str2);
		BiPredicate<String, String> bp2=String::equals;
		System.out.println(bp1.test("sdf", "sdf"));
		System.out.println(bp2.test("asd", "as"));
	}
}

构造器引用
构造器引用 类名::new

//构造器引用 类名::new
	public static void test04() {
		Supplier<Student> su1=()->new Student();
		Supplier<Student> su2=Student::new;
		System.out.println(su1.get());
		System.out.println(su2.get());
		
		Function<String, Student> fun1=(str)->new Student(str);
		System.out.println(fun1.apply("zhnasna"));
		BiFunction<Integer, Integer, Student> fun2=Student::new;
		System.out.println(fun2.apply(123, 23));
	}

比较器
1、使用Arrays.sort(),对基本数据类型的数组进行升序排序
2、比较引用类型数据时
(1)内部比较器:重写Comparable的compareTo进行自定义比较规则,但是这种方式代码耦合度较高,不利于代码维护和复用

@Override
	public int compareTo(Student o) {
		if(this.age==o.age) {
			return this.name.compareTo(o.name);
		}else{
			return this.age-o.age;
		}
	}

(2)外部比较器:实现Comparator的compare方法

public class CompareDemo01 {
	public static void main(String[] args) {
		int[] arr=new int[] {1,3,4,2,5,6,0};
		//存储Student类型的数据
		Student[] stu = new Student[3];
		stu[0]= new Student(121,"luka",23);
		stu[1]= new Student(122,"tammy",25);
		stu[2]= new Student(123,"alen",21);
		Arrays.sort(stu,new ComStudent());
		System.out.println(Arrays.toString(stu));
	}
}
class ComStudent implements Comparator<Student>{
	//内部比较器
	@Override
	public int compare(Student o1, Student o2) {
		return o1.getAge() - o2.getAge();
	}
}

String、StringBuffer、StringBuilder
共同点:都是字符序列
不同点:
1、String长度不可变,StringBuffer和StringBuilder长度可变
2、StringBuffer线程安全,但效率低;StringBuilder线程不安全,性能较高
3、StringBuffer多线程时使用;StringBuilder单线程且对安全要求不高时使用

基本数据类型与包装类
基本数据类型 包装类
byte Byte
short Short
char Character
int Integer
float Float
double Double
long Long
boolean Boolean
使用过程中基本数据类型与其对象的包装类有自动拆装箱操作
1、 Integer 与 new Integer 之间不存在拆装箱过程,两者是不会相等的
2、int 与 Integer、new Integer之间会自动拆装箱,所以只要数值相等就相等
3、两个new Integer是不会相等的,因为会创建对象,在堆中的内存地址不一样
4、两个Integer比较,数值在-128~127之间就相等,否则会自动为对象进行new Integer所以不相等

public class Demo01 {
	public static void main(String[] args) {
		int a = 127;
		Integer a1=127;
		Integer a2=new Integer(127);
		Integer a6=new Integer(127);
		Integer a4=128;
		Integer a5=128;
		System.out.println(a==a1);//true 自动拆箱
		System.out.println(a==a2);//true 自动拆箱
		System.out.println(a1==a2);//false 
		System.out.println(a2==a6);//false
		System.out.println(a4==a5);//false 数值超过
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值