Java学习-第24天

package com._01_Lambda;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 排序
 * 
 * @author SEC90
 * @Date 2022年2月10日 上午10:27:26
 */
public class Lambda_01 {
	public static void main(String[] args) {
		Integer[] arr = { 1, 5, 9, 7, 3, 6, 54 };
		// asList : 把数组 转换为List集合
		List<Integer> integers = Arrays.asList(arr);
		// 匿名内部类写法
		Collections.sort(integers, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2 - o1;
			}
		});
		// lambda
		Collections.sort(integers, (i1, i2) -> i2 - i1);
		System.out.println(integers);
	}
}
package com._01_Lambda;

/**
 * 回调函数 : 简单来说,就是写好功能,通过参数传递过去,让别人帮忙调用
 * 
 * @author SEC90
 * @Date 2022年2月10日 上午10:40:40
 */
public class Lambda_02 {
	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4 };
//		forEach(arr,new A());
		
//		forEach(arr,new Array() {
//			@Override
//			public void m1(int i) {
//				System.out.println(i);
//			}
//		});
		forEach(arr,i->System.out.println(i));
	}

	public static void forEach(int[] arr,Array array) {
		for (int i : arr) {
			array.m1(i);
		}
	}
}
interface Array{
	void m1(int i);
}
class A implements Array{
	@Override
	public void m1(int i) {
		System.out.println(i);
	}
	
}
package com._01_Lambda;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

/**
 * 遍历
 * 
 * @author SEC90
 * @Date 2022年2月10日 上午10:52:19
 */
public class Lambda_03 {
	public static void main(String[] args) {
		Integer[] arr = { 1, 5, 9, 7, 3, 6, 54 };
		// asList : 把数组 转换为List集合
		List<Integer> integers = Arrays.asList(arr);
		
		// 匿名内部类
		integers.forEach(new Consumer<Integer>() {
			@Override
			public void accept(Integer t) {
				System.out.println(t);
			}
		});
		// lambda
		integers.forEach(x->System.out.println(x));
	}
}
package com._02_FunInterface;

import java.util.function.Supplier;

/**
 * Supplier 接口 有一个get方法 用于获取数据
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午2:11:42
 */
public class FunInterface_01 {
	public static int sum(Supplier<Integer> sup){
		return sup.get();
	}
	public static void main(String[] args) {
		int result = sum(()->1+2);
		System.out.println(result);
	}
}



package com._02_FunInterface;

import java.util.function.Consumer;

/**
 * Consumer void accept(T t); 有参 无返回值
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午2:15:21
 */
public class FunInterface_02 {
	public static void print(Consumer<String> con, String msg) {
		con.accept(msg);
	}

	public static void main(String[] args) {
		print((str) -> System.out.println(str), "你好吗");
	}
}
package com._03_FunCall;

import java.util.function.Supplier;

/**
 * 对象引用::成员方法
 * 
 * 要求 : 使用接口的抽象方法的入参和出参 必须和调用方法的入参出参一致
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午2:19:10
 */
public class FunCall_01 {
	public static void main(String[] args) {
		Integer i1 = 10;
		// 方法调用
		String str = i1.toString();
		System.out.println(str);
		// lambda表达式写法
		Supplier<String> su = () -> i1.toString();
		System.out.println(su.get());
		// 方法引用写法
		Supplier<String> su1 = i1::toString;
		System.out.println(su1.get());
	}
}



package com._03_FunCall;

import java.util.function.BiFunction;


/**
 * 类名::静态方法名
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午2:24:40
 */
public class FunCall_02 {
	public static void main(String[] args) {
		int max = Integer.max(10, 33);
		BiFunction<Integer, Integer, Integer> bf = Integer::max;
		System.out.println(bf.apply(10, 33));
	}
}



package com._03_FunCall;

import java.util.function.BiPredicate;
import java.util.function.Predicate;

/**
 * 类名::成员方法
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午2:27:28
 */
public class FunCall_03 {
	public static void main(String[] args) {
		BiPredicate<String, String> bp = String::equals;
		System.out.println(bp.test("asd", "asd"));
	}
}


package com._03_FunCall;

import java.util.function.Supplier;

/**
 * 创建对象
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午2:30:42
 */
public class FunCall_04 {
	public static void main(String[] args) {
		Object o1 = new Object();
		Supplier<Object> sp = Object::new;
		System.out.println(sp.get());
	}
}

package com._03_FunCall;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

public class FunCall_05 {
	public static void main(String[] args) {
		Integer[] is = new Integer[10];
		Function<Integer, Integer[]> fun = Integer[]::new;
		is=fun.apply(5);
		System.out.println(is.length);
		is[0]=100;
		BiFunction<Integer[],Integer,Integer> bc = (arr,index)->arr[index];
		System.out.println(bc.apply(is, 0));
	}
}






package com._04_Stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream对象创建
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午3:05:24
 */
public class Stream_01 {
	public static void main(String[] args) {
		// 1 数组 , 通过stream类的of方法即可
		String[] strings = { "a", "b", "c", "d" };
		Stream<String> stream1 = Stream.of(strings);

		// 2 集合 通过集合对象的stream方法即可
		List<String> strings2 = Arrays.asList(strings);
		Stream<String> stream2 = strings2.stream();

		// 3 通过 Stream的generate方法创建无限流
		// 该流无限大,建议使用limit限制条数
		// 参数是一个Supplier接口,有一个get方法,无参,有返回值
		// 返回的数据就会被放到这个无限流中,也就是,目前这个流中的数据都是1
		Stream<Integer> stream3 = Stream.generate(() -> 1);
		stream3.limit(10).forEach(x -> System.out.println(x));

		// 4 通过Stream.iterate方法创建无限流
		// 第一个参数是起始值,第二个参数 是一个UnaryOperator,是function的子类,所以是有参有返回值
		// x->x+2 : 等于步长为2, 那么此时 数据流中的内容为 1,3,5,7,9,11.....
		Stream<Integer> stream4 = Stream.iterate(1, x -> x + 2);
		stream4.limit(5).forEach(x -> System.out.println(x));

		// 5 已有类的streamAPI
		String string = "asdh";
		IntStream is = string.chars();
		is.forEach(x -> System.out.println(x));
	}
}

package com._04_Stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 中间操作 又称为转换算子,对数据源进行处理
 * 
 * 一个流可以有N个中间操作,每一个中间操作都会返回一个新的流,方便下一个操作使用,可以做到链式调用
 * 
 * 但是一个流只能有一个终止操作
 * 
 * 如果不执行终止操作,中间操作也不会执行
 * 
 * filter : 对元素进行筛选,不符合条件的就不要了
 * 
 * distinct : 去掉重复的元素
 * 
 * limit : 取一个数据集合的前几条数据
 * 
 * skip : 跳过多少元素
 * 
 * sorted : 排序
 * 
 * map : 在遍历集合的过程中对数据进行操作
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午3:18:36
 */
public class Stream_02 {
	public static void main(String[] args) {
		List<String> strings = Arrays.asList("a", "b", "c", "d", "a");
		/**
		 * filter : 对元素进行过滤,不符合条件的就不要了
		 */
		Stream<String> stream = strings.stream();
		// collect : 收集器,把流转换为集合
		// x -> !x.equals("a") 只要不是a的
		List<String> result = stream.filter(x -> !x.equals("a")).collect(
				Collectors.toList());
		System.out.println(result);
		// 流一旦使用过,必须重新生成,否则报错,所以每个中间操作都是返回一个新的流,因为原来的流就不能用了
		// java.lang.IllegalStateException: stream has already been operated
		// upon or closed
		// stream.forEach(x->System.out.println(x));

		/**
		 * distinct 去除重复
		 */
		stream = strings.stream();
		result = stream.distinct().collect(Collectors.toList());
		System.out.println(result);
		/**
		 * map
		 */
		List<Integer> says = Arrays.asList(2000, 1000, 2800, 6666, 8888);
		Stream<Integer> stream1 = says.stream();
		// 所有人涨薪百分之十
		says = stream1.map(x -> x + x / 100 * 10).collect(Collectors.toList());
		System.out.println(says);

		/**
		 * sorted 排序
		 */
		stream1 = says.stream();
		// 默认是升序 [1100, 2200, 3080, 7326, 9768]
		// says = stream1.sorted().collect(Collectors.toList());
		// 降序 [9768, 7326, 3080, 2200, 1100]
		says = stream1.sorted((x, y) -> y - x).collect(Collectors.toList());
		System.out.println(says);
	}
}

package com._04_Stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * 终止操作 又称为动作算子
 * 
 * forEach 循环遍历
 * 
 * collect 收集器
 * 
 * 计算相关 min,max,count,avrage
 * 
 * 匹配相关 anyMatch,allMatch...
 * 
 * @author SEC90
 * @Date 2022年2月10日 下午4:04:03
 */
public class Stream_03 {
	public static void main(String[] args) {
		List<String> strings = Arrays.asList("a", "b", "c","a");
		// forEach
		Stream<String> stream = strings.stream();
		stream.forEach(x -> System.out.println(x));

		// 统计
		stream = strings.stream();
		// 统计元素个数,一般需要和中间操作结合使用,否则还不如使用集合的size方法呢
		long count = stream.count();
		System.out.println(count);

		// 比如 统计有多少个a
		stream = strings.stream();
		// 统计元素个数,一般需要和中间操作结合使用,否则还不如使用集合的size方法呢
		count = stream.filter(x->x.equals("a")).count();
		System.out.println(count);
		
		// 最大值 max  最小值 min
		List<Integer> list = Arrays.asList(1,5,9,7,6,3);
		Stream<Integer> stream1 = list.stream();
		int max =stream1.max((x,y)->x-y).get();
		System.out.println(max);
		
		// 匹配相关  anyMatch 匹配数据 比如是否包含
		stream1 = list.stream();
		// 这种需求 contains就能解决,但是也有contains解决不了的
		boolean result =stream1.anyMatch(x->x==5);
		System.out.println(result);
		
		// 比如 所有学生中,判断是否有19岁的
		List<Student> students = new ArrayList<Student>();
		students.add(new Student("张三", 20));
		students.add(new Student("李四", 21));
		students.add(new Student("王五", 20));
		students.add(new Student("赵六", 29));
		Stream<Student> stream2 = students.stream();
		result = stream2.anyMatch(s->s.age==19);
		System.out.println(result);
	}
}
class Student{
	String name;
	int age;
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值