Stream API习题

题目:

  1. 找出 2011 年发生的所有交易, 并按交易额排序(从低到高)
  2. 交易员都在哪些不同的城市工作过
  3. 查找所有来自剑桥的交易员,并按姓名排序
  4. 返回所有交易员的姓名字符串,按字母顺序排序
  5. 有没有交易员是在米兰工作的
  6. 打印生活在剑桥的交易员的所有交易额
  7. 所有交易中,最高的交易额是多少
  8. 找到交易额最小的交易
    题目的代码:
    package com.company;
    

public class Trader {
private String name;
private String city;

public Trader() {
}

public Trader(String name, String city) {
    this.name = name;
    this.city = city;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

@Override
public String toString() {
    return "Trader [name=" + name + ", city=" + city + "]";
}

}

交易类:

package com.company;

//交易类
public class Transaction {

private Trader trader;
private int year;
private int value;

public Transaction() {
}

public Transaction(Trader trader, int year, int value) {
    this.trader = trader;
    this.year = year;
    this.value = value;
}

public Trader getTrader() {
    return trader;
}

public void setTrader(Trader trader) {
    this.trader = trader;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

@Override
public String toString() {
    return "Transaction [trader=" + trader + ", year=" + year + ", value="
            + value + "]";
}

}

答案代码:

Transaction[] transactions = new Transaction[]{
new Transaction(new Trader("tom","wuhan"),1997,100),
new Transaction(new Trader("vic","beijing"),1995,2300),
new Transaction(new Trader("girls","wuhan"),1997,500),
new Transaction(new Trader("boys","jianqiao"),2011,600),
new Transaction(new Trader("boy2","jianqiao"),2011,600),
new Transaction(new Trader("yin","nanning"),2011,400),
new Transaction(new Trader("yin","nanning"),2011,500)
};
Stream<transaction> stream = Arrays.stream(transactions);
System.out.println("1. 找出 2011 年发生的所有交易, 并按交易额排序(从低到高)");
stream.filter(transaction -> transaction.getYear() == 2011).sorted((o1,o2) -> Integer.compare(o1.getValue(),o2.getValue())).forEach(System.out::println);
System.out.println("2. 交易员都在哪些不同的城市工作过");
Stream<transaction> stream2 = Arrays.stream(transactions);
stream2.map(transaction -> transaction.getTrader().getCity()).distinct().forEach(System.out::println);
System.out.println("3. 查找所有来自剑桥的交易员,并按姓名排序");
Stream<transaction> stream3 = Arrays.stream(transactions);
stream3.filter(transaction -> "jianqiao".equals(transaction.getTrader().getCity())).sorted((o1,o2) -> o1.getTrader().getName().compareTo(o2.getTrader().getName())).forEach(System.out::println);
System.out.println("4. 返回所有交易员的姓名字符串,按字母顺序排序");
Stream<transaction> stream4 = Arrays.stream(transactions);
Stream<string> stringStream = stream4.map(transaction -> transaction.getTrader().getName());
stringStream.sorted(String::compareTo).forEach(System.out::println);
System.out.println("5. 有没有交易员是在米兰工作的");
Stream<transaction> stream5 = Arrays.stream(transactions);
System.out.println(stream5.anyMatch(transaction -> "nanning".equals(transaction.getTrader().getCity())));
System.out.println("6. 打印生活在剑桥的交易员的所有交易额");
Stream<transaction> stream6 = Arrays.stream(transactions);
Stream<integer> integerStream = stream6.filter(transaction -> "jianqiao".equals(transaction.getTrader().getCity())).map(Transaction::getValue);
integerStream.forEach(System.out::println);
System.out.println("7. 所有交易中,最高的交易额是多少");
Stream<transaction> stream7 = Arrays.stream(transactions);
Optional<integer> max = stream7.map(transaction -> transaction.getValue()).max(Integer::compareTo);
System.out.println(max.get());
System.out.println("8. 找到交易额最小的交易");
Stream<transaction> stream8 = Arrays.stream(transactions);
Optional<transaction> min = stream8.min((o1, o2) -> Integer.compare(o1.getValue(), o2.getValue()));
System.out.println(min.get());
```</transaction></transaction></integer></transaction></integer></transaction></transaction></string></transaction></transaction></transaction></transaction>

全部评论

相关推荐

11-14 16:13
已编辑
重庆科技大学 测试工程师
Amazarashi66:不进帖子我都知道🐮❤️网什么含金量
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务