您的位置:首页 > 编程语言 > Java开发

Java8的学习计划--StreamsInAction

2018-02-27 17:37 387 查看
今年计划的学习的任务比较重 恶补中
下面是Stream的练习package com.company.LambdaExpressions;

import com.company.java8.Trader;
import com.company.java8.Transaction;

import java.util.*;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
* ${DESCRIPTION}
* StreamInAction
* @author mengxp
* @version 1.0
* @create 2018-02-27 16:58
**/
public class MyStreamInAction {

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> dataSource = 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)
);

//1. Find all transactions in the year 2011 and sort them by value (small to high).
final List<Transaction> collect = dataSource.stream().filter(transaction -> transaction.getYear() == 2011)
.sorted(Comparator.comparing(Transaction::getValue))
.collect(toList());
collect.forEach(System.out::println);

//2.What are all the unique cities where the traders work?
dataSource.stream().map(transaction -> transaction.getTrader().getCity())
.distinct()
.collect(toList())
.forEach(System.out::println);

System.out.println("==================");

//3.Find all traders from Cambridge and sort them by name.
dataSource.stream()
.map(transaction -> transaction.getTrader())
.filter(t->t.getCity().equalsIgnoreCase("Cambridge"))
.distinct()
.sorted(Comparator.comparing(Trader::getName))
.collect(toList()).forEach(System.out::println);

//4.Return a string of all traders’ names sorted alphabetically
final String reduce = dataSource.stream().map(transaction -> transaction.getTrader().getName())
.distinct()
.sorted()
.reduce("", (name1, name2) -> name1 + " " + name2);
System.out.println(reduce);

//5. Are any traders based in Milan?

final boolean flag = dataSource.stream().anyMatch(transaction -> transaction.getTrader().getCity().equalsIgnoreCase("Milan"));
final boolean flagV = dataSource.stream().map(Transaction::getTrader).anyMatch(t -> t.getCity().equalsIgnoreCase("Milan"));
System.out.println("xxx"+flag+flagV);

//6.Print all transactions’ values from the traders living in Cambridge.
dataSource.stream().filter(transaction -> transaction.getTrader().getCity().equalsIgnoreCase("Cambridge"))
.map(Transaction::getValue)
.forEach(System.out::println);

//7.What’s the highest value of all the transactions?
dataSource.stream().filter(transaction -> transaction.getTrader().getCity().equalsIgnoreCase("Cambridge"))
.mapToInt(Transaction::getValue)
.max().ifPresent(a->System.out.println("max: "+a));

//7.2 方式二
Optional<Integer> maxValue = dataSource.stream().map(Transaction::getValue).reduce((i, j) -> i > j ? i : j);
System.out.println(maxValue.get());

//8.Find the transaction with the smallest value.
final OptionalInt min = dataSource.stream().mapToInt(Transaction::getValue).min();
System.out.println("min:"+min.orElse(-1));

//方式二

dataSource.stream().map(Transaction::getValue)
.reduce(Integer::min).ifPresent(m->System.out.println("Min: "+m));

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java8