JDK1.8 lambda

一篇好文章推荐:http://blog.csdn.net/dm_vincent/article/details/40340291


package Demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Arraysort {

	public static void main(String[] args) {
		//		for (int i = 0; i < 10; i++) {
		//			System.out.println("huo:"+i);
		//		}
		Integer[] players = {1,2,3,4,5,6,7};	

		List<String> friends = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");
		final List<String> comrades = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");
		final List<String> editors = Arrays.asList("Brian", "Nate", "Neal", "Raju", "Sara", "Scott");


		friends.forEach(name -> System.out.println(name));
		friends.forEach(System.out::println);
		for (String string : friends) {
			System.out.println(string);
		}

		//lambda  list赋值
		List<String> uppercaseNames = new ArrayList<String>();
		friends.forEach(name -> uppercaseNames.add(name.toUpperCase()));
		System.out.println(uppercaseNames);


		//lambda
		friends.stream()
		.map(name -> name.toUpperCase())
		.forEach(name -> System.out.print(name + " "));


		friends.stream()
		.map(name -> name.length())
		.forEach(count -> System.out.print(count + " "));


		friends.stream()
		.map(String::toUpperCase)
		.forEach(name -> System.out.println(name));


		/**
		 * 将所有大头字母是N的 添加到 startsWithN 中
		 */
		final List<String> startsWithN = new ArrayList<String>();
		for(String name : friends) {
			if(name.startsWith("N")) {
				startsWithN.add(name);
			}
		}


		final long countFriendsStartN = friends.stream().filter(checkIfStartsWith("N")).count();
		final long countFriendsStartB = friends.stream().filter(checkIfStartsWith("B")).count();

		comrades.stream().filter(checkIfStartsWith("N")).count();
		comrades.stream().findFirst();



		//1.8JDK 新引入StringJoiner类 
		String StringJoiner = String.join(",", comrades);
		System.out.println(StringJoiner);
		System.out.println(friends.stream().map(String::toUpperCase).collect(Collectors.joining(", ")));





		System.out.println("Total number of characters in all names: " +
				friends.stream()
		.mapToInt(name -> name.length())
		.sum());




		//		System.out.println(friends.stream().mapToDouble(name -> name.length()).sum());
	}

	public static Predicate<String> checkIfStartsWith(String letter) {
		return name -> name.startsWith(letter);
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值