Stream流案例

  • 找出2011年发生的所有交易,并按交易额排序(从低到高)
  • 交易员都在哪些不同的城市工作过
  • 查找所有来自于剑桥的交易员,并按姓名排序
  • 返回交易员的姓名字符串,按字母顺序排序
  • 有没有交易员是在米兰工作的?
  • 打印生活在剑桥的交易员的所有交易额
  • 所有交易中,最高的交易额是多少?
  • 找到交易额最小的交易
public static void main(String ...args){    
    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, 2012, 1000),
        new Transaction(raoul, 2011, 400),
        new Transaction(mario, 2012, 710),	
        new Transaction(mario, 2012, 700),
        new Transaction(alan, 2012, 950)
    );	
	// 找出2011年发生的所有交易,并按交易额排序(从低到高)
	List<Transaction> tr2011 = transactions.stream()
										   .filter(t -> t.getYear() == 2011)
										   .sorted(comparing(Transaction::getValue))
										   .collect(toList());
	// 交易员都在哪些不同的城市工作过
	List<String> cities = transactions.stream()
									  .map(t -> t.getTrader().getCity())
									  .distinct()
									  .collect(toList());
	// 查找所有来自于剑桥的交易员,并按姓名排序
	List<Trader> traders = transactions.stream()
									   .map(t -> t.getTrader()) // t指transactions的每个元素
									   .filter(t -> t.getCity().equals("Cambridge")) // t指映射后的traders的每个元素,可以根据实际情况写全,便于阅读
									   .distinct()
									   .sorted(comparing(Trader::getName));
	// 返回所有交易员的姓名字符串,按字母顺序排序
	List<String> names = transactions.stream()
									 .map(t -> t.getTrader().getName())
									 .distinct()
									 .sorted()
									 .reduce("", (n1, n2) -> n1 + n2);
	// 有没有交易员是在米兰工作的?
	boolean miLanBased = transactions.stream()
									 anyMatch(t -> t.getTrader().getCity().equals("Milan"));
	// 打印生活在剑桥的交易员的所有交易额
	transactions.stream()
				.filter(t -> t.getTrader().getCity().equals("Cambridge"))
				.map(Transaction::getValue)
				.forEach(System.out::println);
	// 所有交易中,最高的交易额是多少?
	Optional<Integer> highestValue = transactions.stream()
												 .map(Transaction::getValue)
												 .reduce(Integer::max);
	// 找到交易额最小的交易
	Optional<Transaction> smallestTransaction = transactions.stream()
												  			.reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2);
												  
	Optional<Transaction> smallestTransaction0 = transactions.stream()
												  			.min(comparing(Transactions::getValue))
}
public  class Trader{
	
	private String name;
	private String city;

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

	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;
	}
}
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 +"}";
	}
}
public class Dish {

    private final String name;
    private final boolean vegetarian;
    private final int calories;
    private final Type type;

    public Dish(String name, boolean vegetarian, int calories, Type type) {
        this.name = name;
        this.vegetarian = vegetarian;
        this.calories = calories;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public int getCalories() {
        return calories;
    }

    public Type getType() {
        return type;
    }

    public enum Type {MEAT, FISH, OTHER}

    @Override
    public String toString() {
        return name;
    }

    public static final List<Dish> menu =
            Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
                    new Dish("beef", false, 700, Dish.Type.MEAT),
                    new Dish("chicken", false, 400, Dish.Type.MEAT),
                    new Dish("french fries", true, 530, Dish.Type.OTHER),
                    new Dish("rice", true, 350, Dish.Type.OTHER),
                    new Dish("season fruit", true, 120, Dish.Type.OTHER),
                    new Dish("pizza", true, 550, Dish.Type.OTHER),
                    new Dish("prawns", false, 400, Dish.Type.FISH),
                    new Dish("salmon", false, 450, Dish.Type.FISH));
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值