lamda表达式之集合类操作API

lamda表达式的概念

1、lamda表达式由参数、->、表达式或语句块三部分组成。java类中有匿名类,而lamda表达式的本质相当于匿名方法
普通方法:

public int add(int x,int y){
	return x+y;
}

转换成lamda表达式:

(int x,int y)->x+y

2、捕获
lamda表达式可以使用表达式之外的变量,但该变量的值不能发生变化,即一次赋值,永不改变
3、方法引用
通过lamda表达式可以访问某个类方法的唯一描述符

System.out::print

lamda表达式的使用

1、主要用处:用于替换以前常用的匿名内部类和回调函数,将其向函数式编程语言方向引导
关于函数式编程语言的介绍:https://www.cnblogs.com/formyfish/p/10436767.html
2、lamda表达式与集合类批处理操作
Stream接口api:包含对集合类进行过滤(filter)、去重(distinct)、排序(sort)、重组(map、collect、toArray、of、generate)、调试查看(peek)、限制元素数量(limit)、跳过前n位元素,即前n位元素去除(skip)、循环(foreach)、最大值(max)、最小值(min)、集合数量(count)、条件匹配(所有、任一、没有元素满足allMatch、anyMatch、noneMatch)、计算结果(reduce)
3、lamda表达式练习题
实体类Trader

public  class Trader{
 
    private String name;
    private String city;
 
    public Trader(String name, String city){
        this.name = name;
        this.city = city;
    }
 
    public String getName(){
        return this.name;
    }
 
    public String getCity(){
        return this.city;
    }
 
    public void setCity(String newCity){
        this.city = newCity;
    }
 
    public String toString(){
        return "Trader:"+this.name + " in " + this.city;
    }
}

实体类Transaction

public class Transaction{
 
    private Trader trader;
    private int year;
    private int value;
 
    public Transaction(Trader trader, int year, int value)
    {
        this.trader = trader;
        this.year = year;
        this.value = value;
    }
 
    public Trader getTrader(){
        return this.trader;
    }
 
    public int getYear(){
        return this.year;
    }
 
    public int getValue(){
        return this.value;
    }
 
    public String toString(){
        return "{" + this.trader + ", " +
                "year: "+this.year+", " +
                "value:" + this.value +"}";
    }
}
Trader raoul = new Trader("Raoul", "Cambridge");
Trader mario = new Trader("Mario", "Milan");
Trader alan = new Trader("Alan", "Cambridge");
Trader brian = new Trader("Brian", "Cambridge");
List<Transaction> transactions = Arrays.asList(
    new Transaction(brian, 2011, 300),
    new Transaction(raoul, 2011, 1000),
    new Transaction(raoul, 2011, 400),
    new Transaction(mario, 2012, 710),
    new Transaction(mario, 2012, 700),
    new Transaction(alan, 2012, 950)
);

1、找出2011年发生的所有交易,并按交易额排序

transactions.stream().filter(transaction->transaction.getYear()==2011).sorted
    (Comparator.comparing(Transaction::getValue)).peek(transaction->
    {System.out.println(transaction);}).collect(Collectors.toList());

filter:过滤出2011年发生的交易
sorted:排序,传入参数为Comparator,按交易额排序
peek:调试输出结果
collect:重组为新List
2、交易员在哪些不同的城市工作过

transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct()
    .peek(city->System.out.println(city)).collect(Collectors.toList());

map:重组集合
distinct:去重
3、查找所有来自剑桥的交易员,并按姓名排序

transactions.stream().map(Transaction::getTrader).filter(trader ->
    "Cambridge".equals(trader.getCity())).sorted(Comparator.comparing(Trader::getName)
    ).peek(name->System.out.println(name)).collect(Collectors.toList());

4、返回所有交易员的姓名字符串,并按字母顺序排序

System.out.println(transactions.stream().map(transaction->transaction.getTrader()
    .getName()).distinct().sorted(Comparator.comparing(name->name))
    .reduce("",(name1,name2)->name1+name2));

reduce:第一个参数是初始值;第二个参数为lamda表达式,即需要对集合元素进行的操作
5、有没有交易员在米兰工作的?

System.out.println(transactions.stream().
    anyMatch(transaction->"Milan".equals(transaction.getTrader().getCity())));

anyMatch:任意匹配,返回类型为boolean
6、打印生活在剑桥的交易员的所有交易额

System.out.println(transactions.stream().filter(transaction -> "Cambridge".equals
    (transaction.getTrader().getCity())).collect(Collectors.groupingBy(
    Transaction::getTrader,Collectors.summingInt(Transaction::getValue))));

7、所有交易中,最高的交易额是多少

System.out.println(transactions.stream().map(Transaction::getValue).
    reduce(0,Integer::max));

8、找到交易额最小的交易

System.out.println(transactions.stream().min(Comparator.comparing
    (Transaction::getValue)));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值