Lambda理解,以及聚合操作的笔记
Lambda理解
1.从实现类到Lambda
1.使用实现类,创建对象后调用方法。
package lambda;
public class IMLambda {
public static void main(String[] args) {
ILike test = new Like();
test.lambda();
}
}
interface ILike{
void lambda();
}
class Like implements ILike{
@Override
public void lambda(){
System.out.println("I Like Lambda");
}
}
2.使用静态内部类
package lambda;
public class IMLambda {
static class Like implements ILike{
@Override
public void lambda(){
System.out.println("I Like Lambda");
}
}
public static void main(String[] args) {
ILike test = new Like();
test.lambda();
}
}
interface ILike{
void lambda();
}
3.使用局部内部类
package lambda;
public class IMLambda {
public static void main(String[] args) {
class Like implements ILike{
@Override
public void lambda(){
System.out.println("I Like Lambda");
}
}
ILike like = new Like();
like.lambda();
}
}
interface ILike{
void lambda();
}
4.使用匿名内部类
package lambda;
public class IMLambda {
public static void main(String[] args) {
ILike like = new ILike() {
@Override
public void lambda(){
System.out.println("i like lambda");
}
};
like.lambda();
}
}
interface ILike{
void lambda();
}
5.使用Lambda
package lambda;
public class IMLambda {
public static void main(String[] args) {
ILike like = () -> System.out.println("i like lambda");
like.lambda();
}
}
interface ILike{
void lambda();
}
2.总结
Lambda表达式只能有一行代码的情况下才能简化为一行,如果有多行,那么就用代码块包裹{}
Lambda表达式的要求实现方法所属接口为函数式接口,即接口内只有一个抽象方法
多个参数也可以去掉参数类型,要去掉就都去掉,且必须加上括号
聚合操作
1.引子
传统方式与聚合操作方式遍历数据对比
Hero.java
package lambda;
public class Hero implements Comparable<Hero>{
public String name;
public double hp;
public int damage;
public Hero(){}
public Hero(String name){
this.name = name;
}
public Hero(String name, double hp, int damage){
this.name = name;
this.hp = hp;
this.damage = damage;
}
@Override
public int compareTo(Hero anh){
if(damage < anh.damage)
return 1;
return -1;
}
@Override
public String toString() {
return "Hero [name=" + name + ", hp=" + hp + ", damage=" + damage + "]\n";
}
public boolean matched(){
return this.hp>100 && this.damage<50;
}
}
Unite.java
其中filter方法是来自Java Stream API
package lambda;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Unite {
public static void main(String[] args) {
Random r = new Random();
List<Hero> hs = new ArrayList<Hero>();
for(int i = 0; i < 10; i++)
hs.add(new Hero("hero" + i, r.nextInt(1000), r.nextInt(100)));
System.out.println("初始化后的集合:");
System.out.println(hs);
System.out.println("查询条件:hp>100 && damage<50");
System.out.println("通过传统操作方式找出满足条件的数据:");
for (Hero h : hs) {
if (h.hp > 100 && h.damage < 50)
System.out.println(h.name);
}
System.out.println("通过聚合操作方式找出满足条件的数据:");
hs
.stream()
.filter(h -> h.hp > 100 && h.hp < 200)
.forEach(h -> System.out.println(h.hp));
}
}
2.管道源
Collections(heros.stream)可以切换成管道,而数组不行。
此处heros为List<Hero>!!!
数组需使用
Arrays.stream(hs)
或者
Stream.of(hs)
3.中间操作
每个中间操作,又会返回一个Stream,比如.filter()又返回一个Stream, 中间操作是“懒”操作,并不会真正进行遍历。
中间操作比较多,主要分两类
对元素进行筛选 和 转换为其他形式的流
对元素进行筛选:
filter 匹配
distinct 去除重复(根据equals判断)
sorted 自然排序
sorted(Comparator<T>) 指定排序
limit 保留
skip 忽略
转换为其他形式的流
mapToDouble 转换为double的流
map 转换为任意类型的流
以上方法皆来自Java Stream API
//https://how2j.cn/k/lambda/lambda-stream/700.html#nowhere
package lambda;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import charactor.Hero;
public class TestAggregate {
public static void main(String[] args) {
Random r = new Random();
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i, r.nextInt(1000), r.nextInt(100)));
}
//制造一个重复数据
heros.add(heros.get(0));
System.out.println("初始化集合后的数据 (最后一个数据重复):");
System.out.println(heros);
System.out.println("满足条件hp>100&&damage<50的数据");
heros
.stream()
.filter(h->h.hp>100&&h.damage<50)
.forEach(h->System.out.print(h));
System.out.println("去除重复的数据,去除标准是看equals");
heros
.stream()
.distinct()
.forEach(h->System.out.print(h));
System.out.println("按照血量排序");
heros
.stream()
.sorted((h1,h2)->h1.hp>=h2.hp?1:-1)
.forEach(h->System.out.print(h));
System.out.println("保留3个");
heros
.stream()
.limit(3)
.forEach(h->System.out.print(h));
System.out.println("忽略前3个");
heros
.stream()
.skip(3)
.forEach(h->System.out.print(h));
System.out.println("转换为double的Stream");
heros
.stream()
.mapToDouble(Hero::getHp)
.forEach(h->System.out.println(h));
System.out.println("转换任意类型的Stream");
heros
.stream()
.map((h)-> h.name + " - " + h.hp + " - " + h.damage)
.forEach(h->System.out.println(h));
}
}
/*
package charactor;
public class Hero implements Comparable<Hero>{
public String name;
public float hp;
public int damage;
public Hero(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getHp() {
return hp;
}
public void setHp(float hp) {
this.hp = hp;
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public Hero(String name) {
this.name =name;
}
//初始化name,hp,damage的构造方法
public Hero(String name,float hp, int damage) {
this.name =name;
this.hp = hp;
this.damage = damage;
}
@Override
public int compareTo(Hero anotherHero) {
if(damage<anotherHero.damage)
return 1;
else
return -1;
}
@Override
public String toString() {
return "Hero [name=" + name + ", hp=" + hp + ", damage=" + damage + "]\r\n";
}
}
*/
4.结束操作
在"流"最后被用光后,也就是在进行结束操作后,结束操作才真正进行遍历行为,前面的中间操作也在这个时候,才真正的执行。
forEach() 遍历每个元素
toArray() 转换为数组
min(Comparator<T>) 取最小的元素
max(Comparator<T>) 取最大的元素
count() 总数
findFirst() 第一个元素
package lambda;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import charactor.Hero;
public class TestAggregate {
public static void main(String[] args) {
Random r = new Random();
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i, r.nextInt(1000), r.nextInt(100)));
}
System.out.println("遍历集合中的每个数据");
heros
.stream()
.forEach(h->System.out.print(h));
System.out.println("返回一个数组");
Object[] hs= heros
.stream()
.toArray();
System.out.println(Arrays.toString(hs));
System.out.println("返回伤害最低的那个英雄");
Hero minDamageHero =
heros
.stream()
.min((h1,h2)->h1.damage-h2.damage)
.get();
System.out.print(minDamageHero);
System.out.println("返回伤害最高的那个英雄");
Hero mxnDamageHero =
heros
.stream()
.max((h1,h2)->h1.damage-h2.damage)
.get();
System.out.print(mxnDamageHero);
System.out.println("流中数据的总数");
long count = heros
.stream()
.count();
System.out.println(count);
System.out.println("第一个英雄");
Hero firstHero =
heros
.stream()
.findFirst()
.get();
System.out.println(firstHero);
}
}
5.小练习
Source lambdaTest.java
package lambda;
import java.util.*;
public class lambdaTest {
public static void main(String[] args) {
List<Hero> hs = new ArrayList<Hero>();
Random random = new Random();
for (int i = 0; i < 10; i++) {
Hero hero = new Hero("Hero" + i, random.nextInt(100), random.nextInt(100));
hs.add(hero);
}
Collections.sort(hs, (h1, h2) -> Double.compare(h2.hp,h1.hp));
for (Hero hero : hs) {
System.out.println(hero);
}
System.out.println("传统排序:");
System.out.println(hs.get(2).name);
System.out.println("聚合操作:");
Optional<Hero> res = hs
.stream()
.sorted((h1, h2) -> Double.compare(h2.hp, h1.hp)) // 从大到小
.skip(2) // 跳过前两个
.findFirst(); // 取第三个
/*
hs
.stream()
.sorted((h1, h2) -> Double.compare(h2.hp, h1.hp))
.limit(3)
.skip(2)
.forEach(h->System.out.println(h.name));
*/
res.ifPresent(h -> System.out.println(h.name));
}
}