Java8-04-04-笔记

Posted 寻7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8-04-04-笔记相关的知识,希望对你有一定的参考价值。

Stream API练习

  • 使用之前学到的内容:创建Stream、Stream中间操作、Stream终止操作(终端操作),进行一个简单的练习

1,小试牛刀(@—@)

package com.sunstone.stream;

import com.sunstone.lambda.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class TestStreamAPI {
    //1,给定一个数字列表,输出每个数的平方数列表[1,2,3,4,5]->[1,4,9,16,25]
    @Test
    public void test1(){
        Integer[] nums=new Integer[]{1,2,3,4,5};
        Arrays.stream(nums).map((x)->x*x).forEach(System.out::println);
    }

    //2,使用map和reduce方法数一数流中有多少个元素
    @Test
    public void test2(){
        List<Employee> emps= Arrays.asList(
                new Employee("sun",17,9999),
                new Employee("stone",27,99999)
        );
        Optional<Integer> count = emps.stream().map((e)->1).reduce(Integer::sum);
        //Integer count =emps.stream().count();使用该种方式当然是可以的
        System.out.println(count.get());
    }
}

2,交易员练习

package com.sunstone.stream;

//交易员
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.sunstone.stream;

//交易事件
public class Transaction {

    private Trader trader;
    private Integer year;
    private Integer value;

    public Transaction() {
    }

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

    public Trader getTrader() {
        return trader;
    }

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

    public Integer getYear() {
        return year;
    }

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

    public Integer getValue() {
        return value;
    }

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

    @Override
    public String toString() {
        return "Transaction{" +
                "trader=" + trader +
                ", year=" + year +
                ", value=" + value +
                '}';
    }
}
package com.sunstone.stream;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class TestTransaction {

    List<Transaction> transactions = new ArrayList<>();

    @Before
    public void before() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");
        transactions = Arrays.asList(
                new Transaction(brian, 2009, 777),
                new Transaction(brian, 2013, 10000),
                new Transaction(raoul, 2016, 7777),
                new Transaction(mario, 2017, 789),
                new Transaction(raoul, 2017, 999),
                new Transaction(alan, 2020, 520)
        );
    }

    @Test
    public void test1() {//1,找出2017年发生的所有交易,并按交易额排序(从低到高)
        transactions.stream().filter((t) -> t.getYear() == 2017)
                .sorted((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()))
                .forEach(System.out::println);
    }

    @Test//2,交易员都在哪些不同的城市工作过
    public void test2() {
        transactions.stream().map((t) -> t.getTrader().getCity())
                .distinct().forEach(System.out::println);
    }

    @Test//3,在所有的交易信息中,查找来自剑桥的交易员,并按姓名排序
    public void test3() {
        transactions.stream().filter((t) -> t.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .sorted((t1, t2) -> t1.getName().compareTo(t2.getName()))
                .forEach(System.out::println);
    }

    @Test//4,返回所有交易员的姓名字符串,按字母顺序排序
    public void test4() {
        transactions.stream().map((t) -> t.getTrader().getName()).sorted().forEach(System.out::println);
        transactions.stream().map((t) -> t.getTrader().getName()).sorted().reduce("", String::concat);
    }

    @Test//5,有没有交易员在米兰工作
    public void test5() {
        boolean bl1 = transactions.stream().map((t) -> t.getTrader().getCity()).anyMatch((n) -> n.equals("Milan"));
        boolean bl2 = transactions.stream().anyMatch((t) -> t.getTrader().getCity().equals("Milan"));
        System.out.println("bl1:" + bl1 + "  bl2:" + bl2);
    }

    @Test//6,获取在剑桥工作的交易员的交易总额
    public void test6() {
        Optional<Integer> sum = transactions.stream().filter((e) -> e.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue).reduce(Integer::sum);
    }

    @Test//7,所有交易中,最高交易额
    public void test7() {
        Optional<Integer> maxValue = transactions.stream().map((t) -> t.getValue())
                .max(Integer::compare);
    }

    @Test//8,交易额最小的交易记录
    public void test8() {
        Optional<Transaction> opt = transactions.stream()
                .min((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()));
    }
}

以上是关于Java8-04-04-笔记的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记:python3,代码片段(2017)

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

sh bash片段 - 这些片段大多只是我自己的笔记;我找到了一些,有些我已经找到了

需要一种有效的方法来避免使用 Laravel 5 重复代码片段

Oracle-常用数据库对象笔记(片段)

如何使用导航组件处理片段内的向上按钮