java8中的22个lambda表达式用法入门

/**
 * lambda表达式简明扼要的以流水线的方式云处理集合内的数据,可以很轻松的完成过滤,分组,收集、归约这类操作。
 * 总得来说,lambda的操作分为两类,中间型和最终型。
 * 
 */
public class LambdaExpression {

	public static void main(String[] args) {
        
		//中间型操作
		filter(Lists.newArrayList("frist", "second", "third"));
		map(Lists.newArrayList(1, 2, 3));
		peek(Lists.newArrayList("a", "b", "c"));
		flatMap(Lists.newArrayList("hell", "word"));
		limit(Lists.newArrayList("hello", "world", "good", "boy"));
		skip(Lists.newArrayList("hello", "world", "good", "boy"));
		concat();
		distinct(Lists.newArrayList("1", "2", "3", "2", "4", "5", "5"));
		sorted(Lists.newArrayList("1", "5", "3", "7", "4", "9", "2"));
		//test();
		
		//最终型操作
		
		count(Lists.newArrayList("1", "3", "5", "6"));
		max(Lists.newArrayList("1", "3", "5", "10"));
		min(Lists.newArrayList("4", "3", "5", "6"));
		findFirst(Lists.newArrayList("10", "3", "5", "6"));
		findAny();
		anyMatch(Lists.newArrayList("10", "3", "5", "6"));
		allMatch(Lists.newArrayList("10", "3", "5", "6", "3"));
		noneMatch(Lists.newArrayList("10", "3", "5", "6", "3"));
		collect(Lists.newArrayList("10", "3", "5", "6", "3"));
		reduce();
		toArray(Lists.newArrayList("10", "3", "5", "6", "3"));
		lterator(Lists.newArrayList("10", "3", "5", "6", "3"));
		foreach(Lists.newArrayList("10", "3", "5", "6", "3"));
	}

	private static void test(){
		ArrayList<String> dataList = Lists.newArrayList("hello", "world", "good", "boy");
		List<String> transform = dataList.stream().flatMap(data -> Arrays.stream(data.split(""))).distinct().collect(Collectors.toList());
		System.out.println(JSON.toJSONString(transform));
	}
	
	// 1, filter
	// filter会按照指定的条件匹配出符合要求的元素,并返回一个新的stream流,可以配合其他中间型或最终型API使用.
	// 示例:
	public static void filter(List<String> dataList) {
		List<String> filter = dataList.stream()
				.filter(
						// 筛选出值为second的匹配项
						data -> data.equals("second"))
				.collect(Collectors.toList());
		System.out.println(filter);
	}

	// 2, map
	// map可以将一个对象转换为另一个对象,并返回一个新的stream流,比如将一个integer对象转换为String对象。
	// 示例:
	public static void map(List<Integer> dataList) {
		List<String> transform = dataList.stream()
				.map(
						// 也可以写成String::valueOf
						data -> String.valueOf(data))
				.collect(Collectors.toList());
		System.out.println(transform);
	}

	// 3, peek
	// peek会对流中的元素逐个遍历处理, 它与map的区别在于: map一般用于对象的转换, peek用于对象的消费,即不改变元素本身的类型。
	// 示例
	public static void peek(List<String> dataList) {
		List<String> transform = dataList.stream()
				// peek是对元素逐一消费,在这里程序会逐个打印a, b, c
				.peek(data -> System.out.println(data))
				// map是对元素进行转换,这里将其转写成大写A, B, C
				.map(data -> data.toUpperCase()).collect(Collectors.toList());
		System.out.println(transform);
	}

	// 4, flatMap
	// flatMap可以将已有的对象转化为另一个对象,它是一个一对多的逻辑。它与map的区别在于:
	// map是一对一的,即将一个对象转换为别一个对象,而flatMap是一对多的,即将一个对象拆分为多个对象。
	public static void flatMap(List<String> dataList) {
		List<String> transform = dataList.stream().flatMap(data -> Arrays.stream(data.split("")))
				.collect(Collectors.toList());
		System.out.println(transform);
	}

	// 5, limit
	// 就相当于sql中的limit, 可以指定保留前N的元素。
	public static void limit(List<String> dataList) {
		List<String> transform = dataList.stream().limit(2).collect(Collectors.toList());
		System.out.println(transform);
	}

	// 6, skip
	// 作用与limit相反, 会抛弃前N的元素
	public static void skip(List<String> dataList) {
		List<String> transform = dataList.stream().skip(2).collect(Collectors.toList());
		System.out.println(transform);
	}

	// 7, concat
	// 可以将多个流的数据合并为一个流
	public static void concat() {
		List<String> transform = Stream.concat(Stream.of("hello"), Stream.of("world")).collect(Collectors.toList());
		System.out.println(transform);
	}
	
	//8, distinct
	//用于对流中的元素去重
	public static void distinct(ArrayList<String> dataList){
		List<String> transform = dataList.stream().distinct().collect(Collectors.toList());
		System.out.println(transform);
	}
	
	//9, sorted
	//用于对流中的数据排序
	public static void sorted(ArrayList<String> dataList){
		List<String> transform = dataList.stream().sorted(
				Comparator.comparingInt(Integer :: parseInt)).collect(Collectors.toList());
		System.out.println(transform);
	}
	
	
	//10, count
	//统计元素的个数,不会自动去重
    public static void count(ArrayList<String> dataList) {
    	long count = dataList.stream().count();
    	System.out.println(count);
    }	
	
    //11, max
    //匹配元素最大值并返回
    public static void max(ArrayList<String> dataList) {
    	String max = dataList.stream().max(
    			Comparator.comparing(Integer::parseInt)).get();
    	System.out.println(max);
    }	
	
    //12, min
    //匹配元素最小值并返回
    public static void min(ArrayList<String> dataList) {
    	String min = dataList.stream().min(
    			Comparator.comparing(Integer::parseInt)).get();;
    	System.out.println(min);
    }
    
    //13, findFirst
    //找到第一匹配的元素后立即返回
    public static void findFirst(ArrayList<String> dataList) {
    	String first = dataList.stream().findFirst().get();
    	System.out.println(first);
    }
    
    //14, findAny
    //找到任何匹配的元素就返回。如果用在一个串行流中,跟findFirst效果一样。如果用在并行流中,就会比较高效。
    public static void findAny() {
        int asInt = IntStream.range(1, 10).parallel().findAny().getAsInt();
    	System.out.println(asInt);
    }
    
    //15, anyMatch
    //用于判断是否符合匹配条件的元素
    public static void anyMatch(ArrayList<String> dataList){
    	boolean match = dataList.stream().anyMatch(
    			data -> data.equals("3"));
    	System.out.println(match);
    }
    
    //16, allMatch
    //用于判断是否所有元素都符合匹配条件。
    public static void allMatch(ArrayList<String> dataList){
    	boolean match = dataList.stream().allMatch(
    			data -> data.equals("3"));
    	System.out.println(match);
    }
    

    //17, noneMatch
    //用于判断是否所有元素都不符合匹配条件
    public static void noneMatch(ArrayList<String> dataList){
    	boolean match = dataList.stream().noneMatch(
    			data -> data.equals("3"));
    	System.out.println(match);
    }
	
    //18, collect
    //将流转换为指定的类型, 比如List转换为Set
    public static void collect(ArrayList<String> dataList) {
    	Set<String> collect = dataList.stream().collect(Collectors.toSet());
    	System.out.println(collect);
    }
	
    //19, reduce
    //将元素合并起来,得到一个新值
    public static void reduce(){
    	int reduce = IntStream.range(0,  10).reduce(0,  (v1, v2) -> v1 + v2);
    	System.out.println(reduce);
    }
	
	//20, toArray
    public static void toArray(ArrayList<String> dataList){
    	Object[] collect = dataList.stream().toArray();
    	System.out.println(JSON.toJSONString(collect));
    }
    
    //21, lterator
    //将流转换为一个迭代器
    public static void lterator(ArrayList<String> dataList){
    	Iterator<String> iterator = dataList.stream().iterator();
    	while(iterator.hasNext())
    	{
    		System.out.println(iterator.next());
    	}
    }
    
    //22, foreach
    //对流中的元素逐个遍历
    public static void foreach(ArrayList<String> dataList){
    	dataList.stream().forEach(
    			data -> System.out.println(data));
    	
    	//等同
    	dataList.forEach(data -> System.out.println(data));
    }

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值