Collection
public class Dish {
public Dish(String name, boolean vegetarian, Integer calories, Type type) {
this.name = name;
this.vegetarian = vegetarian;
this.calories = calories;
this.type = type;
}
public String getName() {
return name;
}
public Integer getCalories() {
return calories;
}
public boolean isVegetarian() {
return vegetarian;
}
public Type getType() {
return type;
}
private final String name;
private final boolean vegetarian;
private final Integer calories;
private final Type type;
public enum Type {MEAT, FISH, OTHER}
}
static 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, 300, Dish.Type.FISH)
, new Dish("prawns", false, 300, Dish.Type.FISH)
, new Dish("salmon", false, 450, Dish.Type.FISH) );
//dish数量
menu.stream().collect(counting())
menu.stream().count()
//calories最多的dish
menu.stream().collect(maxBy(comparing(Dish::getCalories))).ifPresent(d -> System.out.println(d.getCalories()));
class Transaction{
private String currency;
private int value;
public Transaction(String currency, int value) {
this.currency = currency;
this.value = value;
}
public Transaction(String currency) {
this.currency = currency;
}
public String getCurrency() {
return currency;
}
public int getValue() {
return value;
}
public Boolean isExpensive(){
return this.value > 500?Boolean.TRUE:Boolean.FALSE;
}
}
static List<Transaction> transactions = Arrays.asList(new Transaction("RMB",200)
, new Transaction("RMB", 600)
, new Transaction("USD", 100)
, new Transaction("USD", 800));
//按货币分组
Map<String, List<Transaction>> transactionsSameCurrency = transactions.stream().collect(groupingBy(Transaction::getCurrency));
//分成高价和低价两组
Map<Boolean, List<Transaction>> transactionsSameCurrency = transactions.stream().collect(groupingBy(Transaction::isExpensive));
//交易的总价格
int sum = transactions.stream().collect(summingInt(Transaction::getValue));
//数据
int[] i = {1, 3, 5};
IntStream intStream = Arrays.stream(i);
System.out.println(intStream.sum());
//从文件中获取Stream。计算文本里单词数量,并且去重
static void linesStream(){
try (Stream<String> lines
= Files.lines(Paths.get("c://work//a.txt"), Charset.defaultCharset())) {
long count1 = lines
.flatMap(line -> Arrays.stream(line.split(" ")))
.distinct()
.count();
System.out.println(count1);
}catch(Exception e){
e.printStackTrace();
}
}
//输出偶数
static void iterateStream(){
Stream.iterate(0, n -> n+2)
.limit(10)
.forEach(System.out::println);
}
//fibonnacci,当前值是前两个数的和
static void fibonnacci(){
Stream.iterate(new int[]{0, 1}, n -> new int[]{n[1], n[0]+n[1]})
.limit(20)
.map(t -> t[0])
.forEach(System.out::println);
}
//后去20个随机数
static void generateStream(){
Stream.generate(Math::random)
.limit(20)
.forEach(System.out::println);
}
//fibonnacci,当前值是前两个数的和
static void generateStateStream(){
IntSupplier intSupplier = new IntSupplier() {
private int privious = 0;
private int current = 1;
@Override
public int getAsInt() {
int oldPrevious = this.privious;
this.privious = this.current;
this.current = oldPrevious + this.current;
return oldPrevious;
}
};
IntStream.generate(intSupplier)
.limit(10)
.forEach(System.out::println);
}