Stream AP练习

问题描述:

1. 给定一个数字列表,如何返回一个由每个数的平方构成的列表 ?
例如: 给定 [1,2,3,4,5] 返回 [1,4,9,16,35]

解析:

	@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 方法数一数流中有多少个Employee

解析:

	List<Employee> emps = Arrays.asList(
            new Employee("张三", 50, 9999.99, Employee.Status.BUSY),
            new Employee("李四", 38, 9933.33, Employee.Status.FREE),
            new Employee("王五", 50, 6666.66, Employee.Status.VOCATION),
            new Employee("赵六", 16, 3333.33, Employee.Status.BUSY),
            new Employee("赵六", 18, 7777.77, Employee.Status.FREE),
            new Employee("赵六", 56, 7777.77, Employee.Status.FREE)
    );
    @Test
    public void test2() {
        Optional<Integer> op = emps.stream()
                .map((e) -> 1)
                .reduce(Integer::sum);
        System.out.println(op.get());
    }

package com.xiaomeng.java8;
/**
 * 交易员类
 * */
public class Trader {
    private String name;
    private String 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;
    }

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

    public Trader() {
    }

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


package com.xiaomeng.java8;
/**
 * 交易类
 * */
public class Transaction {
    private Trader trader;
    private int year;
    private int value;

    public Transaction() {
    }
    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;
    }

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

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

}


package com.xiaomeng.java8;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestTransaction {

    List<Transaction> transactions = null;

    // 初始化
    @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, 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.找出2011年发生的所有交易,并按交易额排序(升序)
    // 2.交易员都在哪些不同的城市工作过
    // 3.查找所有来自剑桥的交易员,并按姓名排序
    // 4.返回所有交易员的姓名字符串,安字母排序
    // 5.有没有交易员是在米兰工作的
    // 6.打印生活在剑桥的交易员的所有交易额
    // 7.所有交易中,最高的交易额是多少
    // 8.找到交易额做小的交易
    @Test
    public void test1() {
        transactions.stream()
                .filter( (t) -> t.getYear() == 2011)
                .sorted((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()))
                .forEach(System.out::println);
    }
    @Test
    public void test2() {
        transactions.stream()
                .map( (t) -> t.getTrader().getCity())
                .distinct()
                .forEach(System.out::println);
    }
    @Test
    public void test3() {
        transactions.stream()
                .map((t) -> t.getTrader())
                .sorted((t1, t2) -> t1.getName().compareTo(t2.getName()))
                .filter((e) ->e.getCity() == "Cambridge")
                .distinct()
                .forEach(System.out::println);
        System.out.println("------------------");
        transactions.stream()
                .filter((t) -> t.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getTrader)
                .sorted((t1, t2) -> t1.getName().compareTo(t2.getName()))
                .distinct()
                .forEach(System.out::println);
    }
    @Test
    public void test4() {
        transactions.stream()
                .map((t) -> t.getTrader().getName())
                .sorted()
                .forEach(System.out::println);
        System.out.println("---------------------");
        // 拼接成一个字符串
        String str = transactions.stream()
                .map((t) -> t.getTrader().getName())
                .sorted()
                .reduce("", String::concat);
        System.out.println(str);
        System.out.println("---------------------");

        // 返回的一串字符串内部按字母排序
        transactions.stream()
                .map((t) -> t.getTrader().getName())
                .flatMap(TestTransaction::filterCharacter) // 都放在一个流中
                .sorted((s1, s2) -> s1.compareToIgnoreCase(s2)) // 忽略大小写
                .forEach(System.out::println);
    }

    public static Stream<String> filterCharacter(String str) {
        List<String> list = new ArrayList<>();
        for (Character ch : str.toCharArray()) {
            list.add(ch.toString());
        }
        return list.stream();
    }

    @Test
    public void test5() {
        boolean b1 = transactions.stream()
                .anyMatch((t) -> t.getTrader().getCity().equals("Milan"));
        System.out.println(b1);
    }

    @Test
    public void test6() {
        Optional<Integer> sumOp = transactions.stream()
                .filter((e) -> e.getTrader().getCity().equals("Cambridge"))
                .map(Transaction::getValue)
                .reduce(Integer::sum);
        System.out.println(sumOp.get());
    }

    @Test
    public void test7() {
        Optional<Integer> maxOp = transactions.stream()
                .map((t) -> t.getValue())
                .max(Integer::compare);
        System.out.println(maxOp);
    }
    @Test
    public void test8() {
        Optional<Transaction> minOP = transactions.stream()
                .min((t1, t2) -> Integer.compare(t1.getValue(), t2.getValue()));
        System.out.println(minOP.get());
    }


}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页