java8 lambda 数组_java8 array、list操作 汇【4】)- Java8 Lambda表达式 函数式编程【思想】...

int tmp1 = 1; //包围类的成员变量

static int tmp2 = 2; //包围类的静态成员变量//https://blog.csdn.net/chengwangbaiko/article/details/73477551 https://www.cnblogs.com/newflydd/p/4948575.html

public static void main(String[] args) throwsException {

List list = Arrays.asList(100, 200, 300, 400, 500);

list.forEach(o-> {System.out.println(o);}); //forEach函数实现内部迭代

System.out.println("1、给出一个String类型的数组,找出其中所有不重复的素数:");

distinctPrimary("2", "3", "4", "5", "6", "7", "8", "9", "2", "3", "4", "5", "6", "7", "8", "9");

System.out.println("2、给出一个String类型的数组,找出其中各个素数,并统计其出现次数:");

primaryOccurrence("2", "3", "4", "5", "6", "7", "8", "9", "2", "3", "4", "5", "6", "7", "8", "9");

System.out.println("3、给出一个String类型的数组,求其中所有不重复素数的和(预定义的reduce操作: 如sum(),max(),min()等。):");

distinctPrimarySum("2", "3", "4", "5", "6", "7", "8", "9", "2", "3", "4", "5", "6", "7", "8", "9");

distinctPrimarySum2("2", "3", "4", "5", "6", "7", "8", "9", "2", "3", "4", "5", "6", "7", "8", "9");

System.out.println("4、生成器函数(Generator function):");

Stream.generate(Math::random).limit(5).forEach(System.out::println);

System.out.println("5、//3.1、λ表达式的更多用法:");//嵌套的λ表达式

Callable c1 = () -> () -> { System.out.println("Nested lambda"); };

c1.call().run();//用在条件表达式中

Callable c2 = true ? (() -> 42) : (() -> 24);

System.out.println(c2.call());//定义一个递归函数//private UnaryOperator factorial = i -> { return i == 0 ? 1 : i * factorial.apply( i - 1 ); };// //...//System.out.println(factorial.apply(3));

System.out.println("6、方法引用(Method reference):");/*Integer::parseInt //静态方法引用

System.out::print //实例方法引用

Person::new //构造器引用

super::toString //引用某个对象的父类方法

String[]::new //引用一个数组的构造器

//c1 与 c2 是一样的(静态方法引用)

Comparator c2 = (x, y) -> Integer.compare(x, y);

Comparator c1 = Integer::compare;

List persons = new ArrayList<>();

//下面两句是一样的(实例方法引用1)

persons.forEach(e -> System.out.println(e));

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

//下面两句是一样的(实例方法引用2)

persons.forEach(person -> person.eat());

persons.forEach(Person::eat);

//下面两句是一样的(构造器引用)

strList.stream().map(s -> new Integer(s));

strList.stream().map(Integer::new);*/}//5、捕获(Capture)

/*public void testCapture() {

int tmp3 = 3; //没有声明为final,但是effectively final的本地变量

final int tmp4 = 4; //声明为final的本地变量

int tmp5 = 5; //普通本地变量

Function f1 = i -> i + tmp1;

Function f2 = i -> i + tmp2;

Function f3 = i -> i + tmp3;

Function f4 = i -> i + tmp4;

Function f5 = i -> {

tmp5 += i; // 编译错!对tmp5赋值导致它不是effectively final的

return tmp5;

};

//...

tmp5 = 9; // 编译错!对tmp5赋值导致它不是effectively final的

}*/

//1、给出一个String类型的数组,找出其中所有不重复的素数

/*** 你可能会觉得在这个例子里,List list被迭代了好多次,

* map,filter,distinct都分别是一次循环,效率会不好。

* 实际并非如此。这些返回另一个Stream的方法都是“lazy”的,而最后返回最终结果的collect方法则是“eager”的。

* 在遇到eager方法之前,lazy的方法不会执行。

*

* 当遇到eager方法时,前面的lazy方法才会被依次执行。

* 而且是管道贯通式执行。这意味着每一个元素依次通过这些管道。

* 例如有个元素“3”,首先它被map成整数型3;

* 然后通过filter,发现是素数,被保留下来;又通过distinct,

* 如果已经有一个3了,那么就直接丢弃,如果还没有则保留。这样,3个操作其实只经过了一次循环。

*

* 除collect外其它的eager操作还有forEach,toArray,reduce等*/

public static voiddistinctPrimary(String... numbers) {

List l =Arrays.asList(numbers);

List r =l.stream()

.map(e-> newInteger(e))//.map(e -> Integer.parseInt(e))//将集合流中的元素一一映射为一个新的元素,并生成到新的输出流中

.filter(e -> isPrime(e)) //过滤,lambda表达式 .filter(e -> Primes.isPrime(e))

.distinct() //stream的高级方法,去除重复

.collect(Collectors.toList()); //将整理好的输出流收集到新的listInt集合中

System.out.println("distinctPrimary result is: " +r);

}//2、给出一个String类型的数组,找出其中各个素数,并统计其出现次数

public static voidprimaryOccurrence(String... numbers) {

List l =Arrays.asList(numbers);

Map r =l.stream()

.map(e-> newInteger(e))//.filter(e -> Primes.isPrime(e))

.filter(e ->isPrime(e))

.collect( Collectors.groupingBy(p->p, Collectors.summingInt(p->1)) ); //把结果收集到一个Map中,用统计到的各个素数自身作为键,其出现次数作为值。

System.out.println("primaryOccurrence result is: " +r);

}//3、给出一个String类型的数组,求其中所有不重复素数的和(预定义的reduce操作: 如sum(),max(),min()等。)

public static voiddistinctPrimarySum(String... numbers) {

List l =Arrays.asList(numbers);int sum =l.stream()

.map(e-> newInteger(e))//.filter(e -> Primes.isPrime(e))

.filter(e ->isPrime(e))

.distinct()

.reduce(0, (x,y) -> x+y); //equivalent to .sum() reduce方法用来产生单一的一个最终结果。

System.out.println("distinctPrimarySum result is: " +sum);

}public static voiddistinctPrimarySum2(String... numbers) {

List l =Arrays.asList(numbers);int sum = l.stream().map(Integer::new)//.filter(Primes::isPrime)

.filter(e ->isPrime(e))

.distinct()

.mapToInt(item->item)

.sum()

;

System.out.println("distinctPrimarySum2 result is: " +sum);

}//3.1、统计年龄在25-35岁的男女人数、比例 (预定义的reduce操作)

public void boysAndGirls(Listpersons) {

Map result = persons.parallelStream().filter(p -> p.getAge()>=25 && p.getAge()<=35).

collect(

Collectors.groupingBy(p->p.getSex(), Collectors.summingInt(p->1))

);

System.out.print("boysAndGirls result is " +result);

System.out.println(", ratio (male : female) is " + (float)result.get(UserT.Sex.MALE)/result.get(UserT.Sex.FEMAILE));

}static classUserT {public enumSex {

MALE("男"),

FEMAILE("女");privateString description;

Sex(String description) {this.description =description;

}publicString getDescription() {returndescription;

}public voidsetDescription(String description) {this.description =description;

}public staticSex fromName(String name) {for(Sex model : Sex.values()) {if(StringUtils.equals(model.name(), name)) {returnmodel;

}

}return null;

}

}

String name;

Integer age;

Integer sex;publicInteger getSex() {returnsex;

}public voidsetSex(Integer sex) {this.sex =sex;

}publicInteger getAge() {returnage;

}public voidsetAge(Integer age) {this.age =age;

}publicUserT(String zm) {this.name=zm;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值